DEV Community

Mohammed Shaheem P
Mohammed Shaheem P

Posted on

Quick Guide for AWS

Deploy to EC2


Security Group

Setup the following security group configuration

Preffered security group for ec2 instance

Connect with SSH

  • Update the permission of downloaded certificate file
chmod 400 <file_name>.pem
Enter fullscreen mode Exit fullscreen mode
  • Connect to the distant server with the command
ssh -i <certificate>.pem ec2-user@<Public_IPv4_DNS>
Enter fullscreen mode Exit fullscreen mode

Install node with nvm

  • Download nvm using curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Enter fullscreen mode Exit fullscreen mode
  • Activate nvm with
. ~/.nvm/nvm.sh
Enter fullscreen mode Exit fullscreen mode
  • Install node with
nvm install node
Enter fullscreen mode Exit fullscreen mode
  • Test node installation with
node -e "console.log('Running Node.js ' + process.version)"
Enter fullscreen mode Exit fullscreen mode

Checkout the official documentation on how to setup node in ec2 linux

Port Forwarding from 80 to 8000

Use the following command for port forwarding

sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8000
Enter fullscreen mode Exit fullscreen mode

Install MongoDB community edition

  • Verify Linux Distribution with
grep ^NAME  /etc/*release
Enter fullscreen mode Exit fullscreen mode

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:
cd /etc/yum.repos.d
sudo touch mongodb-org-4.4.repo
Enter fullscreen mode Exit fullscreen mode
  • Add the following Configuration to created file with nano or vim
[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
Enter fullscreen mode Exit fullscreen mode
  • Install MongoDB packages with
sudo yum install -y mongodb-org
Enter fullscreen mode Exit fullscreen mode
  • Setup storage directory for MongoDb
cd /
sudo mkdir data
cd data
sudo mkdir db
cd ../..
cd home/ec2-user
Enter fullscreen mode Exit fullscreen mode
  • Start mongodb service
sudo service mongod start
Enter fullscreen mode Exit fullscreen mode

Checkout common errors faced during this

Checkout the offficial Documentation

Install PostgreSQL

Follow this article for installing PostgreSQL

after install use this command to open db shell

sudo su - postgres
Enter fullscreen mode Exit fullscreen mode

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
zip -r server.zip <project_directory>
Enter fullscreen mode Exit fullscreen mode
  • Transfer zipped file using scp
scp -i <certificate>.pem server.zip ec2-user@<Public_IPv4_DNS>:
Enter fullscreen mode Exit fullscreen mode
  • Now connect back to instance with SSH and unzip the file
unzip server.zip
Enter fullscreen mode Exit fullscreen mode

Run Application with screen

install the node modules and run the app with screen

screen npm start
Enter fullscreen mode Exit fullscreen mode

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

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

CloudFront Guide


Use this tutorial for setting up cloudfront


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

Top comments (0)