Create an AWS API Gateway for AWS Lambda

Create an AWS API Gateway for AWS Lambda

aws-api-gateway

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.



Create a New API

  1. Click the Create API button
  2. Name your API and put a description.
  3. Choose between Regional or Edge Optimized endpoint (For now we'll choose Regional for better performance.)
  4. Click Create to be redirected your API dashboard.
create-new-api
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

  1. In the Actions dropdown, choose Create Resource
  2. Name your Resource and choose Enable API Gateway CORS
  3. Click Create to proceed.
create-resource
Create Resource


resource-properties
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


  1. In the dashboard, highlight your newly created "list-my-files" resource.
  2. Then click Create Method, a dropdown will be visible.
  3. Choose POST, click the check button.
  4. You will be directed to the Method Setup screen




create-method
Actions > Create Method

 


  1. In the Method Setup Screen
  2. Integration Type should be Lambda Function
  3. Lambda Region - Your region
  4. Lambda Function - Your function name. The screen automatically shows all the available functions that you have.
  5. Click Save









method-setup
Method Setup





Making Sure that The API is Accessible

  1. Four boxes will be shown to you after the previous steps are made. Method Request, Integration Request, Method Response, Integration Response
  2. Click Method Response
  3. Expand the 200 HTTP Status
  4. Click Add Header and type in ACCESS-CONTROL-ALLOW-ORIGIN
  5. Click the check button.
request-response
Request and Response


 
adding-ACCESS-CONTROL-ALLOW-ORIGIN
Adding ACCESS-CONTROL-ALLOW-ORIGIN so that the browser knows the API is allowed to be accessed from outside the domain.

  1. Go back to Method Execution
  2. Click Integration Response
  3. Expand the 200 Method Response Status
  4. Expand Header Mappings
  5. You will see the ACCESS-CONTROL-ALLOW-ORIGIN that you added earlier.
  6. Click the Pencil icon adjacent to the entry.
  7. Type in '*' for the value then click the check button.


integration-response-setup
Integration Response Setup

mapping-value
The wild card (*) Value means "Accept Requests from Any Origin"




 




Deploy API


  1. To deploy the API just click the Actions dropdown and choose a stage where your API will be deployed.
  2. An Invoke URL will be shown, this will be the API Endpoint.
  3. Now the API is ready and accessible via
    POST https://99800b5.execute-api.ap-northeast-1.amazonaws.com/alpha/list-my-files
     
  4. Parameters:
{
    "Bucket":"bucket.domain.com",
    "Prefix":"directoryName"
}
action-deploy
Actions > Deploy API
deployment-information
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

Popular posts from this blog

Terraform: Merge a List ofObjects into One

Send SMS using Windows and any GSM Modem

SMS Notification using Gammu on Linux