Automating Web Server Deployment
As AWS Consulting partners, our Cloud Consulting Practice is often tasked with helping customers to set up scalable web services farms that can expand and shrink in line with demand. This approach ensures that our customers are not billed for excess web server capacity during quiet times, and also ensures that when demand increases, that web servers can be added to the farm quickly to ensure that websites perform well and customer satisfaction remains high. A simple method we sometimes follow to add nodes to web farms is by the use of the “User Data” field within AWS EC2 . By populating the User Data field with appropriate script parameters, web servers can be pre-configured and ready to serve content as soon as they are provisioned.
Example of an automated AWS Web Server Configuration
The follow example shows pre-configured deployment of EC2 Web Servers using the AWS EC2 console, however the same commands can be applied when provisioning via the AWS CLI.
Launch a new Linux EC2 instance and choose the size you want.
For testing just use a nano instance.
Specify the Instance Details
Such as VPC location, subnet, public IP and other fundamental instance settings. Scroll down to the bottom of the Instance Details screen and expand the Advanced Details option.
Add the auto-configuration script
For example, here we update the Linux image, deploy Apache Web Services, and copy key files across from our S3 bucket to the web server’s directory. In this example, the website data we want to use is inside a folder called “web-content”, that is copied across from the S3 bucket to the new web server. The content includes a static “index.html” file that will automatically be used by the Apache Web Server.
Note that the –recursive switch ensures that sub-directories and files are also copied. Also be aware that if the S3 bucket region is not specified in the script, the AWS CLI assumes that the EC2 instance and the S3 bucket are in the same region.
The script, for reference is:
#!/bin/bash yum update -y yum install httpd -y service httpd start chkconfig httpd on sudo aws s3 cp s3://<yourbucketname>/web-content /var/www/html --recursive --no-sign-request
Finish the configuration and launch the instance
Follow through the remaining steps for provisioning the instance – storage, tags, security group before launching the image.
Make sure you secure even your test web servers correctly by applying strict Security Group rules.
After the instance is launched, the new pre-configured web server should be ready to go, with the pre-configured index.html file showing as the landing page on the new server. As part of an AutoScaling script, we would typically add the node to an existing Elastic Load Balancer as well.
As usual, the AWS documentation is thorough. For more info on S3 copy commands, take a look at:
AWS CLI Reference: https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html