AWS API Gateway helps us to expose API to end users by integrating with different AWS services or other HTTP services. Using API Gateway we can expose, monitor and secure the APIs. It doesn't restrict the number of requests concurrently. It can be scaled at any level. We can expose the RESTFul API so that end users (like web applications, mobile apps) can invoke the API.
API Gateway pricing is based on the usage of the APIs. We don't need to pay any upfront fee to create API. It is being charged based on the API calls received and data that is being transferred out. AWS API Gateway also provides data caching mechanism where it charges more based on the cached data size.
In this article we will demonstrate how we can create API Gateway and integrate with Lambda Function. This becomes part of the serverless architecture where we don't have any server to deploy API or run our business logic on server.
For this article we expect Lambda function is already created as demonstrated in article.
Create and Configure API Gateway
- Sign-in to AWS Console
- Click on API Gateway under Application Services or go to https://console.aws.amazon.com/apigateway/home
- Click Create API
Here we have multiple options to create API as:- New API: Choose this option when you want to create API by your own step-by-step
- Clone from existing API: Choose this option when you want to create API by cloning from existing API
- Import from Swagger: Choose this option when you have Swagger API definition and that API definition can be imported here to create API
- Example API: Choose this option when you want to create API using example API
- Create Resource
Here we will create greet resource that acts as a endpoint suffix for our API.
Under Resources navigation pane, click Actions > Create Resource
You will be redirected to Add Child Resource screen
Provide the following details:- Configure as proxy resource: We can configure the resource to act as a proxy resource
- Resource Name: Friendly resource name
- Resource Path: Endpoint path suffix for the resource to be identified
- Enable API Gateway CORS: This parameter needs to be enabled when we want our API to be consumed from domains other than our API's domain.
- Create Method
Here we will create Post HTTP method under greet resource. Select the /greet resource and click Actions > Create Method
You will be asked to choose HTTP method you want to create as:
Choose POST and click checked symbol
- Integrate Lambda Function
Here we will integrate lambda function when our greet's POST method is being called. Provide the following details:- Integration Type: API gateway offer's four types of integration
- Lambda Function: Invoke lambda function when the API Gateway resource method gets invoked
- HTTP: Invoke existing HTTP endpoint
- Mock: We can return the mock response to the service call
- AWS Service: We can integrate various AWS services
- Use Lambda Proxy integration: Select whether lambda function acts as proxy integration. We will deselect this property for our example.
- Lambda Region: Select the region where your Lambda Function resides. We will select us-east-1 as we created lambda function in this region.
- Lambda Function: Choose your lambda function
Click OK
- Integration Type: API gateway offer's four types of integration
- Deploy API
Under this step, we will deploy our API to specific stage. Stage defines the environment of your API - Development, Test, Production, etc.
Click Actions > Deploy API.
You will be prompted for deploy API inputs:- Deployment stage: Choose [New Stage] option
- Stage name: Your environment name
- Stage description: Description for stage
- Deployment description: Description of deployment being done to specific stage. This may include what changes made to API.
Click Deploy. You will be redirected to Stages where you can see your stage is being created and Endpoint for your resource is created.
- Testing
Our API is ready to be consumed. Copy the Invoke URL and test using any Rest Client tool of your choice. Provide the details as:- URL: https://hlrpmcn911.execute-api.us-east-1.amazonaws.com/Development/greet
- Method: POST
- Body: "Addo"
You can see the output that is being returned from the lambda function via API Gateway.
Conclusion
In this article we have seen how to configure API Gateway and integrate lambda function using the AWS Management Console.