Deploy a Web Application on AWS

Introduction

This article will demonstrate using multiple AWS services to deploy a web application. First of all, we will have an EC2 to host an application using PHP. Secondly, our data will be stored on Amazon DynamoDB. Finally, We will use AWS S3 to store our images.

Solution Diagram

Firstly, we will create S3 bucket. Secondly, we will create DynamoDB. Thirdly, we will create an Amazon Virtual Private Cloud (VPC) with an Internet Gateway and a public subnet. Finally, we will deploy a web application on EC2.

Create S3 Bucket

Search S3 and then click create bucket.

Give a unique name to the bucket.

Scroll down to uncheck “Block all public access” and check the acknowledgement.

Click “Create bucket”.

Back to the bucket page, we will see something like this.

Create an Amazon DynamoDB Table

Go to DynamoDB and click create table.

Please follow the exact names shown as above: Table name=”AWS-Services”, Partition key=”Category”, select “Add sort key” and enter “Name” in the text box. Finally, click create.

We will see the table is creating, just move on to the next step.

Create an Amazon VPC

Click “Your VPCs” on left hand side, then click create VPC on right hand side.

Type the name tag and CIDR block, then click create VPC.

Click Subnets and then create subnet.

Choose the VPC we just created, then type the name tag, choose the AZ and type CIDR for subnet, finally create subnet.

It will look like this, we create public subnet successfully.

Choose Internet Gateway and then create internet gateway.

Type the name and create internet gateway.

Once we create the gateway, click Actions => attach to VPC.

Select the VPC we just create, and click attach internet gateway.

Choose Route tables, and create route table.

Type the route table name and choose the VPC we created. Then click create.

Close this.

Select the route table and click edit routes.

Add a new route and choose the internet gateway we created as target. Save routes.

Click Subnet Associations, then click edit subnet association.

Select the public subnet we created, then click save.

Launch an Amazon EC2 Instance

In EC2 instance page, click instance then launch instnace.

Choose Amazon Linux 2 AMI then click select.

Click Next.

Select the network and subnet we just created. Enable auto-assign public IP. Then create an IAM role as shown above.

Click Create Role.

Select EC2 and click Next.

Choose S3 Full Access.

And DyanmoDB Full Access as well. Click Next.

Click Next to review.

Type the role name and create role.

Back to EC2 instance creation, refresh IAM role and choose the role we created.

Scroll down and expand the advanced details. Place the below config into user data.

#!/bin/bash
# Install Apache Web Server and PHP
yum install -y httpd
amazon-linux-extras install -y php7.2
# Download Lab files
wget https://s3-us-west-2.amazonaws.com/aws-tc-largeobjects/CUR-200-CCA-31-EN/lab1src.zip
unzip lab1src.zip -d /tmp/
mv /tmp/lab1src/*.php /var/www/html/
# Download and install the AWS SDK for PHP
wget https://github.com/aws/aws-sdk-php/releases/download/3.62.3/aws.zip
unzip aws -d /var/www/html
# Determine Region
AZ=`curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone/`
REGION=${AZ::-1}
# Copy files to Amazon S3 bucket with name webapp-*
BUCKET=`aws s3api list-buckets --query "Buckets[?starts_with(Name, 'webapp-')].Name | [0]" --output text`
aws s3 cp /tmp/lab1src/jquery/ s3://$BUCKET/jquery/ --recursive --acl public-read --region $REGION
aws s3 cp /tmp/lab1src/images/ s3://$BUCKET/images/ --recursive --acl public-read --region $REGION
aws s3 ls s3://$BUCKET/ --region $REGION --recursive
# Configure Region and Bucket to use
sed -i "2s/%region%/$REGION/g" /var/www/html/*.php
sed -i "3s/%bucket%/$BUCKET/g" /var/www/html/*.php
# Copy data into DynamoDB table
aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services1.json --region $REGION
aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services2.json --region $REGION
aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services3.json --region $REGION
# Turn on web server
chkconfig httpd on
service httpd start

Click Next.

Type Name as key, and Web Server as Value.

Type the security group name and description. Add a HTTP port and open for anywhere. Then click review.

Click Launch.

If you do not have existing key, just create a new one and download key pair. Then launch instances.

Result

Finally, browse the IP that AWS provides to us. Then we can test our application now.

Cleanup

Terminate the instance.

Delete the role.

Delete VPC.

Confirm to delete.

Delete S3 bucket.

Delete table.

Leave a Reply