AWS Elastic Beanstalk is one of the most used PaaS today, it allows you to deploy your application without provisioning the underlying infrastructure while maintaining the high availability of your application. However, it’s painful to use due to the lack of documentation and real-world scenarios. In this post, I will walk you through how to use Elastic Beanstalk to deploy Docker containers from scratch. Followed by how to automate your deployment process with a Continuous Integration pipeline. At the end of this post, you should be familiar with advanced topics like debugging and monitoring of your applications in EB.
1 – Environment Setup
To get started, create a new Application using the following AWS CLI command:
Next, we will create a Dockerfile to build the Docker image. Go is a compiled language, therefore we can use the Docker multi-stage feature to build a lightweight Docker image:
1 2 3 4 5 6 7 8 9 10 11 12 13
FROM golang:1.10 as builder WORKDIR /go/src/github.com/mlabouardy/docker-eb-ci-mon COPY main.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
The pipeline will firstly prepare the environment, installing the AWS CLI. Then run unit tests. Next, a Docker image will be built, then pushed to DockerHub. Last step is creating a new application bundle and deploying the bundle to Elastic Beanstalk.
In order to grant Circle CI permissions to call AWS operations, we need to create a new IAM user with following IAM policy:
Generate AWS access & secret keys. Then, head back to Circle CI and click on the project settings and paste the credentials :
Now, everytime you push a change to your code repository, a build will be triggered:
And a new version will be deployed automatically to Elastic Beanstalk:
3 – Monitoring
Monitoring your applications is mandatory. Unfortunately, CloudWatch doesn’t expose useful metrics like Memory usage of your applications in Elastic Beanstalk. Hence, in this part, we will solve this issue by creating our custom metrics.
I will install a data collector agent on the instance. The agent will collect metrics and push them to a time-series database.
To install the agent, we will use .ebextensions folder, on which we will create 3 configuration files:
01-install-telegraf.config: install Telegraf on the instance
Once the application version is deployed to Elastic Beanstalk, metrics will be pushed to your timeseries database. In this example, I used InfluxDB as data storage and I created some dynamic Dashboards in Grafana to visualize metrics in real-time:
Containers:
Hosts:
Note: for in-depth explaination on how to configure Telegraf, InfluxDB & Grafana read my previous article.
Full code can be found on my GitHub. Make sure to drop your comments, feedback, or suggestions below — or connect with me directly on Twitter @mlabouardy
In this quick tutorial, I will show you how to install Docker 🐋 on AWS EC2 instance and run your first Docker container.
1 – Setup EC2 instance
I already did a tutorial on how to create an EC2 instance, so I won’t repeat it. There are few ways you’ll want to differ from the tutorial:
We select the “Amazon Linux AMI 2017.03.1 (HVM), SSH Volume Type” as AMI. The exact versions may change with time. We configure the security groups as below. This setting allows access to port 80 (HTTP) from anywhere, and SSH access also.
Go ahead and launch the instance, it will take couple of minutes:
2 – Install Docker
Once your instance is ready to use, connect via SSH to the server using the public DNS and the public key:
Once connected, use yum configuration manager to install Docker, by typing the following commands:
1 2
sudo yum update -y sudo yum install -y docker
Next, start the docker service:
In order to user docker command without root privileges (sudo), we need to add ec2-user to the docker group:
1
sudo usermod -aG docker ec2-user
To verify that docker is correctly installed, just type:
As you can see the latest version of docker has been installed (v17.03.1-ce)
Congratulation ! 💫 🎉 you have now an EC2 instance with Docker installed.
3 – Deploy Docker Container
It’s time to run your first container 😁. We will create an nginx container with this command:
If we run the list command “docker ps”, we can see that a nginx container has been created from the nginx official image.
Finally, you visit your instance public DNS name in your browser, you should see something like this below:
Drop your comments, feedback, or suggestions below — or connect with me directly on Twitter @mlabouardy.