Setting up a robust infrastructure on AWS can seem daunting, but with a clear roadmap, you can get your cloud environment up and running efficiently. This guide will walk you through setting up an AWS account, configuring an EC2 instance, installing Node.js with PM2, setting up an RDS PostgreSQL database, and creating an S3 bucket with public read access.

1. Creating an AWS Account and Enabling Multi-Factor Authentication (MFA)

Step 1: Create an AWS Account

  • Visit the AWS website and click on “Create an AWS Account.”
  • Follow the on-screen instructions to provide your account details, including email, password, and payment information.

Step 2: Enable MFA for Account Security

  • After logging in, navigate to the IAM (Identity and Access Management) dashboard.
  • Select “Users” from the sidebar, click on your username, and then go to the “Security credentials” tab.
  • Under “Multi-Factor Authentication (MFA),” click on “Manage MFA” and follow the prompts to configure MFA using an authenticator app.

2. Setting Up IAM User and Role Group

Step 1: Create an IAM User

  • Go to the IAM dashboard and select “Users” from the sidebar.
  • Click “Add user,” enter a username, and select the type of access (Programmatic access and AWS Management Console access).
  • Attached are the necessary policies, such as AmazonEC2FullAccess, AmazonS3FullAccess, and AmazonRDSFullAccess.

Step 2: Create a Role Group

  • Select “User groups” in the IAM dashboard and click “Create group.”
  • Name your group and attach relevant policies that provide the necessary permissions.
  • Add your newly created user to this group.

3. Setting Up an EC2 Instance

Step 1: Launch an EC2 Instance

  • Navigate to the EC2 dashboard and click on “Launch Instance.”
  • Choose an Amazon Machine Image (AMI), select an instance type (e.g., t2.micro for free tier), and configure instance details.
  • In the “Configure Security Group” step, create a new security group and allow SSH (port 22) and any custom port required for your application (e.g., port 3000).

Step 2: Connect to Your EC2 Instance

Once your instance is running, connect to it using SSH from your terminal:
ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip

4. Installing Node.js and PM2 on EC2

Step 1: Install Node.js

Update your instance and install Node.js:
sudo yum update -y

sudo yum install -y nodejs

Step 2: Install PM2

Install PM2 globally to manage your Node.js applications:
sudo npm install pm2@latest -g

5. Configuring Custom Port Range for EC2 API Access

Step 1: Modify Security Group

  • In the EC2 dashboard, select your instance and click “Security Groups” under the Description tab.
  • Click “Edit inbound rules,” add a new rule to allow your custom port (e.g., port 3000 for your API), and set the source to “Anywhere” or a specific IP range.

6. Running Application Permanently with PM2

Step 1: Start Your Node.js Application

Navigate to your application directory and start your app using PM2:
pm2 start app.js

Step 2: Set PM2 to Start on the Boot

To ensure your application runs even after a reboot, set PM2 to start on boot:
pm2 startup

pm2 save

7. Creating and Configuring AWS RDS PostgreSQL Database

Step 1: Create an RDS Instance

  • Go to the RDS dashboard and click on “Create database.”
  • Select PostgreSQL, choose “Free tier,” and configure settings such as DB instance identifier, username, and password.
  • Ensure the database is created in the same VPC as your EC2 instance for connectivity.

Step 2: Configure Security Group for RDS

  • Edit the inbound rules of the RDS security group to allow traffic from your EC2 instance’s security group on the PostgreSQL port (default is 5432).

Step 3: Connect to the Database

Install psql on your EC2 instance and connect to the database using:
psql -h your-rds-endpoint -U your-username -d your-database-name

8. Creating S3 Bucket and Setting Public Read Access

Step 1: Create an S3 Bucket

  • Navigate to the S3 dashboard and click on “Create bucket.”
  • Enter a unique bucket name and select the region where you want to create the bucket.

Step 2: Configure Public Read Access

  • After creating the bucket, go to the “Permissions” tab.

Edit the bucket policy to grant public read access:
{

  “Version”: “2012-10-17”,

  “Statement”: [

    {

      “Sid”: “PublicReadGetObject”,

      “Effect”: “Allow”,

      “Principal”: “*”,

      “Action”: “s3:GetObject”,

      “Resource”: “arn:aws:s3:::your-bucket-name/*”

    }

  ]

}

Conclusion

Following this guide, you’ve successfully set up an AWS environment with a secure account, an EC2 instance running Node.js and PM2, a connected PostgreSQL RDS database, and an S3 bucket with public read access. This setup provides a solid foundation for deploying and scaling your applications on AWS.

References

Setting Up Node.js on an Amazon EC2 Instance

Adding an Amazon RDS DB instance to your Node.js Elastic Beanstalk environment