- Building Serverless Web Applications
- Diego Zanon
- 379字
- 2021-07-15 17:31:32
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.
推薦閱讀
- Beginning Java Data Structures and Algorithms
- Python程序設計(第3版)
- C語言程序設計基礎與實驗指導
- PHP 編程從入門到實踐
- Julia Cookbook
- PySide GUI Application Development(Second Edition)
- Visual C#通用范例開發金典
- WordPress 4.0 Site Blueprints(Second Edition)
- Python算法詳解
- Training Systems Using Python Statistical Modeling
- Java面試一戰到底(基礎卷)
- 微信公眾平臺開發最佳實踐
- Tkinter GUI Application Development Blueprints
- Instant SQL Server Analysis Services 2012 Cube Security
- Kotlin核心編程