Build Efficient CI/CD Pipelines for Connected Microservices in Under an Hour Using AWS Copilot
Here's how to build CI/CD pipelines for connected microservices using AWS Copilot. This should accelerate deploying and hosting container-based service on the AWS Cloud using Amazon ECS.
1. Download copy of sample application
2. Navigate into the backend folder
3. Initialize an application using copilot
4. Set up a development environment
5. Set up required infrastructure in the development environment
1. Initialize the backend service
4. Set up the CI/CD pipeline for backend
1. Initialize the frontend service
2. Initialize the frontend service
4. Deploy the frontend service
- Facilitating faster software release
- Enabling the creation of smaller teams to develop and deliver a single service
- Allowing the team to maintain a smaller codebase, reducing complexity
- Allowing for faster build and deployment processes with a smaller codebase
- Allowing for the freedom to write code independently and differently from all other services (using different programming languages, libraries, approaches, etc)
About | |
---|---|
β AWS experience | 200 - Intermediate |
β± Time to complete | 60 minutes |
π° Cost to complete | Free tier eligible |
𧩠Prerequisites | - AWS Account |
π» Code Sample | Code sample used in tutorial on GitHub |
π’ Feedback | Any feedback, issues, or just a π / π ? |
β° Last Updated | 2023-04-26 |
- An active AWS Account
- AWS CLI installed and configured to interact with your account
- Git client installed
tree -d
):backend
folder on your terminal. If not, run the below command to change directory:copilot app init
, Copilot creates an IAM role using CloudFormation to manage services and jobs. Copilot also registers the /copilot/applications/todo
parameter to the Systems Manager Parameter Store. If you run copilot app ls
, for example, Copilot would check the Parameter Store and notify you of all applications in the AWS Region.copilot
directory. This directory saves the manifest files.development
. Run a copilot env init
command to create a development environment. Follow the interactive terminal and provide the inputs. You will be asked a few questions, and you should provide the following answers:- What is your environment's name? Enter
development
. - Which credentials would you like to use to create development? Choose the profile for your development account and press Enter.
- Would you like to use the default configuration for a new environment? Select
Yes, use default.
and press Enter.
copilot/environments/development/manifest.yml
. This manifest file contains the configuration for the environment that Copilot set up.copilot env ls
command and check if the development environment has been created. You should see an output like this:copilot env deploy
command, it should only prompt you for inputs if you've used Copilot before and have set up some applications in the account you specified in the initialize command. If you only set up a single environment, it will default to using it. It will now proceed to create the VPC, Public Subnets, Private Subnets, Route53 Private HostedZone for Service discovery, custom route table, Security Group for containers to talk to each other, and an ECS Cluster to group the ECS services. When the environment creation is complete, you will see this output:backend
folder inside the mono-repo, create the DynamoDB table it requires, and then move it to a new repo. Once the code is committed to the new repo, we will set up a CI/CD pipeline that will automatically build and deploy any future changes we make to it.copilot svc init
command to create a manifest file which defines your backend service.- Which service type best represents your service's architecture? Select Backend Service and press Enter.
- What do you want to name this service? Enter backend.
- Which Dockerfile would you like to use for backend? Select ./Dockerfile and press Enter.
copilot/backend
directory to save a manifest file. When the backend service creation is complete, you will see this output:copilot svc init
command created a manifest file within the copilot/backend/manifest.yml
file. to ensure we can monitor if our container is healthy, we need to add a healthcheck. This is an endpoint that is periodically accessed to report if the application is healthy. It can run a series of internal checks, or just respond with an HTTP "OK" (HTTP response 200). To add a healthcheck, modify copilot/backend/manifest.yml
, and add the healthcheck
section shown below in the image
section directly below port
:app.py
:copilot storage init
command.copilot storage init
command to create a DynamoDB table to be used by the backend service.- What type of storage would you like to associate with backend? Select DynamoDB and press Enter.
- What would you like to name this DynamoDB Table? Enter todotable.
- Do you want the storage to be created and deleted with the backend service? Select Yes, the storage should be created and deleted at the same time as backend
- What would you like to name the partition key of this DynamoDB? Enter TodoId.
- What datatype is this key? Select Number and press Enter.
- Would you like to add a sort key to this table? Enter n.
copilot storage init
command, Copilot creates a copilot/backend/addons
directory and a CloudFormation template: todotable.yml
in the directory. Copilot uses this template to create additional resources. Once the CloudFormation template has been created, you should see the following output:TODOTABLE_NAME
. If you look at the app.ts
file, the application is already equipped to use this environment variable with the following line of the code:copilot svc deploy
command to deploy the backend service to the development environment automatically. You have not built the Docker image, so it will take some time to build a container image. You can see the progress on the terminal.copilot svc status
to see the service status. If the status is ACTIVE, then your container is running normally.COPILOT_SERVICE_DISCOVERY_ENDPOINT
is a special environment variable that the Copilot CLI sets for you when it creates the service. The format is {env name}.{app name}.local
and requests to /api
are passed to http://backend.development.todo.local:10000/
. The endpoint backend.development.todo.local
resolves to a private IP address and is routed privately within your VPC to the backend service. When configuring the frontend service, you will be using the COPILOT_SERVICE_DISCOVERY_ENDPOINT
environment variable to access the backend service using service discovery. More information about Service Discovery can be found here.automate-container-microservices-aws-copilot
directory, and then run:todobackend
folder should now contain the following:copilot pipeline init
to generate a pipeline manifest - this will create the configuration for the pipeline in the manifest file, but not yet set it up. You will notice the $(aws configure get region)
in the URL, this will get the region that your AWS CLI is set up to use to ensure we create the CodeCommit repository in the same region as the rest of our infrastructure. You will also see an error message between the command you entered and where it is asking for input with the following:master
as the branch name, and we changed it to use main
with the git switch -c main
command, but have yet to commit anything. Copilot will default to using main
since it is the currently selected branch, so you can safely ignore this error. Let's continue with the prompts to set up our CI/CD pipeline. You will be asked a few questions and should provide the following answers:- What would you like to name this pipeline? Type todobackend-main and press Enter.
- What type of continuous delivery pipeline is this? Move to Workloads and press Enter.
- Which environment would you like to add to your pipeline? Move to development and press Enter.
- Which environment would you like to add to your pipeline? Move to No additional environments and press Enter. (If you have never used Copilot, this option will not be shown, and default to the
development
environment)
copilot pipeline deploy
to create it. This command uses the CodeCommit repository that was was added as a remote git repository as a source stage to CodePipeline. Once this command succeeds, you should see the output on the terminal, like this:code/frontend
directory in the automate-container-microservices-aws-copilot
. The folder structure should look like this:copilot app init
to initialize the frontend application. Copilot will detect the previous application and environment we created. You will be asked a few questions and should provide the following answers:- Would you like to use one of your existing applications? (Y/n) Choose Y.
- Which existing application do you want to add a new service or job to? [Use arrows to move, type to filter, ? for more help] Choose todo.
development
environment, and default to using it.copilot svc init
command to create a manifest file that defines it. You will be asked a few questions and should provide the following answers:- Which service type best represents your service's architecture? Select Load Balanced Web Service and press Enter.
- What do you want to name this service? Enter frontend.
- Which Dockerfile would you like to use for backend? Select .Dockerfile and press Enter.
copilot/frontend
. When completed, you should see similar output:copilot svc deploy
to deploy it to the development environment. We have not built a docker image yet, so it will take a bit of time to do so. The output from the command will keep you updated with the progress, and any errors it encountered. Once the deployment is done, you should see the following:copilot svc status
, and select which service you want the status of. To confirm that the container deployed successfully, select the frontend
service, and look for the RUNNING
status. You should see output similar to this:backend.conf.template
. Requests to /api
are passed to backend.${COPILOT_SERVICE_DISCOVERY_ENDPOINT}
. COPILOT_SERVICE_DISCOVERY_ENDPOINT
is a special environment variable that the Copilot CLI sets for you when it creates your service. It uses the format {env name}.{app name}.local
, and requests to /api
are passed to http://backend.development.todo.local:10000/
. The endpoint backend.development.todo.local
resolves to a private IP address and is routed privately within your VPC to the backend service. More information about Service Discovery can be found here.automate-container-microservices-aws-copilot
directory, and run the following commands:todobackend
folder should now contain the following:$(aws configure get region)
to inject the region our AWS CLI is set up to use so it matches the rest of our infrastructure.copilot pipeline init
. You will be asked a few questions and should provide the following answers:- What would you like to name this pipeline? Type todofrontend-main and press Enter.
- What type of continuous delivery pipeline is this? Move to Workloads and press Enter.
- Which environment would you like to add to your pipeline? Move to development and press Enter.
- Which environment would you like to add to your pipeline? Move to No additional environments and press Enter. This will only be asked if you have used Copilot before and created addition environments.
copilot pipeline deploy
to create the pipeline. This command uses the CodeCommit repository that was was added as a remote git repository as the source stage to CodePipeline. Once this command succeeds, you should see this output:Note: Make sure you push the Copilot directory with all its contents.
- In the
todofrontend
runcopilot app delete
and pressy
and enter to confirm. This will delete the backend, frontend, CI/CD pipelines, and thedevelopment
environment. You should see the following output once it completed successfully after a few minutes: - Run the following two commands to delete the CodeCommit repositories created:
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.