Skip to content

CircleCI 2.0 Beanstalk Deployment example

Full code sample is now available on GitHub:

https://github.com/kgoedecke/circleci-beanstalk-example

There are lot of tutorials out there that show how to create a pipeline to deploy apps on AWS Beanstalk using CircleCI but they were either for Version 1 or simply didn’t work because incorrectly set AWS credentials. That’s why I decided to write a tutorial which has all the steps needed to automatically deploy to AWS Elastic Beanstalk.

Beanstalk is an amazing service to deploy and scale web apps and services written in Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker.

I find it super convenient, with our setup I can simply push my code into a repository and CircleCI deploys to Beanstalk which then takes care of the provisioning of the underlying EC2 instances.

In this tutorial we are going to deploy a small dummy NodeJS application using Github and CircleCI 2.0.

Overview of the tutorial:

1. Setup – We are going to create an AWS IAM user which will be used later with CircleCI to handle Beanstalk
2. Add Credentials to your local AWS EB CLI to spin up the Beanstalk environment
3. Beanstalk Environment – Deployment of the Beanstalk Environment
4. Connecting your Git Repository to CircleCI
5. Connect AWS to CircleCI using IAM Credentials
6. Create the CircleCI Config in the Repository
7. Happy Deploying

Requirement:

  • Python and PIP installed
  • AWS Account with Admin Access to generate API credentials
  • CircleCI account setup and linked to a GitHub or Bitbucket Repository

Step 1: Setup IAM User

Before starting to use CircleCI to deploy apps to Beanstalk, we need to generate API credentials that will then be used by CircleCI to execute the necessary commands. In order to generate the API credentials, sign into your AWS Management Console and go to “Identity and Access Management – IAM”:

Then, click on “Add user” :

circleci beanstalk

Assign a username for the new account and select “Programmatic access“:

circleci beanstalk

Add “AmazonElasticBeanstalkFullAccess” to user permissions list, this is whats needed for CircleCI to deploy to Beanstalk.

circleci beanstalk

Next, click on “Review” then “Create User”:

circleci beanstalk

You’ll see your Access key ID and your Secret access Key. Both will be used later to trigger the Beanstalk commands from CircleCI.

Step 2: Add AWS Credentials locally

Using the AWS CLI locally we will spin up the Beanstalk environment that will be later used by CircleCI to deploy to. In order to use AWS CLI or EB CLI on your local computer we first need to create a credentials file ( ~/.aws/credentials ) and store the IAM user credentials in it.

Edit  ~/.aws/credentials  and add the following content (replace YOUR_ACCESS_KEY_ID and YOUR_SECRET_ACCESS_KEY with the credentials generated in the previous step):

Step 3:  Install EB CLI

In this section, we are going to install AWS EB CLI.

This step requires Python and pip installed locally. If you have everything ready execute the following command:

After you’ve installed the EB CLI, add the path to the executable file to your PATH variable.

Step 4: Create your Elastic Beanstalk environment

It’s time to create your Elastic Beanstalk Environment using the EB CLI from your local machine. Just navigate to your app directory (that needs to be deployed to Beanstalk later) and execute eb init  to initialize the directory as a Beanstalk application. NOTE: Alternatively you also use the AWS Management dashboard to create your Elastic Beanstalk environment.

Change into the directory of your app:

Initialize the app as a Beanstalk application:

This will initialize your application and the environment, you’ll notice that the command has created a .elasticbeanstalk  folder in your application. Afterwards we can go ahead and actually spin up the enviroment.

Execute the following command, make sure you use eb-cli  as the profile, because thats how we named our credentials in the ~/.aws/credentials  file

Step 4: Connect Repo with CircleCI

Now that our application environment is up and running (please verify that it actually is using the AWS Management Dashboard), we can go ahead and connect CircleCI to Beanstalk.

CircleCI needs a repository to pull the code from. Currently CircleCI supports GitHub and BitBucket. Go to https://circleci.com/signup/

and Click on Signup with Github or Bitbucket. You should login into Github and Bitbucket before this.

circleci beanstalk

Step 5: Add IAM user Credentials in CircleCI

After you’ve linked your repository to your CircleCI account, head over to the CircleCI Dashboard and navigate into your project. Click on the settings icon of the repository that you want to setup.

circleci beanstalk

In the Settings section scroll down and click on “AWS Permissions” as shown in the screenshot below. Insert your access and secret key of the “circleci” programmatic user that we created previously using the AWS Dashboard.

circleci beanstalk

Step 6: Create CircleCI config

We are going to use CircleCI version 2.0 because CircleCI is officially discontinuing support for CircleCI 1.0 builds after August 31st, 2018.

Create  .circleci/config.yml  inside the root directory of your application and add it to your repository. CircleCI automatically sets two environment variables ( $AWS_ACCESS_KEY_ID  and $AWS_SECRET_ACCESS_KEY ). We will use those variables to configure the AWS EB CLI.

Let’s discuss the importance of the CircleCI config and also look into the run  steps that we’ve configured.

In this step we are manually populate the AWS credentials from the environment variables that CircleCI sets. This is necessary because the AWS EB CLI is not able to use the environment variables directly. Instead it needs them to be set in the config file ( ~/.aws/config ).

The above step tells CircleCI to use the environment named circleci-beanstalk-example with the credentials profile named eb-cli, which we’ve defined in the config file. You might replace the name of the environment with the one of your application.

After we’ve successfully created the CircleCI config file, commit the file and push it to your repository.

Go back to the CircleCI Dashboard and you should see your code building and deploying automatically to AWS ElasticBeanstalk.

circleci beanstalk

Within the AWS Dashboard you should see the deployment process. After a little while the Health status should switch to “Ok”.

circleci beanstalk

 

Every time code is pushed to the master  branch now CircleCI will deploy automatically to Beanstalk.

If you run into any issues please feel free to comment below.

In the next tutorial I’ll show you how to deploy to different environments for a staging and production environment using different branches (develop / master).

 

Published inDevOps

8 Comments

  1. This was very helpful.

    Perhaps a final step on how to `curl` the API?

    Thanks again for putting this together.

  2. Alok Patra

    Great post!
    Crisp and just the required details to get started.
    Thanks

  3. Nate

    Hi Kevin… Thanks for this, this will get me started…

    Any news on the staging / product update to this tutorial?

    Cheers..

  4. Paulo Henrique

    Ohh Tks man!! You save my life! haha! I was seeking exactly this!! I finded others examples that was created a zip file of application and zip file was send to ELB

  5. Amit

    #!/bin/bash -eo pipefail
    eb use circleci-beanstalk-example –profile eb-cli
    eb deploy -v –staged –profile eb-cli
    ERROR: The EB CLI cannot find a default region. Run “eb init” or use a specific region by including the “–region” option with the command.
    Exited with code 3

  6. Anonymous

    can anyone please suggest solution for above error .

  7. amit

    i have solved above problem but still getting different issue .

    #!/bin/bash -eo pipefail
    eb use circleci-beanstalk-example-dev –region us-west-2 –profile eb-cli
    eb deploy -v –staged –profile eb-cli
    ERROR: This directory has not been set up with the EB CLI
    You must first run “eb init”.
    Exited with code 126

    • Huey Ly

      > re: You must first run “eb init”. error

      Took me a while but figured out a work around, hope this helps somebody.

      If you follow just the example in the post you will get the above error (you must run eb init) if the file .elasticbeanstalk/config.yml doesn’t exist (not committed to your repo). If you do not want to commit this file to your repo (so that each dev can have their own eb configuration), here is the workaround.

      1. Create a file call ebconfig.yml somewhere in your repo and commit that instead. I put it it .circleci/ebconfig.yml

      2. Copy the content if the .elasticbeanstalk/config.yml file (generated after you ran eb init locally) into ebconfig.yml. For example, mine looks like this:

      “`
      branch-defaults:
      develop:
      environment: my-example-app-dev
      master:
      environment: my-example-app-prod
      global:
      application_name: my-example-app
      default_ec2_keyname: app_ebkey
      default_platform: 64bit Amazon Linux 2017.09 v2.6.6 running PHP 7.0
      default_region: us-west-2
      profile: aws-user
      sc: git
      “`

      3. Modify .circleci/config.yml to cp that file to .elasticbeanstalk/config.yml during deployment by adding these lines in the run command:

      “`
      mkdir ~/repo/.elasticbeanstalk
      cp ~/repo/.circleci/ebconfig.yml ~/repo/.elasticbeanstalk/config.yml
      “`

      For example, my .circleci/config.yml now look like this:

      “`
      version: 2
      jobs:
      deploy:
      docker:
      – image: circleci/python:2.7
      working_directory: ~/repo
      steps:
      – checkout
      – run:
      name: Install awscli
      command: |
      sudo pip install awsebcli –upgrade
      – run:
      name: Create AWS credentials manually
      command: |
      mkdir ~/.aws
      touch ~/.aws/config
      chmod 600 ~/.aws/config
      echo “[profile eb-cli]” > ~/.aws/config
      echo “aws_access_key_id=$AWS_ACCESS_KEY_ID” >> ~/.aws/config
      echo “aws_secret_access_key=$AWS_SECRET_ACCESS_KEY” >> ~/.aws/config

      mkdir ~/repo/.elasticbeanstalk
      cp ~/repo/.circleci/ebconfig.yml ~/repo/.elasticbeanstalk/config.yml
      – run:
      name: Deploy to EB if branch is develop
      command: |
      eb use my-example-app-dev –profile eb-cli
      eb deploy -v –staged –profile eb-cli

      workflows:
      version: 2
      build-deploy:
      jobs:
      – deploy:
      filters:
      branches:
      only:
      – develop
      “`

      Good luck!

Leave a Reply

Your email address will not be published.