What is Iaac (Infrastructure as a Code)?
IaaC is the concept of creating Infrastructure resources using a configuration file. IaaC allows you to speed up the infrastructure development.
IaaC configuration is error-free as its automation code method and the infrastructure will be deployed exactly as per the desired state in the configuration.
IaaC code must be pushed to the remote version-controlled repositories to maintain track of the infrastructure changes.
What is Terraform?
Terraform is an open-source infrastructure-as-code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language, or optionally JSON.
Why do we use terraform?
Terraform makes it easy to automate infrastructure provisioning on any cloud or data center. It also tracks all changes in a statefile. The file can be committed to a version control system (VCS) and acts as a source of truth for the infrastructure. It thus provides more transparency and prevents unauthorized changes.
What is a Provider?
A provider is a plugin that lets Terraform manage an external API. The provider plugins like the Aws provider and cloud-int providers act as translation layers that allow terraform to communicate with many different cloud providers, databases, and services. Ex: AWS, Azure, Google Cloud, Oracle Cloud etc…
Follow the Link to learn how to configure a provider.
The terraform provider block should be like below.
Provider: AWS
What is a Resource?
Terraform resources are the actual cloud or provider-specific resources that you would like to create/delete/modify the properties of a resource using Terraform configuration.
I am creating VPC in AWS. So resource type is "aws_vpc" and giving the resource name as "tws-vpc" as an identity.
What is a State file in Terraform? What’s the importance of it?
Terraform will store the actual properties of a resource that was created specific to the provider that you choose in the configuration. Terraform will use the state file as a reference to know how the resources were created and the actual current deployed configuration.
The tfstate file will have the resource information about properties and metadata info which means type of resource, and name of the resource information for terraform reference.
By default, the state file is stored locally with the name “terraform.tfstate” but can be configured to store the file in remote repos.
Terraform will take this state file as a reference each time it creates/modifies/destroys the resources.
Ex:
What is the Desired and Current State?
In Terraform, the desired state is the state that you want your infrastructure to be as defined in your Terraform Configuration files.
The current state is the actual state of the infrastructure as represented in the Terraform state file
Task:
Technology Stacks on local ( pre-requisites)
Install Visual Studio Code
Download vs code latest version fromhereand install it.Install AWS CLI
Download AWSCLI latest version fromhereand install it.or you can run the below command in powershell or the command prompt
Install Terraform
Download Terraform the latest version fromhere
Setup environment variable click on start --> search "edit the environment variables" and click on it
Under the advanced tab, choose "Environment variables" --> under the system variables select path variable and add terraform location in the path variable. system variables --> select path add new --> terraform_PathRun the below command to validate the Terraform version the output should be something like below
Connect to AWS Cloud
You must have an AWS account to proceed with the below steps.
Create IAM user
Go to IAM > create user and add "AdministratorAccess" policy
Login to aws cli
Write Terraform code
Write the First Terraform code
Run terraform command > terrafrom init > terraform validate > terraform plan
Before running the 'terraform apply' command check the AWS console
Run terraform apply
EC2 instance created
To destroy created infrastructure run 'terraform destroy'
EC2 instance created, validated, applied and at last destroyed through Terraform code successfully.
"Thank you for reading my blog! Happy Learning!!!😊
Stay tuned for more DevOps articles follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/namratakumari04/ for the latest updates and discussions.