Amazon Web Services (AWS)

Amazon Web Services (AWS) is Amazon's cloud computing platform that offers a wide range of services like virtual servers, storage, databases, and networking on a pay-as-you-go basis. It provides on-demand infrastructure and tools for individuals, companies, and governments to build, scale, and run applications, websites, and other services in the cloud, leveraging economies of scale and industry-leading security.

Amazon S3

Amazon Simple Storage Service (S3) is a storage service that lets you create buckets and store objects inside of them. Common use cases of AWS S3 include backing up data, archiving, storing and sharing files, hosting static websites, supporting analytics, and storing logs or machine learning data.

  • Buckets are containers for objects.
  • Objects are files and any metadata that describes that file. They can be opened, downloaded, or moved.

Creating an S3 Bucket

  1. Click "Create Bucket"
  2. General Configuration
    • Bucket Type: General Purpose
    • Bucket Name: grants-dev-toolkit
  3. Object Ownership
    • Object Ownership: ACLs Disabled
  4. Block Public Access
    • Block All Public Access: Checked
  5. Bucket Versioning
    • Bucket Versioning: Disabled
  6. Default Encryption
    • Encryption Type: Server-side encryption with Amazon S3 managed keys (SSE-S3)
    • Bucket Key: Enable
  7. Click "Create Bucket"

Uploading Objects to Buckets

  1. Click on the bucket you want to add an object to
  2. Click "Upload"
  3. Click "Add Files"
  4. Select the file you want to upload
  5. Click "Upload"

Amazon CloudFront

Amazon CloudFront is a content delivery network (CDN) that speeds up distribution of static and dynamic web content such as .html, .css, or .js and image files, from edge locations close to users to improve performance.

  • A Distribution is a CloudFront configuration that specifies the origin, caching behavior, and other settings for managing content delivery.
  • An Invalidation is a way to force CloudFront to refresh cached content and serve the updated version to users immediately.

Creating a CloudFront Distribution

  1. Click "Create Distribution"
  2. Select "Free Plan" and click "Next"
  3. Get Started
    • Distribution Name: good-css-cdn
    • Distribution Type: Single Website or App
  4. Specify Origin
    • Origin Type: Commonly Amazon S3
    • Origin: Select the S3 Origin (if applicable) grants-dev-toolkit.s3.us-east-1.amazonaws.com
    • Origin: Select the Origin Path /production (this should only be a folder, not a file)
    • Settings: Allow private S3 bucket access to CloudFront - Recommended: Checked
    • Settings: Origin settings: Use recommended origin settings
    • Settings: Cache settings: Use recommended cache settings tailored to serving S3 content:
  5. Enable Security
    • Use Monitor Mode: Selected
  6. Click "Create Distribution"
  7. Access the Distribution https://d11dibs3lqrpwz.cloudfront.net/Good.css

Creating a CloudFront Invalidation

  • Click the Distribution
  • Click Invalidations
  • Click "Create Invalidation"
  • Add an Object Path
    /Good.css
    /*

    These paths are removed from the CloudFront cache. After invalidation, the next time a user requests the file, CloudFront fetches the updated version from the origin so users see the correct content immediately.

Amazon EC2

Amazon Elastic Compute Cloud (EC2) provides on-demand, resizable virtual servers (called instances) in the cloud for running various applications, offering flexible computing capacity without upfront hardware costs.

Common use cases of AWS EC2 include running websites and applications, hosting databases, handling batch processing, supporting machine learning workloads, and creating scalable cloud servers.

  • An Instance is a virtualized computing environment that runs on AWS's physical infrastructure. You can choose the CPU, memory, storage, and operating system of an instance.

Creating an EC2 Instance

  1. Click "Launch Instance"
  2. Add a Name
  3. Application and OS Images:
    • Select Ubuntu
    • Ubuntu Server 24.04 LTS (HVM), SSD Volume Type
  4. Instance Type:
    • Select t2.nano
  5. Create a Key Pair Login (if none exist)
  6. Network Settings
    • Create Security Group: Selected
    • Allow SSH traffic from: My IP
    • Allow HTTPS traffic from the internet: Selected
    • Allow HTTP traffic from the internet: Selected
  7. Create Security Group
    1. Add a name
    2. Add a description
  8. Configure Storage: 1x 8 GiB gp3 Root volume, 3000 IOPS, Not encrypted
  9. Click "Launch Instance"

Note: When creating an EC2 instance, the type depends on the type of instance upon creation (Amazon Linux = ec2-user whereas Ubuntu = ubuntu).

Connecting to an EC2 Instance (w/ SSH)

ssh -i [location of key] [type]@[Public IPv4 address]

ssh -i grants_dev_tools_key.pem ec2-user@18.234.88.211

Updating an EC2 Instance (w/ SSH)

# Create a copy of the file/folder you want to upload
# cp -r directory

# Before you copy your folder to an EC2 instance, remove any files or folders you do not want copied over

cd directory
rm -rf .git
rm -rf node_modules

# Secure copy (SCP) the folder you want copied to the EC2 instance along with your key

scp -i ./Code/Keys/grants_dev_tools_key.pem -r grants_dev_tools ec2-user@184.73.21.17:/home/ec2-user/

# The inside the EC2 Instance is bare bones and has nothing.

# If Amazon Linux
sudo yum update -y
sudo yum install -y nodejs npm

cd directory
npm i

# If Ubuntu or Debian

sudo apt update
sudo apt upgrade

# You need to install a web server

sudo dnf -y httpd

# Remove the old file/folder in the /var/www/html directory

sudo rm -rf /var/www/html/*

# Secure copy the newly uploaded file/folder to the /var/www/html directory

sudo cp -r /home/ec2-user/grants_dev_tools_updated/* /var/www/html/

# Exit the EC2 instance

exit

Creating an Express.js App Hosted on an EC2 Instance

ssh -i ./Code/Keys/grants_dev_tools_key.pem ubuntu@54.147.152.76
# Connect via SSH

ssh -i ./keys/ec2_key.pem ubuntu@54.147.152.76

# Update and Upgrade

sudo apt update
sudo apt upgrade

# Install Node.js

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version
node
console.log("We're using Node on an EC2 instance!");

# Copy Files and Folders w/ Rsync - Be inside the folder you want to copy!

rsync -avz --exclude 'node_modules' --exclude '.git' --exclude '.env' \
-e "ssh -i ../Code/Keys/grants_dev_tools_key.pem" \
. ubuntu@54.147.152.76:~/app

# Install Dependencies

sudo npm install
sudo npm install -g pm2

# Install NGINX

sudo apt update
sudo apt install nginx -y

sudo systemctl start nginx
sudo systemctl enable nginx

sudo nano /etc/nginx/sites-available/express-app
# Copy and paste the NGINX code below

sudo ln -s /etc/nginx/sites-available/express-app /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx

# Add Domain Name and HTTPS



# Set Up PM2

pm2 start server.js
pm2 save
pm2 startup systemd # This will print something similar to: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu. Run that command.
sudo reboot
# Reconnect to the EC2 using SSH: ssh -i ./Code/Keys/grants_dev_tools_key.pem ubuntu@54.147.152.76
pm2 list

# Exit the EC2 instance

exit

Note: ec2-98-88-77-166.compute-1.amazonaws.com is the Public DNC address.

Note: You need to add a custom rule for allowing port 3000 to work. Only HTTP works, NOT HTTPS: http://98.88.77.166:3000/.

Note: PM2 is a process manager for Node.js. It is used to keep the server running automatically.

Note: The -r is used to recursively copy a directory.

server {
  listen 80;
  server_name _;

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

Amazon Lambda

Amazon Lambda is a serverless, secure, on-demand, event-driven, compute service that scales as needed and billed only for what is used. It allows users to run code without provisioning or managing servers. This means that users can focus on writing their application logic, and Lambda automatically handles the underlying infrastructure, including scaling, patching, and maintenance.

Common use cases of AWS Lambda include running event-driven code, automating tasks, processing data streams, building serverless applications, and integrating with other AWS services.

Creating a Lambda Function

Coming soon...