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

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.

主站蜘蛛池模板: 邓州市| 哈密市| 安阳市| 和龙市| 贵溪市| 全椒县| 辽宁省| 同心县| 正安县| 莱阳市| 任丘市| 广汉市| 锡林浩特市| 沧州市| 兴国县| 宝鸡市| 昔阳县| 万盛区| 苍梧县| 香港| 潞城市| 建始县| 边坝县| 黄骅市| 新闻| 高安市| 济宁市| 兰考县| 溆浦县| 炎陵县| 红安县| 茂名市| 彭阳县| 南阳市| 炉霍县| 绥宁县| 瓦房店市| 武威市| 吉安县| 犍为县| 全南县|