In a prior blog post we looked at how to deploy API Builder containers on Google Cloud Run. In this blog post we will see how we can easily deploy an API Builder container using AWS Fargate.
For this blog post, you will need familiarity and access to the following:
You will also need a working and tested API Builder project.
The basic steps are:
- Upload API Builder container image to Elastic Container Registry (ECR)
- Create a service in Fargate/ECS
We will describe the steps from start to finish, and when we are done we will have an API deployed in AWS Fargate that can be publicly accessed over the internet. Most of the work involved is in setting up CLI’s (which is a one time task) and getting an API Builder Docker image into the Elastic Container Registry. Creating the Fargate/ECS service is actually quite quick and easy.
Create Repository in ECR
Start at the ECR repositories page:
- Click on Create Repository and provide a name (e.g. watchlist) and set repository to public.
Note that I will use the API Builder Stock Watchlist API described here.
- Leave other values at their default and fill in any of the optional info as needed (I left blank) and click Create Repository
- Click on the newly created repository
- Click on the View Push Commands button to get a list of commands to create and push the API Builder container image to ECR
Push Container Image to ECR
- Follow instructions here to install/update the AWS CLI. I used the following command on my mac:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
- In your API Builder Project folder, execute the following commands from the ECR image page:
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/w3u3w3k3
docker build -t watchlist .
docker tag watchlist:latest public.ecr.aws/w3u3w3k3/watchlist:latest
docker push public.ecr.aws/w3u3w3k3/watchlist:latest
When the docker push command is complete you should see an image in your ECR repository that you just created:
- Copy the URI (e.g.
public.ecr.aws/w3u3w3k3/watchlist
) as we’ll need it in the next step
Create Fargate Service
- Go to the AWS ECS page
Click Get Started
- Click on the Configure button in the custom Container definition section
- Set the name (e.g. watchlist), registry URI from above (e.g.
public.ecr.aws/w3u3w3k3/watchlist
), and the Port mapping to 8080 (API Builder listens on port 8080 by default)
- Expand the Advanced container configuration section and enter any environment variables for your API Builder project (mine has two:
YAHOO_FINANCE_APIKEY
andAPI_KEY
)
- Leave everything else blank and click Update
- Click Next
- We’ll look at using a Load Balancer in a future post but for now leave Load Balancer set to None and click Next
- Click Next
- Review the details and click on Create
- After a few minutes the Service will be fully created
- Click on View Service
- Click on Tasks and make sure your task is in the RUNNING state as this is the first indication that things are working properly
- Click on the running task and note the Public IP address as we’ll need that for testing our API
- Scroll to the bottom and expand the container (e.g. watchlist) and scroll down to the Log Configuration section
- Click on View logs in CloudWatch and see that your API Builder project is running and waiting for API calls. This is the second indication that things are working properly
- Get your Public IP address from above and test API using the curl command below (or whatever is appropriate for your API Builder project):
curl --location --request GET 'http://3.93.25.230:8080/api/watchlist?stocklist=aapl,txn,intc,amzn,t' --header 'accept: application/json' --header 'apikey: 123456789'
The response for the watchlist API should be:
[
{
"symbol": "AAPL",
"lastPrice": 137.89,
"change": -4.75
},
{
"symbol": "TXN",
"lastPrice": 158.33,
"change": -3.25
},
{
"symbol": "INTC",
"lastPrice": 39.275,
"change": -0.7349968
},
{
"symbol": "AMZN",
"lastPrice": 109.79,
"change": -6.3600006
},
{
"symbol": "T",
"lastPrice": 20.755,
"change": -0.125
}
]
Note that the API will be accessible via http on port 8080. We’ll look at how to modify the security settings so that the API will be accessible via https in a future blog post.
Summary
AWS Fargate offers a simple way to deploy API Builder Docker images. In this blog post we worked through the steps required to publish your API Builder API to AWS Fargate and test it.
Need help with API Builder? Extend your skills with Axway University.