How to Create EC2, S3 Bucket, and Deploy a Website Using AWS Free Tier

image_print


Amazon Web Services (AWS) offers a robust platform for deploying and hosting websites. In this guide, we will walk through the steps of creating an EC2 instance, setting up an S3 bucket, uploading files, assigning IAM roles, and deploying a website live using the Free Tier.


Step 1: Set Up an EC2 Instance

  1. Log in to AWS Console:
  1. Launch a New EC2 Instance:
  • Navigate to EC2 from the services menu.
  • Click Launch Instance to create a new EC2 instance.
  1. Select an Amazon Machine Image (AMI):
  • Choose an AMI for your instance. For a simple website, you can choose the Amazon Linux 2 AMI (Free Tier eligible).
  1. Choose an Instance Type:
  • Select the t2.micro instance type (Free Tier eligible).
  • Click Next: Configure Instance Details.
  1. Configure Instance:
  • Leave the default settings, or modify as needed.
  • Click Next: Add Storage.
  1. Add Storage:
  • You can leave the default storage settings (8GB). Click Next: Add Tags.
  1. Add Tags:
  • Optionally, add a tag with a key-value pair like Name: MyWebsite.
  1. Configure Security Group:
  • Create a new security group and add the following inbound rules:
    • HTTP (Port 80) to allow web traffic.
    • SSH (Port 22) to access the EC2 instance via SSH.
  • Click Review and Launch.
  1. Launch the EC2 Instance:
  • Review your instance settings and click Launch.
  • Select an existing key pair or create a new key pair to access the instance via SSH.
  • Click Launch Instances.

Step 2: Set Up an S3 Bucket

  1. Create an S3 Bucket:
  • In the AWS Console, navigate to S3.
  • Click Create bucket.
  • Enter a unique name for your bucket (e.g., mywebsite-bucket).
  • Select a region and click Create.
  1. Upload Files to S3:
  • Go to your S3 bucket.
  • Click Upload and select the files for your website (e.g., HTML, CSS, JavaScript files).
  • Click Next and Upload.
  1. Set Bucket Permissions:
  • Open your bucket and click Permissions.
  • Edit the Bucket Policy to allow public access to the files (use the following policy):
   {
       "Version": "2012-10-17",
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": "*",
               "Action": "s3:GetObject",
               "Resource": "arn:aws:s3:::mywebsite-bucket/*"
           }
       ]
   }
  • Make sure to replace mywebsite-bucket with your actual bucket name.
  1. Set Bucket to Static Website Hosting:
  • Go to Properties and click on Static website hosting.
  • Select Use this bucket to host a website and set Index Document to index.html and Error Document to error.html (or whatever file names your website uses).

Step 3: Assign IAM Role to EC2 Instance

  1. Create IAM Role:
  • Go to the IAM Console.
  • Click Roles and then Create Role.
  • Select EC2 as the trusted entity type.
  • Attach the policy AmazonS3FullAccess to allow EC2 to interact with the S3 bucket.
  • Review and name the role (e.g., EC2S3Role), then click Create Role.
  1. Attach IAM Role to EC2 Instance:
  • In the EC2 Dashboard, select your EC2 instance.
  • Click on Actions > Security > Modify IAM Role.
  • Select the IAM role you created (EC2S3Role) and click Update IAM Role.

Step 4: SSH into EC2 and Install Web Server

  1. SSH Into EC2 Instance:
  • Download the .pem key file if you haven’t already done so.
  • Open your terminal (or Command Prompt) and use the following command to SSH into the EC2 instance:
   ssh -i /path/to/your-key.pem ec2-user@<your-ec2-public-ip>
  • Replace <your-ec2-public-ip> with your EC2 instance’s public IP.
  1. Install Web Server (Apache/Nginx):
  • Update the instance and install Apache:
   sudo yum update -y
   sudo yum install -y httpd
  • Start the Apache server:
   sudo systemctl start httpd
   sudo systemctl enable httpd
  1. Configure Firewall for HTTP:
  • Make sure your security group allows inbound HTTP traffic.

Step 5: Connect EC2 with S3 and Deploy Website

  1. Access Files from S3 on EC2:
  • Once the IAM role is attached, your EC2 instance can access the files in your S3 bucket.
  1. Download Website Files from S3 to EC2:
  • On your EC2 instance, use the aws s3 cp command to copy files from S3 to the instance:
   aws s3 cp s3://mywebsite-bucket/ /var/www/html/ --recursive
  • Replace mywebsite-bucket with your bucket name.
  1. Restart Apache:
  • After copying the files, restart Apache to serve the website:
   sudo systemctl restart httpd
  1. Access the Website:
  • Open your browser and enter the public IP of your EC2 instance. Your website should be live!

Conclusion:

You have now successfully created an EC2 instance, set up an S3 bucket, uploaded files, assigned IAM roles, and deployed a website using AWS Free Tier. This approach is cost-effective and perfect for small projects, blogs, or learning purposes.


Tips:

  • Always monitor your AWS usage to ensure you stay within the Free Tier limits to avoid unexpected charges.
  • For more advanced use cases, consider using Amazon Route 53 for custom domain management.
  • You can scale your website later by integrating more AWS services like RDS, CloudFront, or Elastic Load Balancing.
image_print

Share on :

Social Share

Recent Post

© 2024 All rights reserved by Go1digital.com