Microservices with AWS Cloud9 and Amazon Lightsail

Microservices are the foundation of many modern applications. Cloud9 simplifies creating and deploying microservices in Lightsail containers.

AWS Admin
Amazon Employee
Published Oct 25, 2023
Last Modified Jun 21, 2024
Microservices are the basis of a service oriented architecture pattern that has been in use for over a decade. It’s a proven way for building agile applications that are easy to deploy, upgrade, and scale flexibly. A microservice solves a specific problem with self-contained code which lends to deploying them in containers. AWS Lightsail is an simple and quick way to build microservices with containers. Lightsail supplies an HTTPS endpoint and automatically sets up a load balanced TLS endpoint with a TLS certificate. In addition., it can replace unresponsive containers automatically and assign a DNS name to the endpoint. When updating the container, Lightsail maintains the old version until the new version is live and healthy. The Lightsail console includes AWS CloudShell, a complete environment for building containers and deploying them in Lightsail. This article demonstrates how to build a microservice and deploy it using Lightsail’s container service.

AWS Cloud9

A common way to build and containerize a microservice is to use a desktop tool such as Docker to build a container before pushing it to a registry or running it in a cloud service. AWS Cloud9 is a cloud-base integrated development environment (IDE) that is prepackaged with popular programming languages and tools such as Docker. The included AWS CLI provides access to AWS services including Lightsail.
To get started, open the Cloud9 console by using the search bar.
Open the Cloud9 console
Choose Create environment.
Create a Cloud9 environment
Name the Cloud9 environment. Optionally provide a Description and set the Timeout duration.
Name the environment and set the Timeout
Choose Create at the bottom of the page.
Choose Create to start the Cloud9 environment
In the Environments page, choose Open.
Open the environment
The Cloud9 environment will open with a text editor window on the top, and a terminal on the bottom.
The Cloud9 IDE displaying the editor and terminal

Setting Up the Environment

To use Lightsail with Cloud9 we will to install two tools. The first is the lightsailctl plugin for deploying containers and the second is the jq utility for parsing JSON data from the container service.
Install lightsailctl plugin.
Install jq.

Create an AWS Lightsail Container Service

A container is a standard unit of software that packages code and dependencies together so the application runs reliably from one computing environment to another. Containers are ideal for microservices because all the code and dependencies are encapsulated in the container. The Lightsail container service is used to store and deploy containers. Use the create-container-service command to create a container service for the microservice.
Check the status of the container service.

Build and Push the Container

The microservice is a REST service that returns dad jokes. It is a NodeJS application built with Express. Start by creating a directory for the project.
Open a new file with the the Cloud9 text editor. Copy the express application below into the text editor window and save it as app.js.
Next, configure the project with npm init and accept the default options. Next, install express.js.
To build a container, open a new file in the text editor, copy the Dockerfile below, and save it as Dockerfile.
Build the container with Docker. After the container is built, list the containers.
Push the container to the Lightsail container service created earlier. The Lightsail container includes a registry for storing images.
Note the image name. We will use the image name to configure the deployment
Refer to this image as :microservice.dadjoke.x in deployments

Deploy the Microservice

Deploying a container requires a JSON file that defines the configuration parameters. The AWS Location Service requires AWS credentials and is available in the us-east-1 region. We also include the image name that we pushed into the container service and the container's exposed port. The configuration file requires an access_key and secret access key. Open a new file in the text editor and copy the JSON file below, add your credentials, and save the file as container.json.
To make the service accessible, we define an endpoint with a JSON file. Open a file in the text editor, copy the JSON file, and save it as endpoint.json.
Use the create-container-service-deployment command to deploy the microservice.
Get the public URL for the geocoder.
Try out the microservice.

Summary

With Cloud9, you can immediately build and deploy applications without the overhead of configuring a local development environment. The tools for creating and managing AWS services are included in Cloud9 and specialized tools can be installed as needed. The integrated development environment simplifies coding and creating configuration files. This example uses the AWS Lightsail container service. The container service includes a container registry for storing container images and automated deployment of containers. The advantage of using the AWS CLI and Lightsail plugin is that it can be scripted and run as a devops workflow for consistent and secure updates.
Check out these articles to learn more about Lightsail container services.

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments