Terraform With Real Time Example 1717640811
Terraform With Real Time Example 1717640811
3. Plan: Run terraform plan to create an execution plan. This plan shows what
actions Terraform will take to achieve the desired state described in your
configuration files.
4. Apply: Run terraform apply to execute the plan and create or modify your
infrastructure to match the desired state.
• These are the .tf files where you define your desired
infrastructure state.
2. Providers:
3. Resources:
4. State File:
5. Modules:
Real-Time Example
Let’s walk through a real-time example of deploying a
web server on AWS using Terraform.
‘main.tf’
provider "aws" {
region = "us-west-2"
}
tags = {
Name = "web-server"
}
}
‘variables.tf’
variable "aws_region" {
description = "The AWS region to create resources in"
default = "us-west-2"
}
‘outputs.tf’
output "instance_id" {
description = "The ID of the web server instance"
value = aws_instance.web.id
}
output "instance_public_ip" {
description = "The public IP of the web server instance"
value = aws_instance.web.public_ip
}
Step 2: Initialize
Run ‘terraform init’ to initialize your configuration. This will
download the AWS provider plugin.
terraform init
Step 3: Plan
Run ‘terraform plan’ to see what actions Terraform will take to create
your web server.
terraform plan
Step 4: Apply
Run terraform apply to create the web server instance.
terraform apply
Step 5: Verify
After the apply command completes, Terraform will output the instance
ID and public IP of your new web server .
Outputs:
instance_id = "i-0123456789abcdef0"
instance_public_ip = "54.123.45.67"
You can now access your web server using the public IP address.
Step 6: Destroy
When you no longer need the resources, you can run ‘terraform
destroy’ to remove all the infrastructure created by your configuration .
terraform destroy
Again, you will be prompted to confirm the action. Type yes to proceed