Use case overview
In this simple post we will show you how easy it is to provision an Apache Server on an AWS EC2 instance and then automatically copy across static content from an S3 bucket. This can be a handy tip if you want to quickly deploy a static website and your content is stored on S3.
The below approach can also be used as part of a Load Balanced configuration across different Regions & Availability zones, we will cover this off in a separate post.
Creating an EC2 Instance
AWS make it easy to create an EC2 server from their console but have you ever spotted the Advanced Details at the bottom of the Configure Instance page? If not then we will show with a simple bash script how to get going.
Firstly, choose the AMI you wish to use for your instance. For the purpose of this post we are going to use the Amazon Linux 2 LTS Candidate 2 AMI (HVM), SSD Volume Type – ami-924aa8f5
Once selected you can then choose the instance type, a general purpose t2.micro is sufficient for our example however you should consider high capacity servers depending on your requirements.
Now within the Configure Instance page you can scroll to the bottom and expand the Advanced Details. You will then be presented with User Data which allows you to configure scripts to run as the instance is provisioned. There are a wide range of uses for this but in our use case we want to automatically create a web server and move the content to it from an S3 bucket.
Adding content to an S3 bucket
In order for the content to be copied to the web server you must have a bucket setup with public permissions. If you create an S3 bucket and make it public please ensure no sensitive data is available.
We have created a test public bucket named s3://myWebsiteBucketName – the content of the website will be stored in this bucket. This could contain HTML, JS, CSS and image files to populate your site.
Using a bash script
Using a bash script we can script what the instance does when it starts, we have provided an example simple script below. If you want to reuse the below then make sure you replace myWebsiteBucketName with your own S3 bucket name.
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
aws s3 cp s3://myWebsiteBucketName /var/www/html –recursive
So what does the above script do, let’s break it down:
Firstly, we perform a yum update on instance so the latest versions have been downloaded and installed. We when install the Apache server by requesting yum install httpd. When the service has been installed we can then start the service by service httpd start.
To ensure that the Apache service starts when the instance is rebooted we can add in chkconfig httpd on now if the server is restarted the service will run again.
Finally, now we have a web server up and running we want to serve up the static content. We then copy across the content from the publicly accessible S3 bucket into our default web server folder by running aws s3 cp s3://myWebsiteBucketName /var/www/html –recursive
Once this has completed when browse the public IP address of your instance and you should see your static web pages displayed.
If you would like more information on the specific AWS commands available then take a look at the below link.