Get started with API Gateway
In this getting started exercise, you create a serverless API. Serverless APIs let you focus on your
applications, instead of spending time provisioning and managing servers. This exercise takes less than 20 minutes
to complete, and is possible within the AWS Free Tier
First, you create a Lambda function using the AWS Lambda console. Next, you create an HTTP API using the API Gateway console. Then, you invoke your API.
Note
This exercise uses an HTTP API. API Gateway also supports REST APIs, which include more features. For a tutorial using a REST API, see Get started with the REST API console.
For more information about the difference between HTTP APIs and REST APIs, see Choose between REST APIs and HTTP APIs.
When you invoke your HTTP API, API Gateway routes the request to your Lambda function. Lambda runs the Lambda function and returns a response to API Gateway. API Gateway then returns a response to you.
To complete this exercise, you need an AWS account and an AWS Identity and Access Management user with console access. For more information, see Set up to use API Gateway.
Topics
Step 1: Create a Lambda function
You use a Lambda function for the backend of your API. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second.
For this example, you use the default Node.js function from the Lambda console.
To create a Lambda function
Sign in to the Lambda console at https://console.aws.amazon.com/lambda
. -
Choose Create function.
-
For Function name, enter
my-function
. For all other options, use the default setting.
-
Choose Create function.
The example function returns a 200
response to clients, and the text Hello from
Lambda!
.
You can modify your Lambda function, as long as the function's response aligns with the format that API Gateway requires.
The default Lambda function code should look similar to the following:
export const handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; };
Step 2: Create an HTTP API
Next, you create an HTTP API. API Gateway also supports REST APIs and WebSocket APIs, but an HTTP API is the best choice for this exercise. REST APIs support more features than HTTP APIs, but we don't need those features for this exercise. HTTP APIs are designed with minimal features so that they can be offered at a lower price. WebSocket APIs maintain persistent connections with clients for full-duplex communication, which isn't required for this example.
The HTTP API provides an HTTP endpoint for your Lambda function. API Gateway routes requests to your Lambda function, and then returns the function's response to clients.
To create an HTTP API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
Do one of the following:
-
To create your first API, for HTTP API, choose Build.
-
If you've created an API before, choose Create API, and then choose Build for HTTP API.
-
-
For Integrations, choose Add integration.
-
Choose Lambda.
-
For Lambda function, enter
my-function
. -
For API name, enter
my-http-api
. -
Choose Next.
-
Review the route that API Gateway creates for you, and then choose Next.
-
Review the stage that API Gateway creates for you, and then choose Next.
-
Choose Create.
Now you've created an HTTP API with a Lambda integration that's ready to receive requests from clients.
Step 3: Test your API
Next, you test your API to make sure that it's working. For simplicity, use a web browser to invoke your API.
To test your API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. Choose your API.
-
Note your API's invoke URL.
-
Copy your API's invoke URL, and enter it in a web browser. Append the name of your Lambda function to your invoke URL to call your Lambda function. By default, the API Gateway console creates a route with the same name as your Lambda function,
my-function
.The full URL should look like
https://
.abcdef123
.execute-api.us-east-2
.amazonaws.com/my-function
Your browser sends a
GET
request to the API. -
Verify your API's response. You should see the text
"Hello from Lambda!"
in your browser.
(Optional) Step 4: Clean up
To prevent unnecessary costs, delete the resources that you created as part of this getting started exercise. The following steps delete your HTTP API, your Lambda function, and associated resources.
To delete an HTTP API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
On the APIs page, select an API. Choose Actions, and then choose Delete.
-
Choose Delete.
To delete a Lambda function
Sign in to the Lambda console at https://console.aws.amazon.com/lambda
. -
On the Functions page, select a function. Choose Actions, and then choose Delete.
-
Choose Delete.
To delete a Lambda function's log group
-
In the Amazon CloudWatch console, open the Log groups page
. -
On the Log groups page, select the function's log group (
/aws/lambda/my-function
). Choose Actions, and then choose Delete log group. -
Choose Delete.
To delete a Lambda function's execution role
-
In the AWS Identity and Access Management console, open the Roles page
. -
Select the function's role, for example,
my-function-
.31exxmpl
-
Choose Delete role.
-
Choose Yes, delete.
You can automate the creation and cleanup of AWS resources by using AWS CloudFormation or AWS SAM. For example AWS CloudFormation
templates, see example
AWS CloudFormation templates
Next steps
For this example, you used the AWS Management Console to create a simple HTTP API. The HTTP API invokes a Lambda function and returns a response to clients.
The following are next steps as you continue to work with API Gateway.
To get help with Amazon API Gateway from the community, see the
API Gateway Discussion
Forum
To get help with API Gateway directly from AWS, see the support options on the AWS Support page
See also our frequently asked questions (FAQs)