Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Automating deployment in AWS:

CloudFormation and Elastic


Beanstalk
Outline
• Creating VMs and running scripts on startup with AWS
CloudFormation
• Deploying common web apps with AWS Elastic
Beanstalk

2
Creating VMs and running scripts on startup with
AWS CloudFormation
• Deployment: Install, update, and configure an application and
its dependencies.
• You’ll learn about three tools for deploying applications
to virtual machines on AWS
1. Deploying a VPN solution with the help of AWS CloudFormation
and a script that starts at the end of the VM boot process.
2. Deploying a collaborative text editor with AWS Elastic
Beanstalk.
• The text editor Etherpad is a simple web application.
3. Deploying an IRC web client and IRC server with AWS
OpsWorks.
• The setup consists of two parts: the IRC web client and the IRC
server itself.
3
Examples are 100% covered by the Free Tier
• The examples in this lecture are completely covered by the
Free Tier. As long as you don’t run the examples longer than
a few days, you won’t pay anything.
• You’ll clean up your account at the end.

4
Steps to deploy a WordPress Web Application on
a VM
1. Install
1. Install an Apache HTTP server, a MySQL database, a PHP runtime
environment, a MySQL library for PHP, and an SMTP mail server.
2. Download the WordPress application, and unpack the archive on your
server.
2. Configure:
1. Configure the Apache web server to serve the PHP application.
2. Configure the PHP runtime environment to tweak performance and
increase security.
3. Edit the wp-config.php file to configure the WordPress application.
4. Edit the configuration of the SMTP server, and make sure mail can only be
sent from the virtual machine, to avoid misuse from spammers.
3. Start the Services: Start the MySQL, SMTP, and HTTP services.
5
Manual vs. Automated Deployment
• Deployment in traditional environment
• Manual Deployment: The deployment steps are often
performed manually following how-to-guides.
• Deployment in a Cloud Environment
• Automated Deployment:
• The goal is to automate the deployment using automation tools.
• For building scalable and highly available infrastructure, you’ll need
to start new virtual machines several times a day.
• Over time, the number of VMs in your infrastructure will grow.
• Deploying software manually to a growing number of VMs becomes
impossible over time.
• Automated deployment process will pay off in the future by
increasing efficiency and decreasing human error.
6
Comparing deployment tools

Deployment must be
automated in a flexible and
scalable cloud environment

7
Three ways of automated deployment on AWS
1. Creating a virtual machine and running a deployment script on
startup with AWS CloudFormation.
2. Using AWS Elastic Beanstalk to deploy a common web application.
3. Using AWS OpsWorks to deploy a multilayer application.

8
Classifying the deployment tools
• AWS Elastic Beanstalk:
• The effort required to deploy an application is low.
• Your application has to fit into the conventions of AWS Elastic Beanstalk. E.g.,
the application must run in one of the standardized runtime environments.
• OpsWorks:
• freedom to adapt the service to your application’s needs. For example,
• you can deploy different layers that depend on each other, or
• you can use a custom layer to deploy any application with the help of a Chef recipe
• CloudFormation:
• deploying applications with the help of a script that runs at the end of the
boot process.
• You can deploy any application.
• The disadvantage is that you have to do more work.
9
Outline
• Creating VMs and running scripts on startup with AWS
CloudFormation
• Deploying common web apps with AWS Elastic
Beanstalk

10
Creating VMs and running scripts on startup with AWS
CloudFormation
• Follow the following steps to automate the deployment of an
application on a VM:
1. Start a plain virtual machine containing just an OS.
2. Execute a script the end of the boot process.
3. Install and configure your applications with the help of the script.
• With these steps, you will go from a plain OS to a provisioned (i.e.,
fully installed and configured) VM
• Using user data to run a script on startup (user data executed as a
script)
• Inject the user data during the creation of a new VM to customize it.
• user data is executed as a shell script at the end of the boot process with root
user privileges.
• user data is always accessible from the VM with a HTTP GET request to
http://169.254.169.254/latest/user-data
11
Deploying OpenSwan: a VPN server to a virtual machine
• Secure communication from a personal computer to a VM through
VPN Tunnel.
• The VPN solution, called OpenSwan, offers an IPSec-based tunnel.
• How to deploy a VPN server to a virtual machine with the help of user
data and a shell script next?

12
Execute the commands shown in listing 5.1 step by step to
start a virtual machine and deploy a VPN server on it

13
Shortcut for Linux and macOS
• You can avoid typing these commands manually at your terminal by
using the following command to download a Bash script and execute
it directly on your local machine.
• The Bash script contains the same steps as shown in listing 5.1.

14
• The output of the last command (i.e., aws cloudformation describe-stacks --stack-
name vpn --query "Stacks[0].Outputs") in listing 5.1 should print out the public IP
address of the VPN server, a shared secret, the VPN username, and the VPN
password.
• You can use this information to establish a VPN connection from your computer, if
you like:

15
A deeper look at the deployment process of the VPN server:
Using CloudFormation to start a Virtual Machine with User Data
• The template for the VPN server includes a shell script packed into user
data, as shown in listing 5.2.

16
17
18
!Sub and !Base64 functions
• The CloudFormation template includes two new functions !Sub and
!Base64.
• With !Sub, all references within ${} are substituted with their real
value.

• The function !Base64 encodes the input with Base64.

19
Installing and Configuring a VPN Server with a Script
• The user data contains a small script to fetch and execute the real
script, vpn-setup.sh,
• which contains all the commands for installing the executables and
configuring the services.
• Doing so frees you from inserting complicated scripts in the
CloudFormation template.
• The vpn-setup.sh script shown in the following listing installs
packages with the help of the package manager yum and writes some
configuration files.
• this shell script is executed during the boot process to install and configure a
VPN server.

20
21
End of automated VPN Server Deployment
• You’ve reached the end of the VPN server example.
• Don’t forget to terminate your virtual machine and clean up your
environment.
• To do so, enter aws cloudformation delete-stack --stack-name vpn at
your terminal.

22
Outline
• Creating VMs and running scripts on startup with AWS
CloudFormation
• Deploying common web apps with AWS Elastic
Beanstalk

23
• With AWS Elastic Beanstalk
• you don’t have to worry about your OS or virtual machines.
• The OS and the runtime (such as Apache + Tomcat) are managed by AWS.
• You only deal with your application.
• AWS Elastic Beanstalk lets you handle the following recurring
problems:
• Providing and updating a runtime environment for a web application (PHP,
Java, and so on)
• Installing and updating a web application automatically
• Configuring a web application and its environment
• Scaling a web application to balance load
• Monitoring and debugging a web application
24
Components of AWS Elastic Beanstalk

25
Components of AWS Elastic Beanstalk
• An application is a logical container. It contains versions, environments, and
configurations.
• A version contains a specific release of your application.
• To create a new version, you have to upload your executables (packed into an
archive) to Amazon S3, which stores static files.
• A version is basically a pointer to this archive of executables.
• A configuration template contains your default configuration.
• You can manage your application’s configuration (such as the port your application
listens on) as well as the environment’s configuration (such as the size of the virtual
machine) with your custom configuration template.
• An environment is where AWS Elastic Beanstalk executes your application.
• It consists of a version and the configuration.
• You can run multiple environments for one application by using different
combinations of versions and configurations.

26
Using AWS Elastic Beanstalk to deploy Etherpad, a
Node.js application
• Etherpad is a Node.js-based open source online editor
• Lets you edit a document with many people in real time.
• You’ll deploy it with the help of AWS Elastic Beanstalk in three steps:
1. Create an application: the logical container.
2. Create a version: a pointer to a specific version of Etherpad.
3. Create an environment: the place where Etherpad will run.
• CREATING AN APPLICATION FOR AWS ELASTIC BEANSTALK
• Open your terminal, and execute the following command to create a
container for all the other components that are necessary to deploy Etherpad

27
Using AWS Elastic Beanstalk to deploy Etherpad, a
Node.js application
• CREATING A VERSION FOR AWS ELASTIC BEANSTALK
• By executing the following command, you create a version labeled 1.

• CREATING AN ENVIRONMENT TO EXECUTE ETHERPAD WITH AWS


ELASTIC BEANSTALK
• To get the latest Node.js environment version, called a solution stack name,
run this command:

28
Using AWS Elastic Beanstalk to deploy Etherpad, a
Node.js application
• Execute the following command to launch an environment, replacing
$SolutionStackName with the output from the previous command.

29
Having fun with Etherpad
• Use the following command to track the state of your Etherpad
environment:

30
31
Having fun with Etherpad
• Point your browser to the URL shown in CNAME, and open a new
document by typing in a name for it and clicking OK.
• Following Figure shows an Etherpad document in action.

32
Exploring AWS Elastic Beanstalk
with the web-based
Management Console @
https://console.aws.amazon.com

33
Troubleshooting an AWS Elastic Beanstalk Application
• You can fetch the log messages from your application (and
other components) using AWS Elastic Beanstalk.
• Follow these steps:
• Choose Logs from the submenu. You’ll see a screen like that shown
in figure 5.7.
• Click Request Logs, and choose Last 100 Lines.
• After a few seconds, a new entry will appear in the table. Click
Download to download the log file to your computer.

34
35
Cleaning up AWS Elastic Beanstalk
• Run the following command to terminate the Etherpad environment:

• Check the state of the environment by executing the following


command:

• Wait until Status has changed to Terminated, and then proceed with
the following command:

36
Summary
• Automating the deployment of your applications onto virtual
machines allows you to take full advantage of the cloud: scalability
and high availability.
• You can update an application by throwing away old VMs and starting
new, up-to-date ones if you’ve automated your deployment process.
• Injecting Bash or PowerShell scripts into a virtual machine during
startup allows you to initialize virtual machines individually—for
example for installing software or configuring services.
• AWS Elastic Beanstalk is best suited for deploying common web
applications.
• AWS CloudFormation gives you the most control when you’re
deploying more complex applications.
37

You might also like