官术网_书友最值得收藏!

Expose your Lambda function using API Gateway

Let's use the API Gateway to expose our previous Lambda function to be accessible using a URL by performing the following steps:

  1. First, go to the API Gateway Management Console by visiting this link https://console.aws.amazon.com/apigateway and click on Create API.

 

  1. Under the Create new API header, select the New API option and type the API name. For example: log-processor:
  1. Under Resources, click on the Actions dropdown and choose Create Method:
  1. In the new dropdown, select the POST HTTP verb.
  2. Under - POST - Setup, select Lambda Function as our Integration type. Select the region in which you have deployed our previous Lambda function and its corresponding name and click on Save button:
  1. A popup will request that you allow this method to access the Lambda function. Accept it.
  2. Under Resources, click again in the Actions drop-down and choose Deploy API.
  3. A popup will request a stage to be selected. You can select [New Stage] and name it as dev:
  1. Click on Deploy. The next screen will show where the API Gateway was deployed. The URL will follow this format, such as https://[identifier].execute-api.[region].amazonaws.com/dev.
  1. If you try this URL in your browser, the result will be an authentication error that happens because the browser will try a GET request that was not defined. As we have defined only a POST resource, we need another way to test. You can use the API Gateway test feature that is accessible under the Resources feature in the left menu, followed by selecting the POST verb and clicking on Test.
  2. You need to provide a Request Body. In our case, it will be a JSON object with a format similar to a new S3 object event:
        {
"Records": [
{
"s3": {
"bucket": {
"name": "my-bucket-name"
},
"object": {
"key": "logs/log1.txt"
}
}
}
]
}
  1. You just need to change the bucket name for the name that you used previously, and the API Gateway will trigger the Lambda function to process the log1.txt file that we have uploaded before.

Another way to test this integration with the API Gateway is using Postman, which is a very popular tool to test any kind of RESTful APIs. Postman can be installed as a Chrome extension or as a macOS application.

主站蜘蛛池模板: 盐津县| 武宣县| 泸州市| 响水县| 靖远县| 保靖县| 临洮县| 新巴尔虎右旗| 北川| 柏乡县| 嵊泗县| 昂仁县| 绥德县| 乌鲁木齐县| 绥宁县| 万盛区| 水城县| 嘉义市| 保山市| 孝感市| 汕头市| 辽宁省| 类乌齐县| 泸水县| 奉新县| 顺平县| 阳新县| 巫山县| 庄河市| 丘北县| 大田县| 南开区| 黎城县| 安乡县| 新绛县| 八宿县| 万年县| 大方县| 峡江县| 夏河县| 颍上县|