Quick Guide for AWS
Tags: aws, ec2, devops, s3
Published: 2023-02-26 07:40:59

## Deploy to EC2

---

### Security Group

Setup the following security group configuration

![Preffered security group for ec2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96any17vdm2gndfn04gi.png)

### Connect with SSH

- Update the permission of downloaded certificate file
```bash
chmod 400 <file_name>.pem
```

- Connect to the distant server with the command
```bash
ssh -i <certificate>.pem ec2-user@<Public_IPv4_DNS>
```

### Install node with nvm

- Download nvm using curl
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
```

- Activate nvm with
```bash
. ~/.nvm/nvm.sh
```

- Install node with
```bash
nvm install node
```

- Test node installation with
```bash
node -e "console.log('Running Node.js ' + process.version)"
```

[Checkout the official documentation on how to setup node in ec2 linux](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)

### Port Forwarding from 80 to 8000

Use the following command for port forwarding
```bash
sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8000
```

### Install MongoDB community edition

- Verify Linux Distribution with
```bash
grep ^NAME  /etc/*release
```
The result should be Amazon Linux or Amazon Linux AMI

- Create a /etc/yum.repos.d/mongodb-org-4.4.repo file so that you can install MongoDB directly using yum:
```bash
cd /etc/yum.repos.d
sudo touch mongodb-org-4.4.repo
```

- Add the following Configuration to created file with nano or vim
```bash
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
```

- Install MongoDB packages with
```bash
sudo yum install -y mongodb-org
```

- Setup storage directory for MongoDb
```bash
cd /
sudo mkdir data
cd data
sudo mkdir db
cd ../..
cd home/ec2-user
```

- Start mongodb service
```bash
sudo service mongod start
```

[Checkout common errors faced during this](https://stackoverflow.com/questions/43368173/why-does-sudo-service-mongod-start-fail-with-linux-ec2)

[Checkout the offficial Documentation](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-amazon/)


### Install PostgreSQL

[Follow this article for installing PostgreSQL](https://www.how2shout.com/linux/install-postgresql-13-on-aws-ec2-amazon-linux-2/)

after install use this command to open db shell
```bash
sudo su - postgres
```

### Transfer node project to ec2

You can use programs like filezilla to transfer project files the method I use personally is to send zipped file using scp command

- Delete node_modules directory from project
- Zip the project directory
```bash
zip -r server.zip <project_directory>
```
- Transfer zipped file using scp
```bash
scp -i <certificate>.pem server.zip ec2-user@<Public_IPv4_DNS>:
```
- Now connect back to instance with SSH and unzip the file
```bash
unzip server.zip
```

### Run Application with screen

install the node modules and run the app with screen 

```bash
screen npm start
```
you can detach the screen without terminating by

i. ctrl+A
ii. D

---

## Bucket Policy for S3

---

Use the following bucket policy in S3 Bucket for public read access

```json
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<bucket_name>/*"
        }
    ]
}
```

---

## CloudFront Guide

---

Use this tutorial for setting up cloudfront

{% embed https://www.youtube.com/embed/uNWHAy5TTx8 %}


---

## Steps to get SSL Certificate to EC2 instance

---

1. move nameserver to Route 53
2. request certificate from ACM
3. create load balancer targeting to ec2 instance
4. create a record pointing to load balancer in route 53