API Builder

Deploy an API Builder container to Amazon Lightsail

Deploy an API Builder Container to Amazon Lightsail

In this blog post, we’ll illustrate how to deploy an API Builder Docker container image to Amazon Lightsail, a powerful virtual cloud server.

We’ll use the API Builder watchlist container image on Docker Hub for this example. This API is described in this blog post.

You’ll need the AWS CLI, installed and working properly, and an Amazon Lightsail (or AWS) account.

Getting started

Log into Lightsail and create a container service.

  • Click on the Container tab

  • Click on the ‘Create container service’ button

Configure service

Now, let’s configure the container service.

  • You can change the Container service location if you’d like. I left it at the default.
  • Set the container service capacity. I chose Na for Power and 1 for scale.

  • For now, skip the Deployment section and give your service a name (e.g. watchlist).

  • Click on the ‘Create container service’ button

It will take a couple of minutes for the service to be ready

Push container images to your Lightsail Account

You can use the Docker CLI to create a docker image of your API Builder project as described here.

We’ll use the Stock Watchlist Docker Hub image in the instructions below.

  • Install the Amazon Lightsail container services plugin using the instructions here
  • Pull API Builder Stock Watchlist image from Dicker Hub using:

docker pull lbrenman/watchlist

  • Follow the instruction here to push to container image to the Lightsail container service using the following command:
aws lightsail push-container-image --region us-west-2 --service-name myservice --label mystaticwebsite --image mystaticwebsite:v2

I used the following command:

aws lightsail push-container-image --region us-east-2 --service-name watchlist --label watchlist --image lbrenman/watchlist
  • Note the CLI response for the image reference to use in your deployment setup:
.
.
.
Digest: sha256:386542fc89d5149a7c19881a183f2c052ddbef798538455b08f172111082fac2
Image "lbrenman/watchlist" registered.
Refer to this image as ":watchlist.watchlist.2" in deployments.

Note that for me, the image name is :watchlist.watchlist.2

  • Click on the Images tab to see your image:

Create your deployment

  • Click on the Deployments tab

  • Click on the ‘Create your first deployment’ link

  • Enter the container name (e.g. watchlist) and image reference (e.g. :watchlist.watchlist.2)

  • Click on the ‘Add environment variable’ button and add any environment variables your image requires

  • Click on the ‘Add open ports’ button and add port 8080, HTTP

  • Select the container to use as your public endpoint (the only container we have) and set the Healthcheck to /apibuilderPing.json as per the API Builder Best Practices guide

  • Click on the ‘Save and deploy’ button

  • It will take about 10 minutes to fully deploy and then be available at the Public Domain listed near the top

  • You can now test your API using Postman or Curl as follows:
curl --location --request GET 'https://watchlist.nu3rimb8ttorq.us-east-2.cs.amazonlightsail.com/api/watchlist?stocklist=aapl,txn,intc,amzn,t' \
--header 'accept: application/json' \
--header 'apikey: 0cded940-51be-4427-a066-bb0e56e3e40f'

with the following response:

[
  {
      "symbol": "AAPL",
      "lastPrice": 168.53,
      "change": 3.6100006
  },
  {
      "symbol": "TXN",
      "lastPrice": 182.17,
      "change": 4.4599915
  },
  {
      "symbol": "INTC",
      "lastPrice": 35.0867,
      "change": 0.5667
  },
  {
      "symbol": "AMZN",
      "lastPrice": 142.29,
      "change": 4.4599915
  },
  {
      "symbol": "T",
      "lastPrice": 18.045,
      "change": -0.055000305
  }
]
  • You can click on the `Open log` button to view the logs:

  • You can click on the ‘Metrics’ tab to view CPU Utilization

Summary

Amazon Lightsail provides a simple means to deploy API Builder Docker Container images. In this blog post we saw detailed instructions for how to do so.

Discover how Amplify API Builder lets you create and deploy perfect APIs in minutes.