Terraform: Your Infrastructure Copilot (Part 1: Getting Started)
To understand Terraform, let’s explore its core purpose:
Let’s understand by its own use cases:
Multi-Cloud Deployment — Suppose the infrastructure you want to create or operate is hybrid, consisting of on-premises, multi-cloud… It’s impossible for a DevOps person to understand CloudFormation (CFT) for AWS, ARM for Azure, and Ansible for on-premises. Terraform allows us to use the same workflow to manage multiple providers and handle cross-cloud dependencies.
Efficient deploy, release, scale, and monitor of application- Terraform help in these features through hosting a static website, rolling out new releases with the help of load balancers, and monitoring applications with Datadog.
Self-Service Clusters — HCP Terraform can be used to build a self-service infrastructure model for a large organization that may get many repetitive infrastructure requests. For example, the Terraform ServiceNow Service Catalog integration enables your end-users to provision self-service infrastructure via ticketing systems like ServiceNow.
Policy Compliance and Management — HPC Terraform uses Sentinel(a policy as code framework )to automatically enforce policy on Terraform configurations, states, and plans.
Installation/Setup of Terraform
Refer to above site for installation or you can also opt for Codespaces(a development environment present in the cloud, by default it runs in a Linux environment) in the GitHub
Installation steps for Terraform & AWS on Codespaces -:
- Create Codespaces for your repository
2. In the search bar , select option -:
3. Rebuild the container
4. Check for installations
5. Configure AWS on Codespaces to interact directly with AWS resources -:
We can do the configure through root user or IAM user
Root user (ex- Admins) - full control over the infrastructure
IAM user(ex- Devops Engineer ,Developers) - individual accounts within AWS account to do a specific kind of task
Create access key (Long term credentials for an IAM user or root user) on AWS
Run the command “aws configure” on codespaces cli and provide the details
Let’s dive deeper into understanding the creation and workflow of Terraform.
- We need main.tf file— Starting point where you implement the logic of infrastructure as a code
In the above image , we are creating EC2 instance through terraform .We have to specify following parameters to create instances on AWS -:
1.Provider -plugin to interact with cloud providers
2. Resource -a crucial element to specify what kind of resource we are creating with unique identifier for instance name within terraform configuration
Apart from that other keys are feature of AWS to create the EC2 instance for ex- AMI (Amazon machine name ) ID to use for the instance, subnet (range of IP address in your VPC(Virtual Private Network) to define network range, gateway and security rule of instance) and key pair to access the instance
After writing the main.tf file ,we have to execute it according to the workflow of terraform -:
- terraform init # Initialize Terraform
- terraform plan #review the plan
3. terraform apply #will execute the main.tf file
4. Check in your AWS platform ,new EC2 instances will get created with the same AMI id that was mentioned in main.tf file (just to clarify we have to take the value from AWS to put in the main.tf)
5. “terraform destroy” #destroy all remote objects managed by a particular Terraform configuration
Note — You can also use “terraform validate” command to check whether a configuration is syntactically valid and internally consistent.
After executing main.tf file ,you will notice one state file has been created in your repository with this kind of content
This state file is by default gets created under the name “terraform.fstate” that stores information that Terraform has created .
Stay Tune and Happy to receive any kind of feedback
Abhishek Veeramalla & Bibin Wilson…Thanks for the motivation !!