Getting started with Terraform.
Terraform is a tool provided by HashiCorp which used to define infrastructure in code format. It supports different providers such as AWS, Azure, Google Cloud Platform and many more. Terraform was coded in the ‘GO’ language. It is used for building, changing, versioning the complete infrastructure.
This tool assists in the creation of a precise execution plan for achieving a given goal. Terraform implements the plan to develop the infrastructure after the blueprint of the approach is successfully prepared. When the configuration changes during the process, Terraform can identify all of them and provide modified execution remedies that may be used.
Features of Terraform
- Infrastructure as a Code
- Re-usability of Code
- Terraform Lint(Linting helps you quickly find any syntax errors and other code validation errors in your Terraform scripts.)
- Rendering of State and Plan.
- Open Source
- Declarative(What end result we want.)
Difference between Ansible and Terraform
Ansible
- Ansible follows the procedural style.
- It is mainly used for configuring the infrastructure.
- We can deploy apps using ansible.
- It maintains all components in working condition and repairs the issue instead of replacing the the entire infrastructure.
- We need to write each step in order to obtain the end result.
Terraform
- Terraform is completely declarative.
- It is mainly used to create infrastructure.
- We can deploy Load Balancer, VPC, RDS, EC2 using terraform.
- Once we give the end instruction, it carries out all the steps and gives us the final result.
- Terraform is used to conserve the environment in a steady state.
How Terraform works?
To work with terraform, we need to connect terraform to cloud providers like AWS using the secret key and access key. Once it gets connected we can create infrastructure as defined in the code.
Core Engine of terraform mainly takes config parameter file named as TF-Config which have details what to configure and State file. It makes a plan like what needs to be created/modified or removed. There is two state-defined current state and desired state.
Terraform Components
- Terraform Executable
- Terraform Files
- Terraform Plugins
- Terraform State
Terraform Installation in Ubuntu
wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip
unzip terraform_1.0.7_linux_amd64.zip
sudo mv terraform /usr/local/bin
sudo apt-get install python3-pip -y
sudo pip3 install awscli
awscli — is used to connect the ec2 with your AWS account
Terraform Lifecycle
Write a terraform file with the extension .tf and save it.
Example: To create EC2 in AWS.
Once the file is written, you can execute the following commands:
- terraform init- Initialize Terraform
- terraform plan- To dry test Terraform
- terraform apply- Apply Terraform
- terraform destroy- Destroy the resources created by Terraform
Best Practices for Terraform Implementation
- Use single shared state location.
- Use Validate and Plan commands frequently.
- Separate modules and environment code.
- Use Terraform registry for best practice.
- Abstract code to reuse.
Terraform Registry — Registry
Terraform — Terraform Signup
GitHub — AWS Terraform