
上QQ阅读APP看书,第一时间看更新
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:
- First, go to the API Gateway Management Console by visiting this link https://console.aws.amazon.com/apigateway and click on Create API.
- Under the Create new API header, select the New API option and type the API name. For example: log-processor:

- Under Resources, click on the Actions dropdown and choose Create Method:

- In the new dropdown, select the POST HTTP verb.
- 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:

- A popup will request that you allow this method to access the Lambda function. Accept it.
- Under Resources, click again in the Actions drop-down and choose Deploy API.
- A popup will request a stage to be selected. You can select [New Stage] and name it as dev:

- 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.
- 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.
- 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"
}
}
}
]
}
- 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.