Vue The Series — Chapter 2.1: Deploy on Amazon EC2
Hola!! Quite fast from Chapter 1, Now you have the image that you can run anywhere with Docker.
This chapter we will deploy our application to Amazon EC2 ☁
Amazon EC2
Amazon Elastic Compute Cloud (Amazon EC2) is a cloud computing that provides scalable computing capacity in AWS. For more details click here.
Let’s get started!
You have to create an AWS account then sign-in to your console.
On Your console you can see all services that AWS provides for us, Now we choose EC2 to deploy the application.
Create your EC2 instance
Choose your AMI (Amazon Machine Image) that will be a template of your instance. It looks like you have a physical computer in front of you and then chooses the operating system that you like such as Windows Server or Linux.
I choose Amazon Linux 2 AMI (HVM), SSD Volume Type 64-bit (x86), I think Linux is easier to remote control over the internet such as SSH.
Choose as Instance Type
I choose t2.micro (Variable ECUs, 1 vCPUs, 2.5 GHz, Intel Xeon Family, 1 GiB memory, EBS only) because it’s free 😂, We don’t have to choose faster for a pretty simple app except you’re rich 💸 then click Review and Launch
In the final step, you see all the details of your instance then click Launch.
As you see, you have to use key pair for secure SSH to your instance (We don’t type a password when we want to access our instance).
Create a new one then download the private key file (*.pem file) and store in your machine like ~/.ssh/aws not ~/Downloads 😂
Then Launch your instance
Here we are your instance, you will see details of your instance such as
Public DNS (IPv4), IPv4 Public IP, etc.
How we deploy our application to this instance ???
Yes! We use SSH to remote control your instance,
It’s like control machine, download docker, download an image and run container that expose port 80 that we can access from internet
Access your instance by SSH
ssh -i {pem_file} ec2-user@{public_dns}
{pem_file}: Where your pem file stored.
{public_dns}: IP of your instance (in the description of your instance)
for my connection
ssh -i ~/.ssh/aws/poseidon-key.pem ec2-user@ec2-54-254-179-35.ap-southeast-1.compute.amazonaws.com
and yes to confirm the connection with ECDSA key fingerprint
If you get this error, you have to give permission to access this key and try again.
chmod 600 ~/.ssh/aws/poseidon-key.pem
Gotcha! we can access to our instance
then update packages
sudo yum update
install Docker withamazon-linux-extras
sudo amazon-linux-extras install docker
Start Docker service
sudo service docker start
let our ec2 user execute docker command without using sudo
sudo usermod -a -G docker ec2-user
try with
docker info
Here we go! Next how we can move our image to this instance???
Jumping back to the image that we created, I will upload this image to the repository.
Let me ask you, Where did we stored code on the internet?
Yes! someone might say GitHub or Gitlab and image has Docker Hub that stored docker image too.
docker ps
You have to login Docker on your machine if you not have an account click here.
docker login
Then tag your image and push
Tag or Target image is used to refer to source image.
docker tag {image_id} {docker_hub_account}/{name_of_repository}
It will automatically create a repository that stored images
Our image is stored on DockerHub 🚀
Jumping to our EC2, What do we do next?
Yes, download this image from DockerHub
docker run -d -p 80:80 {repository_name}
try to call our service
curl localhost:80
It’s done?
Not yet, our internal network can call port 80 that mapping to port 80 (NGINX provide Http server) but can’t access from the internet.
Go to EC2 Dashboard and setting Security Groups: Inbound rules
as you see we only have port 22 that we provide for SSH.
Let create a new security group that provides port 22 (SSH) and 80 for (HTTP)
Create and change our instance security
and open IPv4 Public IP from your browser
🍻Yeah!! Our app is deployed on EC2 and can access anywhere with your browser!
Next chapter we will deploy by AWS Elastic Beanstalk that is a more easier way to deploy your container on EC2
Thank you, and see next 🚀
Thank for review: Bhuridech Sudsee