Terraform is an open source infrastructure as code tool. You are here because you know what terraform is and what it can do for you. Here, the primary goal is to understand the basics of terraform and write a script that will support the theory and make things clearer.
Terraform installation guide on Mac os:
Before we begin, let’s make our system ready by installing the terraform.
- Open terminal (press command⌘ + space and type terminal)
- brew install terraform
- Type terraform and if you see some message popping out regarding terraform then the installation was successful.
Quick start example:
The example above creates a EC2 instance of type t2 micro in us-east-2 region.
Some useful Terraform command before we begin
terraform init: to initialize the terraform
terraform plan: This is like dry run, will show the details what will terraform do and what are the settings it will apply.
terraform apply: The main command to start terraform
terraform fmt: will auto format the file in proper standard formatting
terraform validate : will look for some errors in the file
terraform plan: this command will show in detail what it is really going to do. Ie what it will add what will not and many more
terraform destroy: destroy the infrastructure created by terraform apply
Trying to create AWS VPC and Subnet using the Terraform:
We all know VPC is the building block of everything. If someone starts to learn AWS they will obviously go through it and is one of the important things to must know and implement in AWS cloud. So why not start with it. We will also learn to attach Subnet to VPC.
Terraform basic template:
Before diving into writing the terraform script, lets see, how the basic terraform script format looks alike.
Fig: Basic format of terraform script
As seen in the figure, we have two main parts in terraform: a part to set the cloud provider and a part to define the resources to provision.
Terraform script (main.tf):
Understanding the terraform script:
The terraform will do the provisioning on aws cloud provider and the region will be us-east-2. We must be familiar with all the AWS terminology to get used to with region and upcoming scripts as the terraform, here is meant to work on AWS cloud provider.
Now, the second section is related to resources. The resources refer to any services provided by the cloud. As our goal was creating VPC and Subnet using terraform. In our case VPC and Subnet is the resource for the terraform.
The resource takes 2 parameters:
- The first parameter is the resource name (eg: aws_ec2, aws_vpc, aws_subnet). Check terraform docs for more.
- The second parameter is the logical name given to the resource. This is used to give identification to the resources. If we have a multiple resources and need to refer any resource then we can use the logical name preceded by the resource name.
The above script will create a VPC with the properties defines inside the curly braces. Please, check the official terraform docs regarding the purpose of each key being used.
Output: it will create a VPC with defined keys and the tags. The Name parameter will also be used to name a resource.
This sections is another resource associated with subnet creation. It creates a subnet and is associated with with VPC resource. The association is done by the line:
vpc_id = aws_vpc.terraform_vpc.id
Here, we used the following syntax to link the resource:
Now, let’s hit the terraform command that will create our resources defined in Amazon.
$ terraform apply
Type yes, for confirmation