April 4, 2016


Automating your VPC with Terraform and Jenkins

Consulting & Advisory Digital Transformation Software Development

In DevOps, they say once you have to complete a task more than once manually, it’s time to automate the process. One of the tasks we do in AWS is duplicate environments for Dev, QA, Staging, and Production. So it’s time to create some templates and automate the process with Terraform and Jenkins.  This will allow us to implement push-button creation of our infrastructure as needed, on demand, with thoroughly tested code that is predictable, efficient, and reliable.

First, we need to decide how we are going to separate environments in AWS. In my experience, the best way to do so is to use different VPCs for each special use case:

  •         Infrastructure – VPC
  •         Production – VPC
  •         Development – VPC
  •         Staging – VPC
  •         QA – VPC

The first VPC we want to create is for our company infrastructure. We want to model our current legacy/bare metal or VMWare environment to fit our new cloud infrastructure as closely as possible while upgrading and improving as needed. This means we will have a mixture of Windows and Linux servers, so plan accordingly. Now is a great time to plan for OS upgrades and security update automation. I mean, who doesn’t like a Greenfield Project with the flexibility to fix all the mistakes of one’s current infrastructure environment? One thing to keep in mind is to use replication and/or master slave setups so that your local office does not go down if the link to AWS goes down. After all, it’s way up in the cloud 🙂

Now, let’s set up our initial Terraform script to create our Infrastructure VPC. We are going to use the AWS stack from segment.io for all our Terraform automation because it is thoroughly tested code and works out of the box. This is a good place to start learning Terraform if you have never used it. All you have to do is put in a default setting for most of the configuration files, and you won’t have to import an output file from the interactive input. So we want to go over to Github and clone the segment.io AWS Terraform stack so we can customize it to fit our needs. You can always fork it and point the scripts to pull from your own repository. Oh, and don’t make the rookie mistake of pushing your AWS keys up to Github or you will be hosting a botnet in a matter of minutes. It’s best to put your AWS keys in your ~/.bash_profile as environment variables and use them that way.

https://github.com/segmentio/stack

When setting up the Infrastructure VPC, public and private subnets, NAT gateway, initial security groups, and basic routes, keep our current network subnets in mind so we do not overlap networks, because that will break connectivity to the network subnets. Remember that AWS provides multiple ways to connect to the cloud network, like direct connect or VPN. In most enterprise networks, you would have multiple private subnets, and our VPC would have additional private or public subnets that would extend that network.

We also want to decide what region to designate for the Infrastructure VPC. We can duplicate it in multiple regions for HA/DR and can load balance between multiple availability zones as well. Replication and backups will have to be planned accordingly because AWS does not provide this capability out of the box, unless you’re using RDS or some other managed Infrastructure.

Make sure you plan the security for future support of our infrastructure environment by creating different security keys to access the servers. We can stage the security keys on the Jenkins server so that we can use them for deployments, connect to needed resources, and upload them to other regions if necessary.  Just make sure you lock down the files and folders on the Jenkins server so only root and the Jenkins user have access in case the server is compromised.  When creating the first Terraform script for your Infrastructure VPC, you may have to refactor your code a few times until you get it perfect. We will be keeping the Terraform state file in an S3 Bucket in AWS, so our scripts will always have access to the current objects and names in AWS.

The VPC Terraform file from segment.io only creates the base network plumbing, which is what we want so we can duplicate the environment on the most basic level and modularize the build as needed. After the initial plumbing is in, we can start building infrastructure servers. The first server you want to build is your Configuration Management server. Many DevOps Engineers automate the build of as many infrastructure servers as possible because it is predictable and reliable. After your Configuration Management server is set up and running, start downloading cookbooks, recipes, and playbooks to automate the set up of the rest of your servers.

My recommendation is to start with a t2.medium and scale as needed. Here is an easy way to create instances with Terraform and script the install of your configuration management server. If the infrastructure server uses a shared database or backend and separate frontend web servers, you can easily create multiple frontend web servers and put them behind an ELB for HA. Graylog is one server that allows you to easily complete this task.

 

 

We will be using Route 53 for local DNS resolution and standard DHCP from AWS. After you create a local, private DNS zone in Route 53, make sure you switch out the DHCP profile so that your instances get the correct DNS information of your new zone. Here is how to create records with Terraform:

 

 

If you’re currently using a Linux-only infrastructure, you will want to set up an LDAP server, Configuration Management Server, Email server, OpenVPN (unless you have direct connect), File Server, Central Logging server like Graylog, Monitoring Server like Sensu, Prometheus, or Nagios, and whatever else you currently use in your bare metal/VMWare environment.

If Windows is your main infrastructure platform, you will probably have a few DNS Servers, Domain Controllers, an Anti-virus Management Console, maybe SCCM/SCOM, SQL, WSUS or Windows Update Server, and Web Servers for different services.

I won’t get into how to provision those infrastructure servers because you have so many options, but for those of you who have never done this task, here are a few suggestions:

  1. Use Terraform to create the instance. Then use provisioning script to kick off Chef Cookbook/Recipe.
  2. Create an AMI image of the pre-configured server. Then use provisioning script to incorporate the AMI image for the server template.
  3. Create Docker images or deploy preconfigured Docker image.

After you have created your Terraform Infrastructure VPC, we want to automate with Jenkins. We will give the user three options:  Plan (test the code), apply (add or update infrastructure), or destroy (start over). In order to allow multiple users to add or update infrastructure, we will use an S3 Bucket to store our Terraform state file. The state file holds the current objects and names of each AWS resource.

Here is how you set up Jenkins to run the Terraform script while creating an S3 Bucket on demand for our state file. Make sure you have the proper permissions so this process will be successful.

Here is what you need to automate the state file updates to an S3 Bucket. This pushes and pulls your state file from the current workspace in Jenkins.

 

 

When you have your Infrastructure VPC completed and scripted in Jenkins, you can change the region and availability zones to make another copy in another region for HA/DR.

In my next blog post, I will show you how to create the Terraform scripts to automate your DevOps Pipeline with Jenkins so that your environments are identical, and you can move code to another environment when necessary.

Consulting & Advisory Digital Transformation Software Development

Latest Insights in Consulting & Advisory

The Rise of Kotlin – Moving Away from Java for Android Development

Kotlin is a programming language for the Java Virtual Machine that’s able to be used in any scenarios that currently…

Introducing our Sphere Heroes Program – Artem Korenev – Employee of the month

At Sphere, employee recognition is a key component of our corporate culture. We believe in celebrating the successes of our…

Write For Sphere

Are you a writer with tech expertise? Then we want to hear from you! Here are a few guidelines for…

View All Articles arrow

We are here to help:

checkmarkto become a customer checkmarkto become an investor checkmarkto send a media inquiry checkmarkto join our team checkmarkto simply say ‘hi’
Get in Touch