Infrastructure as code (IaC) is something that gets me excited. Automation has been pushing the industry towards IaC for a while now. Tools like Terraform and Ansible have matured so much that it’s more fun than click-ops systems administration. Self-documenting code that magically controls your infrastructure with nothing more than a single binary and a text file? Sign me up! So what is IaC? IaC refers to the practice of managing your infrastructure using code. IaC sounds simple enough, right? 😅 A variety of tools and techniques manage and deploy IaC, but the high-level steps for creating and deploying infrastructure as code are as follows:

  1. Choose a configuration management tool: Various tools are available for creating infrastructure as code, such as Terraform, CloudFormation, and Ansible. Choose one that meets your needs and is compatible with the infrastructure you want to manage.
  2. Define your infrastructure in code: Use the syntax and conventions of your chosen configuration management tool to define your infrastructure. Your code may include resources such as virtual machines, storage, and networking components.
  3. Version your code: It is important to version your infrastructure code so that you can track changes and roll back if necessary. Use a version control system such as Git to manage your code.
  4. Test your code: Before deploying your infrastructure, it is a good idea to test your code to ensure that it is correct and free of errors. To do this, you can use tools such as ’terraform validate’ and ’terraform plan'.
  5. Deploy your code: Once you have tested and are satisfied with the results, use your configuration management tool to deploy your infrastructure. The deployment may involve creating new resources, updating existing ones, or destroying others.
  6. Monitor and maintain your infrastructure: Ongoing maintenance and monitoring are essential for ensuring that your infrastructure runs smoothly. Use tools such as monitoring and logging to track the performance of your infrastructure and identify any issues that may arise.

Only six steps!? Yep! By following these steps, you will be creating and maintaining your infrastructure using code, making it easier to automate tasks and maintain consistency across your environment. Is it going to be easy? Probably not, but plan well, and hopefully, you’ll have some fun along the way.

Terraform

Terraform is a popular tool for creating infrastructure as code. It is an open-source tool that HashiCorp maintains. Terraform (the binary application) is written in the Go programming language (Something I want to dive into later on) and is available for Windows, macOS, and Linux. In this article, and probably any future IaC articles, we will focus on Terraform. Mileage may vary, but the same principles should apply to other tools such as CloudFormation and Ansible. You’ll just have to use the syntax and conventions of your chosen tool.

Code

I will not be getting into the details of Terraform code yet, but I will give you a brief overview of what it looks like. Terraform coding is written in the HashiCorp Configuration Language (HCL). It is a declarative language that allows you to define your infrastructure in code. The code is organized into blocks, which define resources. Each block has a type, which specifies the type of resource to create, and a name used to identify the resource. For example, the following shows some of the code it takes to create a virtual machine named myvm:

resource "azurerm_virtual_machine" "myvm" {
 name = "myvm"
 location = "westus"
 resource_group_name = "myrg"
 network_interface_ids = [azurerm_network_interface.myvm.id]
 vm_size = "Standard_DS1_v2"

 storage_image_reference {
 publisher = "Canonical"
 offer = "UbuntuServer"
 sku = "18.04-LTS"
 version = "latest"
 }

 storage_os_disk {
 name = "myvm_os_disk"
 caching = "ReadWrite"
 create_option = "FromImage"
 managed_disk_type = "Standard_LRS"
 }

 os_profile {
 computer_name = "myvm"
 admin_username = "azureuser"
 }
}

Version Control

Terraform code should be stored in a version control system such as Git. Version control allows you to track changes to your code and roll back to a previous version if necessary. You can also use a version control system to collaborate with other developers on your team. For example, you can use Git to create a branch for each developer and merge changes back into the main branch when they are ready.

Testing

Before deploying your infrastructure, it is a good idea to test your code to ensure that it is correct and free of errors. To do this, you can use tools such as ’terraform validate’ and ’terraform plan’. The ’terraform validate’ command checks your code for syntax errors. The ’terraform plan’ command generates an execution plan that shows what Terraform will do when you apply your code. Reviewing the plan can be useful for identifying potential issues before deploying your infrastructure.

Deployment

Once you have tested your code and are satisfied with the results, use your configuration management tool to deploy your infrastructure. Terraform uses the command:

terraform apply

This command reads your code and creates the resources that you have defined.

Monitoring and Maintenance

Ongoing maintenance and monitoring are essential for ensuring that your infrastructure runs smoothly. Use tools such as Azure monitoring and logging to track the performance of your infrastructure and identify any issues that may arise. You can also use tools such as Terraform Cloud to automate tasks such as deploying infrastructure and managing secrets.

Conclusion

In this article, we have covered the basics of infrastructure as code. We have also looked at some tools and techniques you can use to create infrastructure as code. I hope you are interested in learning more about Terraform! Check out the Terraform documentation. You can also find more information about Terraform in upcoming articles.