Exposing AWS Lambda functions with the API Gateway

Amazon API Gateway is an AWS service that enables developers to create, publish, maintain, monitor, and secure APIs at any scale. You can create APIs that access AWS or other web services or resources, or data stored in the AWS Cloud. Together with AWS Lambda, API Gateway forms the key app-facing part of the AWS serverless infrastructure. You will find the combination of API Gateway with AWS Lambda a great solution for Alexa Skill development, Mobile App backend development, Web services or Microservices development, and just about any modern cloud development activities, will be involved with.

In this article, we will start with the most basic AWS Lambda function, fronted by an API Gateway endpoint, and incrementally step through some of the most used and key aspects of integrating these two together. If you still have not seen the Sigma IDE for Serverless computing, just visit https://sigma.slappforge.com and register for a new account for free, and you could try out and use it to improve your Serverless development, without having to install any software bloating up your local computer, as everything we do will only need a web browser.

The basic Lambda function – Getting Started!

Lets start with the empty Lambda function generated by the SLAppForge Sigma IDE by default and insert a console log statement to print the received event. Lets leave the rest as-is for now.

exports.handler = async (event) => {
console.log("Event: " + JSON.stringify(event));
return {"message": "Successfully executed"};

Then you will need to drag the ‘API Gateway’ element from your Resources tab in the left pane of the IDE and drop it on the handler function definition line as follows to integrate the Lambda function with the API Gateway.

Link Lambda function to API Gateway

Now you will be prompted to provide the key details of the Lambda function integration with the API Gateway resource you will create.

API Gateway definition


Here you can define an API Gateway ‘REST API’ or a ‘HTTP API’. Also note that you are required to provide a ‘Deployment stage’ which we can state as ‘dev’ for our example. But you can define this as needed, and will become part of the URL given to your API as you will see shortly. The ‘method’ can specify if your API should only support a specific HTTP method such as POST or GET etc, or leaving this as ANY will allow it to accept any HTTP method to its URL. The ‘Resource Path’ is a core part of the exposed API URL, and for our example lets specify it to be ‘/01-basic-lambda’. You should also specify a human readable name for the API being defined, but lets leave the advanced attributes out of our discussion for now.

Deploying and Testing Lambda function

Deploying to AWS – without editing configuration files and executing CLI commands

As you can see above, once the API Gateway trigger is linked to your Lambda function, the handler function definition line (line #1 above) gets a green icon that can be clicked on to change the trigger or edit it later. However as can be seen above, you have the ability to ‘Deploy Project’ to live AWS cloud in one click, and the ability to commit to a Git-based VCS system through the ‘Save Project’ icon. You also have the ability to ‘Create an event’ for testing and to run this function or to step through and debug it live while executing on the AWS platform (not in a simulated mode, but live on the real AWS platform). An important time-saver is the ‘Quick Deploy’ which will be able to push code only changes in less than 3 seconds to the live AWS environment, after the first full deployment you make through the ‘Deploy Project’ option.

Lets ‘Deploy Project’ and you will see that the Sigma IDE has done everything on behalf of you, like automatically creating the minimum security policy required for the execution, creation, and applying of the CloudFormation configuration to perform the deployment, etc. On completion of this step you are presented with the ARN of the Lambda to invoke via Alexa skills etc, and the API Gateway endpoint URL  as follows.

Lambda Deployment

Testing and Debugging – on Live AWS environment

Now press ‘Create an Event’. Copy the URL of the API Gateway endpoint and add a test event to try out our function. The built-in Event Templates allow you to test the Lambda function easily by creating test requests of different types. Let’s use the ‘Hello World’ template for this exercise, and then execute the Lambda function with it. Executing the function should show you the output right below the function on your IDE, and saves you the visit to the CloudWatch log page to search for it. You would see an output as follows. Note that the output states its a ‘[TEST]’ execution and the output from the function and the event we logged to the console are shown, along with the execution time and the memory consumption, etc.

Lambda function output log

Execution via API Gateway

Now lets invoke the function externally through the API Gateway using ‘curl’ as follows.

curl -sD - -d '{"key1":"value1","key2":"value2","key3":"value3"}' https://jqw54d34o3.execute-api.us-east-1.amazonaws.com/dev/01-basic-lambda
HTTP/2 200
content-type: application/json
...<additional headers removed for clarity>...

{"message":"Successfully executed"}

This time the output indicates its a ‘[PROD]’ type execution and the invocation comes externally from your computer through the API Gateway and then to the Lambda function.

API Gateway HTTP Endpoints

API Gateway HTTP Endpoints first became generally available in 2020 and is a significantly faster and lower cost option than REST APIs. AWS recommends the use of HTTP APIs for all use cases, for which the HTTP API features meet the requirements, and REST APIs may be migrated to HTTP APIs in the future.

To create an HTTP API you would need to select that from the drop-down after dragging an API Gateway into your Lambda function handler definition line of the Sigma IDE. Here you need to define the ‘Route’ where this API will receive requests.

HTTP API definition

The Lambda function code could the same as in our first example, and you could invoke this API as follows. Continue to the next post on Lambda Proxy Integration to learn more about the format of the ‘event’ object passed into the Lambda functions when using HTTP API endpoints.

curl -sD - -d '{ "name":"John", "age":31, "city":"New York" }' 'https://y07wvgwxe0.execute-api.us-east-1.amazonaws.com/04-basic-http-api'

Next Steps

In our next post, we will talk about Lambda Proxy Integration and why it might be used.

Continue to the next article ‘What is Lambda Proxy Integration

Note: This article is a significant update to the article with the same name written over 2 years ago and published in Medium