Deploy Server App to EC2
Tags: webdev, javascript, aws, devops
Published: 2023-12-20 00:00:00

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np4mghbb0uhejp3mji0m.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jxpb708dklp61yxff28.png)

# **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4gjz623o1j8rds5z6t3.png)

After that, click the **`Connect`** button again.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brz160v8r1lrbinwdmwn.png)

The console instance terminal will then be shown.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anmf1tioe1ceejxvxlnm.png)

# **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:

```bash
chmod 400 file_name.pem
```

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

```bash
icacls my-key-pair.pem /inheritance:r /grant:r "$($env:USERNAME):(R)"
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4co7xtso2f9dnpejlsdr.png)

Finally, run this command to connect to the instance:

```bash
ssh -i my-key-pair.pem ubuntu@3.27.113.187
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89buyfhd1vcq13cq53aj.png)

# **Install Docker**

The Docker installation guide on Ubuntu can be found [here](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository). After installation finishes, we need to run this command to make the docker compose command work.

```bash
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:

```powershell
cd ~/.ssh/

```

Then generate the **`.pub`** file. You can name the file whatever you want; for this file, I named it **`my_ssh`**

```powershell
ssh-keygen -t rsa
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27cgbvw3fauri7ldh82u.png)



Copy the text inside **`.pub`**

```powershell
cat ./my_ssh.pub
```

and add it to your [GitHub SSH keys](https://github.com/settings/keys).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzypqxcdloqb2huje2c6.png)



To test the key, run this command:

```bash
eval $(ssh-agent)
ssh-add ./my_ssh
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1713ynunmtceho97jrj.png)



And test the connection:

```bash
ssh -T git@github.com
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xs8effmzbyq9txjqygaz.png)



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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7fw09spkxlp26ejsqbh.png)

# **Clone Repository**

To copy the repository, run this command:

```bash
git clone git@github.com:akbarnafisa/aws-devops-fullstack.git
```

Then switch to the branch that you want to deploy:

```bash
git checkout remotes/origin/6-setup-docker-production-mode
```

Finally, run our Docker Compose file using this command:

```powershell
docker compose -f docker-compose.prod.yml
```

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

```powershell
http://<public_ip>:3000/api/todo
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azqj68mvwwceokrq659y.png)