Skip to content

Instantly share code, notes, and snippets.

@viniciusban
Created March 19, 2014 15:14
Show Gist options
  • Save viniciusban/9643863 to your computer and use it in GitHub Desktop.
Save viniciusban/9643863 to your computer and use it in GitHub Desktop.
AWS EB tuto

AWS Elastic Beanstalk

This document is divided into 2 main parts:


Part I: Concepts and Diagrams

Workflow of EB

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/images/clearbox-flow-00.gif

Workflow of EB

AWS EB Components and Architectural Overview

MUST READ

  1. AWS EB Components
  2. Architectural Overview
  EB APPLICATION
 +---------------------------------------------------------------+
 |                                                               |
 |                                +----------------------------+ |
 |                                | AWS RESOURCES (deployed)   | |
 |   +-------------------+        |----------------------------| |
 |   | VERSION 1         |        |                            | |
 |   |                   |        |    +------------------+    | |
 |   +-------------------+        |    | env A            |    | |
 |   | VERSION 2         |        |    |   (VERSION 2)    |    | |
 |   |                   |        |    |   (ENV CONFIG x) |    | |
 |   +-------------------+        |    +------------------+    | |
 |   | VERSION n         |        |    | env B            |    | |
 |   |                   |        |    |   (VERSION 2)    |    | |
 |   +-------------------+        |    |   (ENV CONFIG y) |    | |
 |   | ENV CONFIG x      |        |    +------------------+    | |
 |   +-------------------+        |    | env B            |    | |
 |   | ENV CONFIG y      |        |    |   (VERSION n)    |    | |
 |   +-------------------+        |    |   (ENV CONFIG x) |    | |
 |                                |    +------------------+    | |
 |                                |                            | |
 |                                +----------------------------+ |
 |                                                               |
 +---------------------------------------------------------------+

Personal notes

  • Application: has versions, environments and environment configs.
  • Application version: Each application version is unique and you might have different versions deployed to an environment if you have a need to test changes you’ve made between one version of your application and another.
  • Environment: An environment is a version that is deployed onto AWS resources.
  • You can run the same version or different versions in many environments at the same time.
  • The environment is the heart of the application.

The diagram below shows the components of an environment:

Architectural overview of Web Server Environment tiers

  • Each environment has a elastic load balancer, a security group, an auto scaling group and one or more EC2 instances inside it.
  • There's a HOST MANAGER (HM) running inside each EC2 instance allocated for an environment.
  • The HM deploys the app.
  • Every environment has a CNAME (URL) that points to a load balancer. The environment has a URL such as MyApp.elasticbeanstalk.com. This URL is aliased in Amazon Route 53 to an Elastic Load Balancing URL—something like abcdef-123456.us-east-1.elb.amazonaws.com—by using a CNAME record. Your domain name that you registered with your DNS provider will forward requests to the CNAME.

For more information about designing scalable application architectures for AWS, read AWS Cloud Best Practices (PDF).

(end of PART I)


PART II: HANDS-ON

### Getting Started Using AWS Elastic Beanstalk (the web console tool)

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.html

Creates a default application and makes deploy of 2 versions of it using AWS EB console (web).

You can practice changing versions on the fly in a live server with no downtime. A nice playground. :-)

Git deployment via Eb

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.UsingAEB.html#GettingStarted.UsingAEB.cli

Just useful links to getting started with Eb, operations and Getting Set Up.

Getting Started with eb (the CLI tool)

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-reference-get-started.html

Or, "deploying an empty application"

Important reading before start:

I highly recommend you to create a brand new virtualenv to play with eb in this study phase. And, working with virtualenv, enjoy virtualenvwrapper, too. We'll use both of them here.

Starting with eb

To start, follow these steps:

  1. Download eb from AWS Elastic Beanstalk Command Line Tool Download page.

  2. Unzip it to your ~/bin directory.

    The unzip process will create the directory AWS-ElasticBeanstalk-CLI-2.6.0 (or something similar, if you downloaded another version) inside your ~/bin.

  3. mkproject aws_eb

    This will create your virtualenv project for this exercise.

  4. Edit your $VIRTUAL_ENV/bin/postactivate file and append this line to it:

    export PATH=~/bin/AWS-ElasticBeanstalk-CLI-2.6.0/eb/linux/python2.7:$PATH

    Important: I'm considering you're using Linux and Python 2.7. If you're using Mac OS, change the "linux" part to "mac". If you're using Python 3, change the "python2.7" part to "python3".

  5. deactivate

  6. workon aws_eb

    Deactivate and activate it again to get the new path.

  7. git init .

  8. eb init

    Answer questions following the tutorial to initialize your eb settings (local and remote).

    Note: I didn't choose to create a RDS instance.

  9. eb start

    Yes, you're right, we are deploying an empty application with an empty git repo!

    In this case, AWS configures the same "Sample Application" we used in the previous Getting Started Using AWS Elastic Beanstalk (the web console tool) section.

    Note: this step can take some time and you'll have to wait.

If everything went OK, you see the last INFO message showing something simmilar to this:

2014-03-18 22:28:20     INFO    Successfully launched environment: HelloWorld-env
Application is available at "HelloWorld-env-tjehzq4giw.elasticbeanstalk.com".

Now you can copy & paste the URL shown. That is your application URL and you have a running Python application, the same way we had in the console example.

If you go into your AWS EB console, you'll see your application and your environment there.

Congratulations, mission accomplished. \o/

Note to self: an environment is what AWS calls "a deployed application".

Important commands here:

  • eb init: initializes your configs for an application.
  • eb start: deploys an application (creates the environment and associated resources).
  • eb stop: deletes an environment (delete load balancer, security group and auto scaling group and stop EC2 instances allocated)
  • eb delete: deletes the application.

Deploying a Git Branch to a Specific Environment

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-reference-branch-environment.html

Or "really deploying my code".

  1. Create a branch of your repo, let's say "testing_branch".

  2. Insert some code to this branch and commit.

    When you deploy a new branch, you'll be deploying a new application version.

    Tip 1: If you're a kind of lost, like I was when playing with this step, you'll like to get the "second application version" you downloaded when practicing the AWS EB web console, unzip it (it's just one python file), change the H1 tag with your name, for instance, and commit it.

    Tip 2: The commit message will become the new version description in AWS EB web console.

  3. Map your current branch to a specific environment.

    $ eb branch
    

    In this step you'll be asked some questions. Answer them.

    Tip: probably you'll have to name an environment. Environment names must follow these rules: they "must contain only letters, digits, and the dash character and may not start or end with a dash". So, create something like testing-env.

    If you just created a new environment, you'll need to eb start it before going on. Remind to copy it's url shown in last line of INFO log.

  4. Deploy your branch.

    $ git aws.push
    

Done. :-)

Now, if you go to AWS EB web console, you'll see 2 environments. Also, you'll see 2 versions in application versions page. Both of them deployed. \o/

Important: It may occur when you go to AWS EB web console, your see the new environment in gray. It's because your new application version is still being deployed. Wait a little and it'll be green.

You can, optionally, use git aws.push --environment <some_other_env_name>. In this case you'll be deploying current branch to another environment, instead of that environment configured in previous eb branch.

A practical taste

Now we know how to deploy different versions through command line. So, it's show time. :-)

Let's deploy a new app version to other environment.

  1. git checkout master

    Enter into master branch.

  2. git checkout testing_branch -- application.py

    Copy your application.py from branch testing_branch to master.

  3. Edit your application.py and change the word "second" to "FIRST". Save it.

  4. git add application.py

  5. git commit -m "A new first version"

  6. git aws.push --environment testing-env

    You'll see a message in your terminal telling you this version was uploaded and environment update was forced. That's what we expect.

    Enter in your AWS EB web console to see the environment being updated (gray color, remember?) and wait until it's green.

  7. Go to your testing environment URL. You'll see the page you just pushed.

Now, you can restore your testing environment version, checking out your testing_branch and issuing a git aws.push.

Deploying AWS Elastic Beanstalk Applications in Python Using Eb and Git

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python.html

@royalsflush
Copy link

Just finished – great tutorial, even though I'd leave the virtualenvwrapper out of it for simplicity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment