Part 1: Introduction to Infrastructure as Code (IAC) and Terraform
Welcome to a series on using terraform for Infrastructure as Code (IAC) with TeamCity and Octopus to create a CI/CD pipeline for deploying infrastructure into Azure.
In this series, we will explore how to use terraform to manage infrastructure as code, TeamCity for continuous integration, and Octopus Deploy for continuous delivery to automate the deployment of resources in Azure.
Whether you are new to infrastructure as code or an experienced practitioner, this series will guide you through the process of building a robust and automated deployment pipeline using these powerful tools.
So, let's dive in and explore the world of IAC with terraform, TeamCity, and Octopus Deploy.
In this first part, we will introduce you to the concept of IAC and Terraform.
What is IAC?
Infrastructure as code (IAC) is the practice of managing infrastructure in a declarative manner using code, rather than through manual configuration. This approach brings many benefits, including improved efficiency, greater consistency, and better collaboration across teams.
If you are using Azure you are probably familiar with Azure Resource Manager (ARM) templates. Most people probably start the IAC journey with ARM templates but if you are working with a large enterprise level deployment you will quickly realise they become difficult to manage and deploy consistently. Even when used in conjunction with Octopus Deploy you can often get into a situation where deployment paralysis sets in and no-one feels comfortable pushing that big red button because they are not 100% sure what will happen.
This “configuration drift” happens when infrastructure deployments are not done in an automated way. Engineers may be putting their hands on Azure resources directly or maybe special “pet” configurations exist in production for various reasons.
Infrastructure deployments should be triggered on a schedule rather than synchronised with software releases or done once and forgotten. My preference is to trigger an infrastructure deployment on a nightly schedule and if everything is setup correctly there will be no impact or outage on the target environment, and it becomes a simple and predictable process allowing incremental changes without boiling the ocean every time we need get a change into production.
In other words, “Let the robots do the work!”. I will cover this in detail in future parts of this series.
So IAC is more than just defining your configuration in code it is also about limiting configuration drift in your deployed environments and should encapsulate everything from using source code management, CI/CD and scheduling deployments.
What is Terraform?
Terraform is an open-source tool for building, changing, and versioning infrastructure. It provides a simple and intuitive way to manage infrastructure as code, using a high-level configuration language called HCL (HashiCorp Configuration Language). With Terraform, you can easily define and provision infrastructure resources such as virtual machines, networks, storage, and more, across multiple cloud providers and on-premises environments.
Why use Terraform for IAC?
Terraform is a powerful tool that allows you to manage your infrastructure as code, regardless of the cloud provider or environment you're working with. It's a popular choice among developers and operations teams for several reasons.
One of the main advantages of Terraform is that it lets you define your infrastructure as code. This means you can easily create consistent environments across your infrastructure without worrying about the specific details. By automating your infrastructure, you can easily scale it up or down as needed, reducing the risk of errors and downtime.
Another great thing about Terraform is that it promotes collaboration among teams. You can use version control to manage changes to your code, and multiple people can work on the same code at the same time. This reduces the risk of conflicts and ensures everyone is working towards the same goal. In a well curated dev-ops culture this helps your development team to think beyond “works on my machine”.
Terraform also makes auditing your infrastructure changes easier. You can track when and by whom changes were made, and you can easily roll back changes if necessary. This is particularly important in enterprise environments where compliance and security are top priorities.
Finally, terraform can help you optimize your infrastructure costs. By automating the management of your resources, you can ensure that you're only paying for what you're using. You can set up rules to automatically shut down resources when they're not needed, reducing unnecessary costs.
Installing and setting up Terraform
So, hopefully I have got you as excited as I am about good IAC practices it’s time to get our hands dirty.
To start using Terraform, you'll need to install it on your machine. Fortunately, Terraform is easy to install and is available on multiple platforms, including Windows, macOS, and Linux.
Head over to https://www.terraform.io/ and select the download button.
As of writing the latest version of Terraform is 1.4.6 so follow the instructions for your particular OS of choice, full instructions here:
https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli
Pro tip: On windows use Chocolatey to avoid mucking about with paths:
choco install terraform
You can create Terraform code in any text editor but my IDE of choice for IAC is Visual Studio Code. You will need it to follow this series so grab Visual Studio Code and install it here if you don’t already: https://code.visualstudio.com/
Create a folder for this series walkthrough and name it iac-terraform-demo.
Open VS Code, select Open Folder and open the iac-terraform-demo folder. Go to File | Save Workspace As and click save to create the new workspace definition.I like to keep my code organised in a specific structure for IAC so I will encourage you to do the same but feel free to modify this to suit your needs later.
Create a new folder in VS Code called service_modules and then create the following files:
- main.tf
- outputs.tf
- variables.tf
- Hashicorp Terraform: This is the official Hashicorp Terraform Extension for Visual Studio Code.
Go to Terminal and select New Terminal. For this series we will use powershell which should be the default option on Windows.
At the cmd prompt enter:
cd .\service_modules\
terraform init
This will initialize terraform for the workspace project and you should see quite a bit of output in the terminal window. If all goes well, you will see this at the end of the output:
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
In the next part of the series, we will dive deeper into Terraform and explore its key features and benefits for managing infrastructure as code.
