- 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.
推薦閱讀
- 大學計算機基礎(第三版)
- Angular UI Development with PrimeNG
- Apache Hive Essentials
- R語言編程指南
- 面向STEM的Scratch創新課程
- Implementing Cisco Networking Solutions
- JavaScript 程序設計案例教程
- Java項目實戰精編
- 數據結構習題解析與實驗指導
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- C專家編程
- Nagios Core Administration Cookbook(Second Edition)
- 3ds Max 2018從入門到精通
- Java自然語言處理(原書第2版)