AWS Cloud and Key Concepts
Why Do We Need Cloud?
The cloud enables businesses and individuals to access and manage computing resources over the internet, without the need to invest in and maintain physical infrastructure. It offers flexibility, scalability, cost-efficiency, and global accessibility for applications and services.
AWS Regions and Availability Zones
- AWS Regions: AWS operates in multiple geographic regions across the world. A region is a collection of data centers (Availability Zones) located in a specific area. Each region is isolated from others to ensure that failures in one region do not affect others.
- Availability Zones (AZ): Each AWS region is divided into multiple AZs, which are isolated locations within the region. They are connected to each other with low-latency links, providing fault tolerance, high availability, and scalability.
- Local Zones: AWS Local Zones are an extension of an AWS region located closer to end-users, offering lower-latency access to AWS services. This is useful for applications that require single-digit millisecond latencies.
Creating a Free AWS Account and EC2 Instance
- Free AWS Account: By signing up for an AWS free tier, users can access a range of AWS services with limited usage, including EC2, Lambda, S3, and more, at no cost for the first 12 months.
- EC2 Instance: EC2 (Elastic Compute Cloud) is a service that provides scalable computing capacity. Users can create an EC2 instance, which functions as a virtual server, to run applications or host websites.
Accessing EC2 Instance
There are several ways to connect to an EC2 instance:
- Command Line (CLI): Using SSH (Secure Shell) to connect to your EC2 instance from a terminal on your local machine.
ssh -i your-key.pem ec2-user@your-ec2-public-ip
- Git Shell: Git Bash can be used to access EC2 instances via SSH for users on Windows.
- AWS Browser: You can also access EC2 instances through the browser-based AWS Systems Manager Session Manager or the EC2 Dashboard.
Nginx Installation and Writing to a File
- Nginx: Nginx is a popular web server that can be installed on EC2 instances to host websites.
- Write to
index.html: To update the website's content, you can edit the/var/www/html/index.htmlfile. You might need to usesudoto grant write permissions.
sudo echo "welcome to learning-ocean.com" | tee /var/www/html/index.html > /dev/null
- This updates the homepage of the Nginx server.
Scripting with Bootstrap
- Bootstrap: A popular front-end framework used for designing responsive and mobile-first websites. You can use Bootstrap to quickly style your web pages and make them visually appealing. Combining Bootstrap with scripting (JavaScript or jQuery) helps build interactive web applications.
Blog Summary Example:
Why AWS and Cloud Computing? Cloud computing provides flexibility, scalability, and reduced costs. AWS, a leading cloud provider, offers global access through its regions and availability zones. Local zones extend AWS services closer to end-users, ensuring low-latency access.
Setting Up and Accessing AWS EC2 After creating a free AWS account, you can launch an EC2 instance to run applications. AWS offers various methods to access your instance, including SSH via command line, Git Bash, or even browser-based tools.
Nginx Setup and Website Deployment Once your EC2 instance is up and running, you can install Nginx to host a website. Updating the homepage is simple, using commands like echo and tee to edit the index.html file.
Scripting with Bootstrap Bootstrap helps you create responsive websites quickly, while scripting adds dynamic functionality to your pages.
This summarizes how cloud computing, AWS, and basic server configurations enable powerful web solutions.
Deploying a MERN Stack App on AWS: Methods and Best Practices
Why Deploy on AWS?
AWS provides reliable, scalable, and globally accessible infrastructure for deploying web applications. Key AWS features include:
- Regions and Availability Zones: Ensure low latency and high availability by hosting resources closer to users.
- Local Zones: Support latency-sensitive applications with infrastructure even closer to the target audience.
Deploying a MERN Stack App Using EC2 Instance
What is User Data?
User data scripts are commands executed when an EC2 instance is launched, automating setup tasks.
Shell Script Example for MERN Deployment
#!/bin/bash sudo apt-get update -y sudo apt-get install -y nodejs npm git git clone https://github.com/your-repo/your-mern-app.git cd your-mern-app npm install npm run build npm start
This script:
- Updates the instance.
- Installs Node.js, npm, and git.
- Clones your MERN app repository and starts the app.
If you don't want to dive deep into shell scripting, there are simpler or alternative methods to deploy a project through user data while launching an AWS EC2 instance. Here's an overview of the approaches:
1. Use Pre-Packaged Deployment Scripts
You can use pre-written shell scripts tailored to your stack. For example:
- Use services like CodeDeploy or Ansible with pre-configured scripts.
- Modify basic deployment scripts (like the one I provided) with minimal changes (e.g., replacing URLs or commands).
2. Use Docker Containers
Docker simplifies deployment by packaging your application and its dependencies into containers.
Steps:
- Build a Docker Image of your MERN app (frontend + backend).
- Push the image to Docker Hub or Amazon Elastic Container Registry (ECR).
- Write a user data script to:
- Install Docker on the EC2 instance.
- Pull the Docker image from the repository.
- Run the container(s).
Example user data:
#!/bin/bash # Update and install Docker sudo apt-get update -y sudo apt-get install -y docker.io # Pull your Docker image docker pull your-dockerhub-username/your-mern-app # Run the Docker container docker run -d -p 80:3000 -p 5000:5000 your-dockerhub-username/your-mern-app
Why Use Docker?
- Avoids dependency issues.
- Works the same across different environments.
- Easier to test locally.
3. Use AWS Elastic Beanstalk
AWS Elastic Beanstalk automates deploying and managing applications.
Steps:
- Zip your MERN stack project.
- Upload it to Elastic Beanstalk via the AWS Management Console.
- Elastic Beanstalk will handle:
- EC2 instance creation.
- Load balancer setup.
- Deploying and managing your app.
Why Use Elastic Beanstalk?
- No need for manual instance setup.
- Easy scaling and monitoring.
- Ideal for quick deployments.
4. Use CloudFormation Templates
AWS CloudFormation allows you to define your infrastructure and deployments in a JSON or YAML file.
Steps:
- Write a CloudFormation template specifying:
- EC2 instance.
- User data script to deploy the project.
- Networking and storage configurations.
- Deploy the stack, and AWS will handle the rest.
Why Use CloudFormation?
- Automates repeatable deployments.
- Infrastructure as Code (IaC) for consistent setups.
5. Use Third-Party Deployment Tools
You can leverage tools like:
- GitHub Actions: Automate your CI/CD pipeline to deploy the project directly to EC2.
- Jenkins: Use Jenkins to automate builds and deploy them to your instance.
- Terraform: Manage and deploy infrastructure as code.
6. Manual Deployment After EC2 Launch
If automation is not a priority:
- Launch the EC2 instance.
- SSH into the instance.
- Clone your project and deploy it manually using the provided script or commands.
Which Method Should You Choose?
- Beginner-Friendly: Use Elastic Beanstalk or Docker for simplicity.
- More Control: Use shell scripts or CloudFormation templates.
- Professional/Scalable: Use CI/CD tools like GitHub Actions or AWS CodeDeploy.
If you're looking for minimal effort, Elastic Beanstalk or Docker is your best choice. If you’re aiming for professional growth, learning shell scripting or IaC tools like CloudFormation/Terraform is worthwhile.
Setting Up a Web Server with Nginx on Ubuntu
If you're looking to quickly set up a web server to serve custom content, Nginx is a lightweight and powerful solution. Here's a step-by-step guide to install Nginx, configure it, and serve your content.
1. Install Nginx
Nginx is available in Ubuntu's default repositories, so installation is straightforward.
- Update the Package List:
sudo apt update
- Install Nginx:
sudo apt install nginx -y
- Start and Enable Nginx: Start the Nginx service and ensure it runs on boot:
sudo systemctl start nginx sudo systemctl enable nginx
- Verify Installation: Test if Nginx is running:
curl localhost
- You should see the default Nginx welcome page.
2. Write Content for Your Web Page
The default Nginx configuration serves files from /var/www/html. We'll create a custom index page there.
- Navigate to the Web Root Directory:
cd /var/www/html
- Create an Index File: Write your custom message into the
index.htmlfile:
echo "Welcome to learning-ocean.com!" | sudo tee index.html > /dev/null
- Test the Custom Page: Access your server’s IP in a browser or use
curl:
curl localhost
- Output:
Welcome to learning-ocean.com!
3. Configure Firewall (Optional)
If your firewall is active, allow HTTP traffic:
sudo ufw allow 'Nginx Full'
4. (Optional) Customize Nginx Configuration
You can update the Nginx configuration to serve files from a different directory or domain name.
- Edit the Default Nginx Config File:
sudo nano /etc/nginx/sites-available/default
- Ensure the Following Configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
- Reload Nginx: Apply the changes:
sudo systemctl reload nginx
5. Conclusion
In just a few steps, you’ve installed Nginx, served a custom web page, and configured your server. Nginx’s simplicity and performance make it an excellent choice for hosting websites or applications.
Now, visit your server's IP or domain to see your custom message live!
Example Output in the Browser:
Welcome to learning-ocean.com!
