DEV Community

Akbar Nafisa
Akbar Nafisa

Posted on • Edited on

Deploy Server App to EC2

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.

Image description

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.

Image description

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.

Image description

After that, click the Connect button again.

Image description

The console instance terminal will then be shown.

Image description

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
Enter fullscreen mode Exit fullscreen mode

If you are on Windows, you can run this command instead.

icacls my-key-pair.pem /inheritance:r /grant:r "$($env:USERNAME):(R)"
Enter fullscreen mode Exit fullscreen mode

Then we need to get the public IP of the instance.

Image description

Finally, run this command to connect to the instance:

ssh -i my-key-pair.pem ubuntu@3.27.113.187
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Create Github SSH

For this part, we will use SSH to clone our repository from GitHub. First, enter the .ssh folder:

cd ~/.ssh/

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Copy the text inside .pub

cat ./my_ssh.pub
Enter fullscreen mode Exit fullscreen mode

and add it to your GitHub SSH keys.

Image description

To test the key, run this command:

eval $(ssh-agent)
ssh-add ./my_ssh
Enter fullscreen mode Exit fullscreen mode

Image description

And test the connection:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Image description

If we've done this part correctly, we should see that the SSH is being used.

Image description

Clone Repository

To copy the repository, run this command:

git clone git@github.com:akbarnafisa/aws-devops-fullstack.git
Enter fullscreen mode Exit fullscreen mode

Then switch to the branch that you want to deploy:

git checkout remotes/origin/6-setup-docker-production-mode
Enter fullscreen mode Exit fullscreen mode

Finally, run our Docker Compose file using this command:

docker compose -f docker-compose.prod.yml
Enter fullscreen mode Exit fullscreen mode

If everything is successful, we will be able to access our app at this URL.

http://<public_ip>:3000/api/todo
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)