Configure an API endpoint with AWS Lambda function: A Step-by-step guide
AWS services often contribute to shaping our day-to-day code with its seamless and efficient services. Some prominent AWS tools used for serverless computing with HTTP requests are AWS Lambda and AWS API Gateway. This article will be exploring the configuration of an API Gateway URL to a priorly created Lambda function.
If you are unfamiliar with AWS-related jargon here is a small insight to get a better understanding
- AWS Lambda- serverless service that runs code with a minimum configuration of servers
- API Gateway — service used for creating and maintaining REST, HTTP, and WebSocket APIs
- IAM — web service that provides secure access to AWS resources
- Stages — deployment environment for an API
- Base URL — url prefix for all API paths
Creating API
First, navigate to the AWS Management Console and access API Gateway Services, under REST API must select “Build”.If there are priorly created APIs then select under next you must select ‘Create API’.Next, proceed with a new API name and set the endpoint type to “regional”.
After creating the API it is required to configure the required security protocols and create the resources that are to be mapped to your lambda function.
Creating Lambda function
Once the API Gateway is ready the next step is to create the relevant Lambda function. From the AWS console navigate to the AWS Lambda service and select “Create function”.Afterward, you will be prompted to configure your lambda function and set up triggers, we will be using API Gateway as the trigger for this task. For simplicity let’s select the Hello World blueprint. You can also test your lambda function as an API gateway test event
Now that the API and Lambda function is created it’s a matter of fact to connect both of them. You will need to navigate back to API Gateway Console. Here select your API and proceed to Resources. You must go to the Action dropdown and select Create Resource and provide a resource name, let’s name it as an endpoint. Remember to set the Resource path as /endpoint. Now in the resources list select /endpoint and you will have to select the appropriate HTTP method and the Integration to use, for this task select REST API and Lambda Function Integration.
Deploy API
The last step is to deploy API, choose Deploy API from the Actions dropdown, enter details, and choose Deploy. In your API access stage, you will note the base URL of your API Gateway. Once you identified the base URL if you go to your lambda function the Trigger of \ API gateway has appended it with the resource name. The format is as such {base URL}/{resource-path}. For example, if your base URL is “https://abcd123.execute-api.eu-central-1.amazonaws.com/api". It will be as “https://abcd123.execute-api.eu-central-1.amazonaws.com/api/endpoint".
That is the basic procedure to configure an API Gateway URL with a lambda function. Hope you have a better understanding of the topic.