CI/CD with Travis CI & Terraform

Emile Bronkhorst
Javarevisited
Published in
5 min readJun 1, 2020

Everything you need to setup a CI/CD pipeline for FREE with TravisCI and Terraform to automate the creation of your infrastructure

Image by Yancy Min on Unsplash

What is CI/CD

Continuous Integration (CI) & Continuous Deployment (CD) is a set of operational principles (most popular in Agile methodologies) that allow you to deliver code changes more frequently and reliably without the need for additional interaction. What this means for development teams is that it allows you to focus on providing business value rather than focussing on the operational parts of the software lifecycle.

The process in a nut shell:

CI: Build -> Package -> Test

CD: Deploy output from CI across all environments

Authorising Travis CI to GitHub

First things first, head over to Travis CI and click on the SIGN IN WITH GITHUB button (note: your repository must be public for you to use this service). You should be presented with an authorisation screen asking you to give Travis access to read data form your GitHub account

Authorise Travis CI

Once you have authorised Travis, you will be taken to your Dashboard and you can begin connecting your repositories.

Connecting your repositories

At this point you will need an AWS IAM user setup with programmatic access and the associated credentials. If you haven’t got those, check out last weeks tutorial on how to Setup Terraform with AWS.

On the left most side of the Travis Dashboard you will see a My Repositories pane with a + button, click it to add a new repository

Add a new repository

If your repository doesn’t show up in the list (now or in future), you might have to do a one-off sync to get it added

Sync your GitHub repositories

Finally, we have to add our AWS IAM Credentials to the builds environment variables. Select your repository from your Dashboard -> More options -> Settings

Build settings

Scroll down to the Environment Variables section and add your Access/Secret Key here (note: keys must match the below names)

Add AWS Secret/Access key

Adding Travis YML Config

Travis gives us the ability to define your pipeline using YML config so that you never have to worry about dealing with manual interactions again. This gives you the flexibility to define a new pipeline for every repository you own through code as part of your development process (DevOps)

Create a new file in the root of your repository called .travis.yml and populate it with the following (full list of commands can be found here)

.travis.yml

Now that we have our YML in place, lets push our code to GitHub and then head over to start creating our Pull Request (PR)

git add . 
git commit -m "Initial commit of Travis CI configuration"
git push -u origin feature/travis-ci-build

Creating a Pull Request

In the GitHub UI for your repository click on Compare & pull request

Open a new PR

It should automatically default to the branch which you have most recently committed but in the event that it doesn’t, go ahead and select it from the drop down. Next, Give your PR a title, description and then click the Create pull request button.

Create the PR

Once the Pull Request is created, GitHub will send a notification to Travis which will trigger a new build step as per our YML definition.

CI Merge Check

You can see above that the merge will only complete if our CI process has validated a successful build. This stops you from merging code that doesn’t work in production systems — so it’s good practice to get used to this style of handling pull requests. If you head over to the Travis Dashboard now you will see your build running and you can then follow the output in the logs!

Deploying to AWS

So we have defined a build plan that we successfully setup to run when we create a new PR. Once you have validated the output of Terraform’s Plan stage and are happy with the changes that are going to be applied… Click the Merge pull request button.

Merge PR

Now remember, we only wanted to execute the Plan phase for PR’s in our build pipeline and the Apply phase comes when we merge. So head over to your Travis Dashboard again and you should see it executing another build but this time actually deploying the changes!

Succesful deploy to AWS

Conclusion

Now that you have integrated your repositories, you can do so for every project you start! You can already see how much time it will save you in the future building, testing and deploying your code, so try to make it part of your development lifecycle from here on.

Let me know what you thought of this style of tutorial in the comments below and please do give suggestions on any improvements that can be made for future tutorials. See you next week!

Resources

Travis CI: https://travis-ci.com/signin

Travis Build Commands: https://docs.travis-ci.com/user/customizing-the-build

Setting up Terraform with AWS: https://medium.com/startupchronicles/setting-up-terraform-with-amazon-web-services-aws-afb2fde6cf18

Travis YML Config: https://gist.github.com/startupchronicles/9f1dd05ebb5f37e30075de2094cf53ae

--

--

Emile Bronkhorst
Javarevisited

Tech enthusiast — specialising in Cloud Architecture, Microservices, Distributed Systems & Digital Transformations