Terraform Work Flow: The user needs to create the Terraform configuration file, the terraform configuration file needs to b...
Terraform Work Flow:
The user needs to create the Terraform configuration file, the terraform configuration file needs to be in the .tf file.
The Terraform file defines the provision and creation of the resources.
The second input source is a terraformed core that keeps an up-to-date state. How the current setup of the whole infrastructure looks like.
The Terraform core takes the input and figures the plan of what needs to be done.
It compares the state and checks that the current state is the configuration that you desired in the end result.
It figures out what needs to be done to get a desired state in the configuration file.
Providers:
This is the second component of Architecture. It gives the possibility to create infrastructure on different levels.
It has hundred of providers for the different technologies, and each provider gives access to the users to use the resources.
For example, Aws provides give access to hundred of AWS resources like Ec2 instance, user, etc.
Kubernetes providers access the commodities, resources like services, deployments, namespace, etc.
How to write terraform files, and provision the infrastructure in AWS?.
I am going to create the directory for my purpose. inside the directory, we can add the terraform file.
command: mkdir terraform
Now inside the directory, we are going to create the file called terraform.tf, now using any editor to
open the file to add the terraform codes on it.
commands: vi terraform.tf
How to add the Terraform code(HCL):
The Terraform registry itself gives the support to write the code for all the providers.
Now open the terraform file, and use the syntax from the terraforming registry. Now change the code for our used case.
provider "aws" {
region = "us-west-2"
access_key = "my-access-key"
secret_key = "my-secret-key"
}
Now we are using the Terraform file to create the AWS resources, so we need AWS access.
The AWS has an IAM service, we need to use the user’s access key and secret key of the user.
Our Demo code looks like this.
Now we are going to create the EC2 instance using the Terraform.
Using the Terraform registry reference, we are creating the ec2 instance.
Now use the below code to create only ec2-instance with default aws options.
resource "aws_instance" "foo" {
ami = "ami-005e54dee72cc1d00" # us-west-2
instance_type = "t2.micro"
}
“Foo” needs to change the region, we are using us-east-Ohio, so change the region to “us-east-2”
For AMI id varies depending on the AMI we are choosing, now I am going to choose amazon Linux
existing AMI, So give that AMI id in the code. Instance type basic free tire t2.micro fine for us.
Once the code is updated save the terraform file.
As of now, Our terraform file was ready, now we are going to use that terraform file to create the ec2 instance using the terraform commands
Initialize the terraform using the command terraform init
After the initialize, Apply the plan command to create the desired state of the infrastructure
Command: terraform plan
Now apply the changes to the aws infrastructure as defined in the plan.
command: terraform apply
After issuing the terraform apply commander it will ask the option like the do you want to perform these actions, we need to give the yes command to run the apply command.
We must check our aws, whether the ec2-instance was created or not.
In our requirement, I want to create the ec2-instance in Ohio-us-east-2.
Ec2-instance was created using terraform successfully.
Now we don’t want the created instance so going to destroy the ec2-instance using the command “terraform destroy”
After issuing the destroy command, It will ask, do you really want to destroy all resources,
give yes to destroy the infrastructure created before.
Cross-check this operation in aws, The previously created AWS instance was destroyed successfully
In the Next Blog, we will see how to create AWS VPC, Subnet, S3, RDS using Terraform.