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

2 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

Leave a Reply

Your email address will not be published.