Create an AWS API Gateway for AWS Lambda
Create an AWS API Gateway for AWS Lambda
Goal:
- Function will list all items inside an S3 Bucket
- Should be able to be triggered via API
What we'll have in the end:
A Working API URL that will run our AWS Lambda function and returns response.
If this is your first time, you would want to know how Easy Way to Create a Lambda Function first before making and API.
The API can now be accessed by Postman or AJAX via POST method.
with the Bucket and Prefix parameters.
The Lambda Function will then be triggered by the API fulfilling the logic of your program.
We will tackle API Security on my next entry.
- Function will list all items inside an S3 Bucket
- Should be able to be triggered via API
A Working API URL that will run our AWS Lambda function and returns response.
If this is your first time, you would want to know how Easy Way to Create a Lambda Function first before making and API.
Create a New API
- Click the Create API button
- Name your API and put a description.
- Choose between Regional or Edge Optimized endpoint (For now we'll choose Regional for better performance.)
- Click Create to be redirected your API dashboard.
Create a New API |
Regional Endpoint: A regional API endpoint is a new type of endpoint that is accessed from the same AWS region in which your REST API is deployed. This helps you reduce request latency when API requests originate from the same region as your REST API. Additionally, you can now choose to associate your own Amazon CloudFront distribution with the regional API endpoint.
Edge Optimized Endpoint: The edge-optimized API. Edge-optimized APIs are endpoints that are accessed through a CloudFront distribution that is created and managed by API Gateway. Previously, edge-optimized APIs were the default option for creating APIs with API Gateway. To know more about API Endpoints, visit Amazon API Gateway Support
Create a New Resource
- In the Actions dropdown, choose Create Resource
- Name your Resource and choose Enable API Gateway CORS
- Click Create to proceed.
Create Resource |
Resource Properties |
When you enable CORS, or Cross-Origin Resource Sharing, your API will be accessible to other hosts.
Allowing an AJAX call to your cloud API URL from your Local Host.
There are security measures that are needs to be done, but for the sake of simplicity, we won't be tackling them here. For now, our aim is to create a working API URL.
Create a New Method
- In the dashboard, highlight your newly created "list-my-files" resource.
- Then click Create Method, a dropdown will be visible.
- Choose POST, click the check button.
- You will be directed to the Method Setup screen
Actions > Create Method |
- In the Method Setup Screen
- Integration Type should be Lambda Function
- Lambda Region - Your region
- Lambda Function - Your function name. The screen automatically shows all the available functions that you have.
- Click Save
Method Setup |
Making Sure that The API is Accessible
- Four boxes will be shown to you after the previous steps are made. Method Request, Integration Request, Method Response, Integration Response
- Click Method Response
- Expand the 200 HTTP Status
- Click Add Header and type in ACCESS-CONTROL-ALLOW-ORIGIN
- Click the check button.
Request and Response |
Adding ACCESS-CONTROL-ALLOW-ORIGIN so that the browser knows the API is allowed to be accessed from outside the domain. |
- Go back to Method Execution
- Click Integration Response
- Expand the 200 Method Response Status
- Expand Header Mappings
- You will see the ACCESS-CONTROL-ALLOW-ORIGIN that you added earlier.
- Click the Pencil icon adjacent to the entry.
- Type in '*' for the value then click the check button.
Integration Response Setup |
The wild card (*) Value means "Accept Requests from Any Origin" |
Deploy API
- To deploy the API just click the Actions dropdown and choose a stage where your API will be deployed.
- An Invoke URL will be shown, this will be the API Endpoint.
- Now the API is ready and accessible via
POST https://99800b5.execute-api.ap-northeast-1.amazonaws.com/alpha/list-my-files - Parameters:
{
"Bucket":"bucket.domain.com",
"Prefix":"directoryName"
}
Actions > Deploy API |
Deployment information |
The API can now be accessed by Postman or AJAX via POST method.
with the Bucket and Prefix parameters.
The Lambda Function will then be triggered by the API fulfilling the logic of your program.
We will tackle API Security on my next entry.
Comments