In a previous post we discovered how can we run commands on each push and check if the test is passing against multiple versions of go. This is a continuation of the same concept. We are going to deploy our code to Elastic Beanstalk, straight from git push.
Elastic Beanstalk is nothing but a wrapper around it’s EC2 infrastructure. It is a developer-centric view of deploying an application on AWS. Developers just have to push the code and Beanstalk can take care of Auto Scaling Group, Elastic Load Balancer, databases etc. No need to manage infrastructure manually.
Here is the outline of the post:
- Prerequisites
- Create an app on Elastic Beanstalk
- Obtain the access and secret key from AWS Console
- Create an HTTP Server
- Setup .travis.yml
Please connect with me on LinkedIn.
Prerequisites
I will assume that:
- You have a working knowledge of git and TDD.
- You have an AWS account.
- You are hosting your code on GitHub, and Travis is enabled on repo.
This setup must work on other providers, but I haven’t got time to test it. Let me know in the comments if you did it yourself and want to share it.
Create an app on Elastic Beanstalk
Before we automate the process of deployment, we must have an already running application on Beanstalk. If you don’t have one, head over to https://console.aws.amazon.com/elasticbeanstalk/home and look for a button saying Create a new application.
Select Docker for platform.
This will create an app, an environment, and an S3 bucket where the code well be uploaded.
Here is my already working instance of unittest application which we’ll be automating the CD workflow to.
Get the access and secret key from IAM Console
At this stage, you might want to create a user for this app and attach AWSElasticBeanstalkFullAccess
policy to the user. But I am going to use my master account.
You can generate Access key ID and Secret access key by heading over to https://console.aws.amazon.com/iam/home?#/security_credentials.
You access key ID will look something like AKIABOOMLWMXVM5DUA4G
and secret access key MdrNmw6Ub+vHp2cBZKZLBbtY54XaAMufNnfW1Rz5
. Both will be needed by Travis to access your AWS programatically.
Let’s create a simple app
main.go
file with driver code:
|
|
main_test.go
file with test:
|
|
While this is a simple server. I’ll do a tutorial on multi-container deployment when I’m upto it.
Dockerfile
Dockerfile for our repo:
|
|
Tune up the .travis.yml
Add deploy section to .travis.yml
:
|
|
As you can see, we haven’t put access key and secret key directly. Travis provides a way to pass environment variables to running jobs. You can find it in the project settings. Below I have set both those variables.
Set the region in which you have created the application. Fill in the app, env and bucket_name appropriately.
End of the blog post. Please list your queries in comments. Also, subscribe to the newsletter for updates.