Infrastructure As Code (IaC): Deploy Cloud Resources using Terraform
Provisioning of cloud resources by clicking multiple buttons on the cloud providers' management console is a time-consuming process that can be automated to achieve faster development timelines and save on operational costs.
In most organizations, deployment of cloud resources is a repetitive process and not a one time task. Imagine a situation where a cloud engineer needs to deploy a complex infrastructure in multiple environments (E.g. dev, test, and prod) or in a multi-cloud situation where you want to replicate the same cloud setup to a different service provider (E.g. replicate GCP to AWS).
In this kind of scenario, it is almost impossible not to make mistakes when things are done manually. IaC tools like Terraform can be used to reduce the risk of failure by automating the whole process.
Benefits of using IaC
Infrastructure as Code (IaC) is a DevOps practice that automates the provisioning of infrastructure, enabling your organization to develop, deploy, and scale cloud applications with great speed, less risk, and reduced cost.
In simple words, IaC is the process of replacing manual effort required for IT resource management and provisioning with simple lines of code.
So what are the key benefits of using IaC?
Consistency and repeatability: One of the biggest causes of development failure is having development/testing environments that don't properly mimic the production environment. IaC helps avoid cloud deployment inconsistencies.
Faster speed to market: IaC helps to ship products much faster. It speeds up development timelines by eliminating some of the boring stuff. This also means that you can iterate quickly and more often.
Reduced cost: the time saved by automating infrastructure deployment can save more money for the organization on hiring costs and engineers' salaries.
Introduction to Terraform
Terraform is an open-source infrastructure as code software tool created by HashiCorp. According to the creator, Terraform can be defined as a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Terraform can help with multi-cloud by having one workflow for all clouds.
Though this article is focused on Terraform as a multi-cloud IaC tool, most cloud providers have their own native IaC tool. For example, AWS has CloudFormation, Azure has Azure Resource Manager and Google Cloud has Deployment Manager. Each of these tools has its own unique way of declaring infrastructure as code.
Providers in Terraform act as a communication channel between Terraform and a target API. These can be major cloud providers or various other tools that can be used with Terraform. To date, Terraform has 100+ providers including AWS, Azure, GCP, OracleCloud, Heroku, and many more.
How to provision Apache Web Server with Terraform
To provide a hands-on experience of how to use Terraform, the step-by-step guide below would work you through how Terraform can be used to provision GCP resources.
The exercise makes use of a simple Terraform configuration file (
main.tf) located in the DIR, which can be used to provision an Apache Web Server installed on Linux.
1. Basic Setup: There are 3 basic setup procedures to be achieved before running the Terraform commands to deploy our resources.
Download and install Terraform on your local machine. Guideline for Windows, macOS, and Linux machines can be found here.
Clone the developed Terraform infrastructure code on GitHub. The configuration file is well documented and easy to follow.
From the GCP web console, create an IAM service account with authorization to work with Compute Engine. When the service account is created, click the
Create keyaction button to download the JSON file that contains the private key. Copy the JSON file to the same DIR as the Terraform configuration file.
2. Terraform Init:
terraform init to initialize the project. This command downloads the plugin that allows Terraform to interact with GCP.
3. Terraform Plan: Run
terraform plan to generate an execution plan for Terraform. This execution plan can be reviewed prior to running
terraform apply to get a sense of what Terraform will do.
4. Terraform Apply:
terraform apply to provision the GCP resources according to the configuration files in the DIR. When Terraform asks you to confirm type
yes and press
5. Terraform Destroy:
If you don't need the created resource to keep running after the exercise, run
terraform destroy to stop the virtual machine from running and delete all associated resources on GCP that were created by Terraform.
This is just a beginner's guide to IaC with Terraform. It can be used to deploy much more complex architectures than what has been showcased in this exercise.
As a multi-cloud tool, the procedures above can be used to deploy resources on other public cloud providers platforms. The only change required is that the configuration file
main.tf needs to be aligned with the Terraform syntax for the target provider.