From Localhost To Production Using The DevOps Way - Part 2
This post covers:
- The continuation of the first post of this series
- How Continuous Delivery integrates with Continuous Integration
- How to deploy the latest version of our application to AWS using Elastic Beanstalk
Continuous Delivery
In the previous post we focused on the setup of the Continuous Integration (CI) part of our implementation. Meaning that now we can add a new feature to our application and push a new version of it to a container repository within minutes. All of this while relying on an automated process (except for the peer review).
Now we want to be able to send our application from Docker Hub to production in a safe, quick and sustainable way.
Continuous delivery is a software development practice where code changes are automatically prepared for a release to production. A pillar of modern application development, continuous delivery expands upon continuous integration by deploying all code changes to a testing environment and/or a production environment after the build stage.
As a result, developers will always have the next release available. One that went through a code review and that has passed through a standardized testing process.
Gameplan
To do this, we are going to use Amazon Web Services (AWS) as our cloud provider, so make sure you have an AWS account. We are also going to use Elastic Beanstalk (EB). The latter is a Platform-As-A-Service that allows developers to easily leverage on AWS for application deployment and management.
Architecture
Since trendlit has a very simple architecture, using an EC2 instance and Application Load Balancer should be more than enough.
EC2
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.
Application Load Balancer
Application Load Balancer is best suited for load balancing of HTTP and HTTPS traffic and provides advanced request routing targeted at the delivery of modern application architectures, including microservices and containers.
Configure access to AWS
We are going to setup an application and environment using EB. This will be done by using the AWS command line tool. If you are a macOS user install it using homebrew, otherwise you can also use pip:
sudo pip install -U awscli
Setting up AWS in our host machine
Credentials - We need to make sure that we have the credentials file in the following path: $HOME/.aws/credentials
[default]
aws_access_key_id = SECRET
aws_secret_access_key = SECRET
Region - We need to make sure that we have the config file in the following path: $HOME/.aws/config
[default]
region=us-west-2
Create an EB Application
An EB application is like a project folder that will allow us to organize our components.
aws elasticbeanstalk create-application --application-name trendlit --description "trendlit application"
Retrieve the name of the latest Docker EB stack available
Since we are going to run a docker container in our EC2 instance, we can take advantage of the stack that is offered straight out by the box by AWS:
aws elasticbeanstalk list-available-solution-stacks | jq -r '.SolutionStacks[] | select(contains("Docker"))' | head -1
Create an EB environment
An application can have several environments such as development, staging, and production. For this example we will create only one environment inside the application we just created:
aws elasticbeanstalk create-environment \
--application-name trendlit \
--environment-name trendlit-production \
--description "trendlit's production environment" \
--solution-stack-name "64bit Amazon Linux 2018.03 v2.12.11 running Docker 18.06.1-ce" \
--tier "Name=WebServer,Type=Standard,Version=''"
Retrieving the public endpoint
After creating the environment, we can retrieve the public hostname of the EB load balancer:
aws elasticbeanstalk describe-environments --environment-name trendlit-production | jq -r '.Environments[0].CNAME'
This means that we should be able to curl it and if you see “Congratulations” we are golden.
Deploying The Container In Our Environment
Our infrastructure is now set, but we still need run our Docker container in it. We’ll use a JSON config file to configure our running server that specifies the Docker image from Docker Hub.
Uploading the Application Configuration to S3
We need to save the application configuration in our infrastructure so EB can easily retrieve it.
# Create an S3 bucket with a unique identifier
aws s3 mb s3://trendlit-YOURNAME
# Copy the config file into it
aws s3 cp trendlit-latest.json s3://trendlit-YOURNAME/
Assigning the application configuration to the EB environment
aws elasticbeanstalk create-application-version \
--application-name "trendlit" \
--version-label trendlit-latest \
--description "This config will always pull the latest tag from Docker Hub" \
--source-bundle "S3Bucket=trendlit,S3Key=trendlit-latest.json"
Deploy the application configuration
First, get the EnvironmentId:
aws elasticbeanstalk describe-environments --environment-names trendlit-production | jq -r '.Environments[0].EnvironmentId'
Then update the environment:
aws elasticbeanstalk update-environment \
--application-name trendlit \
--environment-id e-12345678 \
--version-label trendlit-latest
After the update completes successfully, your application should be up and running!
Wrapping it up
For now, we have an automated process that tests our application, pushes it to a container repository, and updates the latest version of the application with single command. Although it might seem we finished the proposed implementation, there is still a lot of work that can be done. We must be aware that we neglected a lot of important aspects such as the least privilege principle in order to go from Localhost to Production.
Some things that we could also address in the next release are things like having a rollback strategy. So if there is a problem with a release, we can always select the previous version and roll it back with a single command.
References
- AWS Elastic Beanstalk Survival Guide
- AWS - Continuous Delivery
- What is EC2
- AWS Elastic Load Balancing
- AWS S3 Bucket Definition
Originally published on Medium.