This is the part where we deploy our Server App to EC2. Here is how we do it:
Add New Rules in Security Group
We need to add new inbound rules to expose our app in Docker. For the server app, we set the port to be 3000
, so we need to add it here. You can add the rules inside security groups. Inside security groups page, click on Edit inbound rules
.
Next, add Custom TCP
with the 3000
as the port range and set the source to 0.0.0.0/0
, then click Save rules
button to finish.
Connect to EC2 Instance
This can be done by connecting to the instance in the AWS EC2 console or by using SSH.
Connect to EC2 Instance in AWS Console
In your EC2 dashboard, select the instance that you want to connect to, then click the Connect
button.
After that, click the Connect
button again.
The console instance terminal will then be shown.
Connect to EC2 Instance by SSH
To connect to the instance by SSH, we need the .pem
file we downloaded when we created the instance. The first step is to set the .pem
file permissions to read-only for the owner and no permissions for others. To do this, we can run this command:
chmod 400 file_name.pem
If you are on Windows, you can run this command instead.
icacls my-key-pair.pem /inheritance:r /grant:r "$($env:USERNAME):(R)"
Then we need to get the public IP of the instance.
Finally, run this command to connect to the instance:
ssh -i my-key-pair.pem ubuntu@3.27.113.187
Install Docker
The Docker installation guide on Ubuntu can be found here. After installation finishes, we need to run this command to make the docker compose command work.
sudo chmod 666 /var/run/docker.sock
Create Github SSH
For this part, we will use SSH to clone our repository from GitHub. First, enter the .ssh
folder:
cd ~/.ssh/
Then generate the .pub
file. You can name the file whatever you want; for this file, I named it my_ssh
ssh-keygen -t rsa
Copy the text inside .pub
cat ./my_ssh.pub
and add it to your GitHub SSH keys.
To test the key, run this command:
eval $(ssh-agent)
ssh-add ./my_ssh
And test the connection:
ssh -T git@github.com
If we've done this part correctly, we should see that the SSH is being used.
Clone Repository
To copy the repository, run this command:
git clone git@github.com:akbarnafisa/aws-devops-fullstack.git
Then switch to the branch that you want to deploy:
git checkout remotes/origin/6-setup-docker-production-mode
Finally, run our Docker Compose file using this command:
docker compose -f docker-compose.prod.yml
If everything is successful, we will be able to access our app at this URL.
http://<public_ip>:3000/api/todo
Top comments (0)