What is terraform?
According to the official Terraform documentation, Terraform is 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.
It is an open-source IaC (Infrastructure as Code) tool developed by HashiCorp.Terraform creates an execution plan that explains how it will get to the desired state and then implements it to build the infrastructure specified. Terraform can decide what changes as the configuration changes and build gradual execution plans that can be implemented.
- Input Variables — Serve as parameters for a Terraform module, so users can customize behavior without editing the source
- Modules — Acts as a container for multiple resources that are used together. It is a way to package and reuse resource configurations.
- Resources — Documents the syntax for declaring resources
- Data sources — Allow data to be fetched or computed for use elsewhere in Terraform configuration
- Output values — Return values for a Terraform module
- Local values — A convenience feature for assigning a short name to an expression
Terraform Lifecycle Methods
- terraform init :Initializes the working directory which consists of all the configuration files. The terraform init command creates a working directory in which Terraform configuration files can be found.
- terraform validate : Validates the configuration files in a directory. terraform validate to make sure that your code is valid. If there are any errors in the syntax or missing data, this will display that.
- terraform plan — Creates an execution plan to reach a desired state of the infrastructure. We can run terraform plan to see the execution plan. This is a fantastic way to double-check the changes before executing them. If you don’t specify any VPC, subnet IDs, this configuration will create the resources in the default VPC with respective default values.
- terraform apply — Makes the changes in the infrastructure as defined in the plan. To actually create the resources, run terraform apply.
Example by terraform to deploy simple EC2 instance in AWS
- To connect with our AWS account and the Terraform code, we need to set up the AWS user credentials. Ensure that the user credentials you’re using have access to the resources you’re going to provision. Get a hold of the AWS Access key and the Secret access key of the IAM user with the right permissions and save it in a secure place.
- Since we’re dealing with AWS, we need to provide the credentials that we retrieved earlier to connect with our respective AWS resources. To set the AWS credentials in your command-line workspace
- Usually, the first step in using Terraform is to set up the provider you want to use. Make a file named aws.tf and insert the code below into it.
- Below mentioned is the code that you need to deploy a t2.micro instance. Write code in ec2.tf file.
- In a terminal, go to the folder that you created ec2.tf and aws.tf and run terraform init command.
- You can run terraform validate to make sure that your code is valid.
- We can run terraform plan to see the execution plan.
- To actually create the resources, run terraform apply.
- As you can see, we have now successfully created our first ec2 instance with the help of Terraform. If you navigate to your AWS console, you’ll see something like this