Overview

Skill Level: Any Skill Level

Familiarity with Bluemix and AWS

Ansible is a simple, powerful, agentless technology for automating application deployment and configuration management. Learn how to use Ansible from your Bluemix DevOps pipelines to deploy to Bluemix and AWS following this recipe.

Ingredients

This is the second recipe in a series. For the first segment, check the out how to Run Ansible from your IBM Bluemix DevOps Pipelines.

In this tutorial we will use an Ansible Playbook executed from the Bluemix Continuous Delivery service to deploy a sample PHP application. We will automatically create a Delivery Pipeline from the Toolchain. Then we will use Ansible to deploy an application from this Pipeline to Bluemix and AWS Elastic Beanstalk. Ansible Playbooks can be used to deliver simple applications, as in this recipe, as well as complex application infrastructure.

 

Step-by-step

  1. Explore the sample application

    Review the sample source repository. These are the key elements of the repository:

    • index.php – the sample application
    • setup_tools/install_ansible.sh – script to set up¬†Ansible,¬†Boto, and the Ansible Elastic Beanstalk library
      #!/bin/bash
      python --version
      echo "apt-get -qq update"
      sudo apt-get -qq update
      echo "apt-get -qq -y install python-dev libssl-dev libffi-dev"
      sudo apt-get -qq -y install python-dev libssl-dev libffi-dev

      # Boto needed for s3 & elastic-beanstalk
      echo "pip install pycrypto pyyaml boto ansible --quiet"
      sudo pip install pycrypto pyyaml boto ansible --quiet

      echo "apt-get -qq clean"
      sudo apt-get -qq clean

      #Install required library
      echo "git clone -b release-1.0 https://github.com/IBMCloudDevOps/ansible-elastic-beanstalk"
      git clone -b release-1.0 https://github.com/IBMCloudDevOps/ansible-elastic-beanstalk


    • .playbook.yml ‚Äď This playbook defines how we will deploy to both Bluemix and AWS. For Bluemix¬†we upload the code using cf push. For¬†AWS¬†we first zip and upload our code to S3, then we create an Elastic Beanstalk application, link our code zip¬†to our¬†application, and then create the environment on which the application runs. See Amazon’s documentation for information on AWS Elastic Beanstalk,¬†Elastic Beanstalk solution stacks, and AWS Regions.
      # Publish this application to bluemix using ansible
      ---
      - name: Publish the app to AWS
      hosts: localhost
      connection: local
      vars:
      app_name: "{{ lookup('env','APP_NAME') }}"
      s3_bucket: "{{ lookup('env', 'S3_BUCKET') | default(app_name,true) }}"
      region: "{{ lookup('env', 'AWS_DEFAULT_REGION') | default('us-west-2', true) }}"
      solution_stack_name: "64bit Amazon Linux 2016.09 v2.3.1 running PHP 5.5"

      tasks:
      # ------------------------------------------------------------
      # Set Facts
      # ---
      # Store datetime for version info
      - name: Create version string
      set_fact:
      version: "{{ lookup('pipe', 'date +%Y%m%d-%H%M%S%Z') }}"

      # Create information for archive zip
      - name: Create zip information
      set_fact:
      zip_filename: "{{ app_name }}_{{ version }}.zip"
      zip_dir: "{{ lookup('env', 'PWD') }}/s3"

      # ------------------------------------------------------------
      # Bluemix Deploy
      # ---
      - name: CF Push Application
      shell: "cf push -f manifest.yml -u none --hostname {{ app_name }}"

      # ------------------------------------------------------------
      # AWS Deploy
      # ---
      # Create a directory for the archive
      - name: Create Directory
      file:
      path: "{{ zip_dir }}"
      state: directory

      # Create a zip archive (elasticbeanstalk only supports zips and jars currently)
      - name: Create zip archive
      shell: "zip -r {{ zip_dir }}/{{ zip_filename }} . -x s3\\* .bluemix\\* .git\\* ansible-elastic-beanstalk\\* setup_tools\\* .playbook.* .gitignore .cfignore .tarignore _*.sh manifest.yml README.md"

      # ansible s3 docs: http://docs.ansible.com/ansible/s3_module.html
      # you can enable versioning in bucket, set the zip_filename: {{ app_name }}.zip and use version: {{ version }}
      - name: push to s3
      s3:
      mode: put
      region: "{{ region }}"
      bucket: "{{ s3_bucket }}"
      src: "{{ zip_dir }}/{{ zip_filename }}"
      object: "{{ zip_filename }}"

      # ansible elastic beanstalk: https://github.com/hsingh/ansible-elastic-beanstalk
      # Create beanstalk app
      - name: Create Elastic Beanstalk application
      elasticbeanstalk_app:
      region: "{{ region }}"
      app_name: "{{ app_name }}"
      state: present
      register: app_output

      # Connect to s3 bucket version
      - name: Link uploaded version
      elasticbeanstalk_version:
      region: "{{ region }}"
      app_name: "{{ app_name }}"
      version_label: "{{ version }}"
      s3_bucket: "{{ s3_bucket }}"
      s3_key: "{{zip_filename}}"
      state: present
      register: version_output

      # Create the environment
      - name: Create app env
      elasticbeanstalk_env:
      region: "{{ region }}"
      app_name: "{{ app_name }}"
      env_name: "{{ app_name }}-env"
      version_label: "{{ version }}"
      solution_stack_name: "{{ solution_stack_name }}"
      register: env_output
      # ------------------------------------------------------------
    • .bluemix/ – the configureation files needed to create the toolchain and pipeline

    The running application looks like:

    PHPStarter

  2. Create the toolchain

    1. Authenticate to GitHub and Bluemix
    2. Click the Create toolchain button in the repository readme to fork the repo into your GitHub account
      reporeadme
    3. Once the repository is forked, you will be taken to the Bluemix Continuous Delivery toolchain setup. This toolchain has been defined by the template in the sample repository.
      Toolchain Template Page
      • If you have not authenticated to GitHub you will see an Authorize button.
        NotAuthorized
      • You can select the Bluemix organization and space by clicking the Delivery Pipelines button.
        Toolchain Page
    4. Click the Create button. This will generate a toolchain that looks like the following:
      The Generated Toolchain
  3. Configure the stage properties

    1. Select the Delivery Pipeline tile from the toolchain view to open the pipeline stages view.
      Toolchain Pipeline button
    2. The pipeline executes immediately after being created. The Deploy Prod(Bluemix) & Prod(AWS) stage will fail on the first run. The stages view will look like this once the build completes.
      Deployment Failed
    3. Click on the gear at the top right corner of the Deploy Prod(Bluemix) & Prod(AWS) stage to select Configure Stage.
    4. Set the APP_NAME, S3_BUCKET, AWS_SECRET_ACCESS_KEY, and AWS_ACCESS_KEY variables under Environment Properties tab of this stage. The APP_NAME will be used as the hostname in Bluemix, and the application name in AWS. The hostname must be unique across Bluemix. If you have not generated AWS keys before, Amazon has documentation on AWS access keys. This process will create the S3 Bucket for you if it does not exist. Remember the S3 Bucket names must be unique.
      Configure Properties
    5. Run the Deploy Prod(Bluemix) & Prod(AWS) stage using the Run Stage button at the top righthand side of the stage’s card. This time the Deploy Prod(Bluemix) & Prod(AWS) will pass.
      Deployment Passed
  4. View the application in Bluemix

    1. Click the menu on the upper left of the Bluemix interface. Choose Dashboard to display all the applications.

      BMDashboardMenu

    2. The PHP sample application will appear in the Bluemix Apps Dashboard

      BMDashboard

    3. Click the Route link to launch the sample application.

      PHPStarter

  5. View the application in AWS

    1. Log in to the AWS Management Console
    2. Go to Elastic Beanstalk. You should see your application here. If you do not see your application, ensure you are viewing the same region you deployed it to, by default this is us-west-2 (Oregon).
      Elastic Beanstalk Applications Overview
    3. Click on the application name in the green card, this will bring you to the application dashboard.
      Elastic Beanstalk PHP Application
    4. Click the URL at the top of the application’s dashboard to see it running. It will look like this:
      PHPStarter
    5. Navigate to S3 and open the bucket you configured in step 4. You will see the generated source zip. If you do not see your bucket, ensure you are viewing the correct region.
      PHP S3 Bucket
  6. Closing thoughts

    The Bluemix Continuous Delivery service provides a flexible continuous delivery pipeline that can employ a variety of technologies to effect deployments. Ansible provides an open, all purpose deployment and configuration management framework. Together they give you effective continuous delivery using an open platform for deployment. Horizontal scaling is great for load balancing and failover. Deploying to multiple environments using Ansible and adding a load balancer is a good way to acheive this.

     

    Authors

    Jesse Antoszyk is a Software Engineer with IBM Cloud.

    Adam King is a Senior Software Engineer with IBM Cloud.

2 comments on"Deploying to AWS from Bluemix?!"

  1. Doyoung Im March 29, 2017

    Hi,
    This is valuable recipes.

    Most of Devops developers are more using AWS EC2 and AWS ECS instead of Beanstalk…
    I want to be notified when the new posts on this subject~~

Join The Discussion