How to deploy to Elastic Beanstalk (Part 2)

- By Abhijit Kini on September 19, 2016

In the previous part, we went over the steps of source code deployment to AWS Elastic Beanstalk using a simple Node.js app. We deployed the source code natively at first, then compared with deploying it through Shippable. The latter approach showed actions in the work flow executed automatically for you, by Shippable's unified CI/CD platform.

I'll take a similar approach for this part where we'll go through a deployment of a Docker container of a Node.js app to AWS Elastic Beanstalk. To fully understand this tutorial, complete the previous source code deployment to AWS Elastic Beanstalk first.

We'll first configure a simple Node.js application, manually build and push a Docker image with a tag, and deploy it to AWS Elastic Beanstalk. Next we'll configure Shippable's continuous deployment platform, to automatically execute several actions for you in the background, update and deploy the Docker image on AWS Elastic Beanstalk. The tutorial work flow is shown below:

Authenticates into the Elastic Beanstalk console using the right credentials

Zips your application files

Uploads the artifacts to Amazon S3

Updates the application version

Issues the Deploy command to update the Elastic Beanstalk from the right directory

In order for Shippable to perform the above actions, you'll need to configure Shippable for a successful deployment to Elastic Beanstalk.

It is important that you complete the Docker deployment of the Elastic Beanstalk natively and then configure Shippable to automate tasks to update your application. This approach will give you a good understanding of the clear delineation of what Shippable does automatically behind the scenes and what occurs outside of that context.

Clone your GitHub repository locally by running the following command:git clone git@github.com:abhijitkini/sample_node_eb_docker.git --branch tutorial --single-branch NodeJSDockerEBwhere github@github.com:abhijitkini/sample_node_eb_docker should be replaced by the fork you created in the previous step.

Configure Elastic Beanstalk

Create a Dockerfile in the root of the repository (For this tutorial, it has already been created for you and exists in the root of the forked repository)

Create a Dockerrun.aws.json in the root of the repository (You'll need to create this file in the repo. This file is the configuration to deploy a Docker container from an existing Docker image to Elastic Beanstalk.)

Wait for few minutes until the environment has been created and you are back to the prompt in your Terminal.

You will see the NodeJSDockerEB-app-dev running in the Elastic Beanstalk AWS console.

Create a .ebignorefile at the root of your repository and add the code below to the file. This ensures that Elastic Beanstalk uploads only the Dockerrun.aws.jsonfile and ignores files with .ebextensions.

*
!Dockerrun.aws.json
!.ebextensions/*

Ensure the .gitignorefile at the root of the repository has the entries shown below. This ensures everything in .elasticbeanstalk folder will be ignored, except the files ending in cfg.ymland global.yml

Build Docker Image and push to DockerHub

In your Terminal, from the NodeJSDockerEBdirectory, run the following command to build the Docker image with a tag.docker build --rm=true -t abhijitkini/sample_node_eb_docker:nativeEBDeployment .

Depending on the size of the image and your internet connection, this step could take a few minutes.

Next, push this image to Docker Hub by running the command:docker push abhijitkini/sample_node_eb_docker:nativeEBDeployment

Similarly, this step can take a few minutes to complete successfully.

Upon completion, verify if the image and tag have been updated on your Docker Hub.

For more information on building an image and pushing it to DockerHub, refer the Docker tutorials.

Deploy and View the Application

Run theeb deploycommand to deploy the application to Elastic Beanstalk.

This process may take several minutes to complete. Elastic Beanstalk will provide status updates during the process. Once done, you'll see a similar pictures shown below.

Notice the application version and the successful pulling of Docker image and tag specified in the AWS Elastic Beanstalk console.

You can also check the image and tag updated on your Docker Hub.

NOTE: It is important to understand that the above command will provision Elastic Beanstalk resources on AWS and you may incur charges.

From the NodeJSDockerEB directory, run the eb open command to view the application in your browser. You'll see the Shippable Mascot with the message "Congratulations Captain! You have successfully deployed Docker image of this NodeJS test app to Elastic Beanstalk".

Remember, any changes you make to your application, you'll need to go through all these steps to deploy the same successfully on Elastic Beanstalk.

Configure your Shippable environement

Configure Docker and AWS Integrations. Follow these steps:

Access your subscription and go to the 'Settings' tab

On the 'Integrations' tab, click 'Add Integration'

Give a name to your Docker Integration and select 'Docker Hub' from the drop down list of 'Account Integrations'.

This will take you to the sample_node_eb_docker project page. You will see a 'master' and a 'tutorial' branch. We are working off the 'tutorial' branch.

NOTE: If you trigger the build manually at this point, by clicking the 'Build' button, it will fail with the error "failed to find shippable yml file". The reason is because we have not completed the needed configurations.

Configure the shippable.yml file in your local NodeJSDockerEB directory.

For this tutorial, populate the simplified shippable.yml with the syntax given below:

Push the changes in steps 3 and 4 to your GitHub repository by running the following commands in your Terminal from your local NodeJSDockerEB directory:

git add .

git commit -m "added shippable yml and modified dockerrun files"

git push origin tutorial

As mentioned above, we are working on the 'tutorial' branch of the 'master'. This action will trigger continuous integration for Elastic Beanstalk within Shippable and successfully deploy your application to Elastic Beanstalk.

Let us now verify the above step once the build completes successfully in Shippable:

In your local machine Terminal & within the NodeJSDockerEB directory, run the eb status command.

Pay particular attention to Deployed Version field. It is shippable.NodeJSDockerEB-app-dev.tutorial.1

Then, check out the updated image and tag on Docker Hub and you'll see the tutorial.1 image and tag

Next, let's look at the deployed version in the Shippable console logs for this build in the aws_eb_deploy > eb deploysection. It is shippable.NodeJSDockerEB-app-dev.tutorial.1

Lastly, take a look in the AWS Elastic Beanstalk console. It is shippable.NodeJSDockerEB-app-dev.tutorial.1

Bada Bing Bada Boom!

We have successfully configured Shippable to automate several actions in the Docker deployment to AWS Elastic Beanstalk.

Shippable automates Elastic Beanstalk deployment

Now that we have configured Shippable to automate tasks in the Docker deployment to Elastic Beanstalk, update the application & watch Shippable's automation deploy the update and make it available to your end users.

In your local NodeJSDockerEB directory, go into the 'views' folder & open 'index.ejs' file.

At the end of the sentence 'You have successfully deployed this NodeJS test app to AWS Elastic Beanstalk', add "using Shippable"

Authenticates into the Elastic Beanstalk console using the AWS credentials you have configured.

Zips your application files in your repository.

Uploads the zipped files to Amazon S3 for Elastic Beanstalk to access.

Updates the application version

Issues the Deploy command on Elastic Beanstalk from the right directory.

Once the build on Shippable is successfully complete, let's verify if the end users can see the update we made.

In your Terminal, within the NodeJSDockerEB directory, execute the eb open command.

The web page is updated to show the below picture

Congratulations!

You have successfully enabled Shippable to automate repetitive developer tasks and update your Node.js application to deploy to Elastic Beanstalk, with Docker, as a frictionless deployment. You've seen the comparison of deploying it natively and using the Shippable platform.

Depending on your automated tests and deployments scenario, you can perform customization by configuring Elastic Beanstalk directly, outside of Shippable. Shippable will continue to automatically trigger deployments into Elastic Beanstalk upon a successful Shippable CI build and help you ship code faster.

If you have questions or feedback on this topic, reach out to us through our support channel.