Tutorial: Create a REST API with a private integration
In this tutorial, you create a REST API that connects to an Amazon ECS service that runs in an Amazon VPC. Clients outside of your Amazon VPC can use the API to access your Amazon ECS service.
This tutorial takes approximately an hour to complete. First, you use an CloudFormation template to create a Amazon VPC and Amazon ECS service. Then you use the API Gateway console to create a VPC link V2. The VPC link allows API Gateway to access the Amazon ECS service that runs in your Amazon VPC. Next, you create a REST API that uses the VPC link V2 to connect to your Amazon ECS service. Lastly, you test your API.
When you invoke your REST API, API Gateway routes the request to your Amazon ECS service through your VPC link V2, and then returns the response from the service.
Note
This tutorial was previously supported for HTTP APIs, and now is supported for REST APIs using VPC link V2.
To complete this tutorial, 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 an Amazon ECS service
Amazon ECS is a container management service that makes it easy to run, stop, and manage Docker containers on a cluster. In this tutorial, you run your cluster on a serverless infrastructure that's managed by Amazon ECS.
Download and unzip this CloudFormation template, which creates all of the dependencies for the service, including an Amazon VPC. You use the template to create an Amazon ECS service that uses an Application Load Balancer.
To create an CloudFormation stack
Open the CloudFormation console at https://console.aws.amazon.com/cloudformation
. -
Choose Create stack and then choose With new resources (standard).
-
For Specify template, choose Upload a template file.
-
Select the template that you downloaded.
-
Choose Next.
-
For Stack name, enter
rest-api-private-integrations-tutorialand then choose Next. -
For Configure stack options, choose Next.
-
For Capabilities, acknowledge that CloudFormation can create IAM resources in your account.
-
Choose Next, and then choose Submit.
CloudFormation provisions the ECS service, which can take a few minutes. When the status of your CloudFormation stack is CREATE_COMPLETE, you're ready to move on to the next step.
Step 2: Create a VPC link
A VPC link allows API Gateway to access private resources in an Amazon VPC. You use a VPC link to allow clients to access your Amazon ECS service through your REST API.
To create a VPC link
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
On the main navigation pane, choose VPC links and then choose Create.
You might need to choose the menu icon to open the main navigation pane.
-
For Choose a VPC link version, select VPC link V2.
-
For Name, enter
private-integrations-tutorial. -
For VPC, choose the VPC that you created in step 1. The name should start with RestApiStack.
-
For Subnets, select the two private subnets in your VPC. Their names end with
PrivateSubnet. -
For Security groups, select the Group ID that starts with
private-integrations-tutorialand has the description ofRestApiStack/RestApiTutorialService/Service/SecurityGroup. -
Choose Create.
After you create your VPC link V2, API Gateway provisions Elastic Network Interfaces to access your VPC. The process can take a few minutes. In the meantime, you can create your API.
Step 3: Create a REST API
The REST API provides an HTTP endpoint for your Amazon ECS service.
To create a REST API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
Choose Create API, and then for REST API, choose Build.
-
For Name, enter
private-integration-api. -
For IP address type, select IPv4.
Choose Create API.
After you create your API, you create a method.
-
Choose Create method, and then do the following:
For Method type, select
GET.For Integration type, select VPC link.
Turn on VPC proxy integration.
For HTTP method, select
GET.-
For VPC link, choose the VPC link V2 you created in the previous step.
-
For Integration target, enter the load balancer that you created with the CloudFormation template in Step 1. It's name should start with rest-.
-
For Endpoint URL, enter
http://private-integrations-tutorial.com.The URL is used to set the
Hostheader of the integration request. In this case, the host header isprivate-integrations-tutorial. Choose Create method.
With the proxy integration, the API is ready to test.
Step 4: Test your API
Next, you test invoking the API method.
To test your API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
Choose your API.
Choose the Test tab. You might need to choose the right arrow button to show the tab.
Choose Test
Verify that your API's response is a welcome message that tells you that your app is running on Amazon ECS.
Step 5: Deploy your API
Next, you deploy your API.
To deploy your API
Choose Deploy API.
For Stage, select New stage.
For Stage name, enter
Prod.(Optional) For Description, enter a description.
Choose Deploy.
Step 6: Call your API
After your API is deployed, you can call it.
To call your API
-
Enter the invoke URL in a web browser.
The full URL should look like
https://.abcd123.execute-api.us-east-2.amazonaws.com/ProdYour browser sends a
GETrequest to the API. -
Verify that your API's response is a welcome message that tells you that your app is running on Amazon ECS.
If you see the welcome message, you successfully created an Amazon ECS service that runs in an Amazon VPC, and you used an API Gateway REST API with a VPC link V2 to access the Amazon ECS service.
Step 7: Clean up
To prevent unnecessary costs, delete the resources that you created as part of this tutorial. The following steps delete your VPC link V2, CloudFormation stack, and REST API.
To delete a REST API
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
On the APIs page, select an API. Choose Actions, choose Delete, and then confirm your choice.
To delete a VPC link
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway
. -
Choose VPC link.
-
Select your VPC link, choose Delete, and then confirm your choice.
To delete an CloudFormation stack
Open the CloudFormation console at https://console.aws.amazon.com/cloudformation
. -
Select your CloudFormation stack.
-
Choose Delete and then confirm your choice.