Task 1:
Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (any one)
First, we'll require a provider terraform file for AWS
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Now we will create a main.tf file, in which we will write all required AWS EC2 specifications
If you want to know about Terraform basics, blocks, expressions in HCL, check out previous blogs...
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "Terra-Demo" {
ami = "ami-053b0d53c279acc90
instance_type = "t2.micro"
tags = {
Name = "Terraform-Demo"
}
}
output "public_ip" {
value = aws_instance.Terra-Demo.public_ip
}
This configuration file is written in the Terraform language known as HCL (HashiCorp Configuration Language).
EC2 instance resource defines an AWS provider. Choose the ami
and instance_type
as per your requirements.
terraform init
Run terraform init command, to initialize the terraform, it will download all required dependencies.
terrfaom plan
Run terraform plan command to preview the changes that Terraform will make to your infrastructure before actually applying those changes.
ubuntu@ip-172-31-35-115:~/Terra-day-3$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.Terra-Demo will be created
+ resource "aws_instance" "Terra-Demo" {
+ ami = "ami-053b0d53c279acc90"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ disable_api_stop = (known after apply)
+ disable_api_termination = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ host_resource_group_arn = (known after apply)
+ iam_instance_profile = (known after apply)
+ id = (known after apply)
+ instance_initiated_shutdown_behavior = (known after apply)
+ instance_lifecycle = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ monitoring = (known after apply)
+ outpost_arn = (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
+ placement_partition_number = (known after apply)
+ primary_network_interface_id = (known after apply)
+ private_dns = (known after apply)
+ private_ip = (known after apply)
+ public_dns = (known after apply)
+ public_ip = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_groups = (known after apply)
+ source_dest_check = true
+ spot_instance_request_id = (known after apply)
+ subnet_id = (known after apply)
+ tags = {
+ "Name" = "Terraform-Demo"
}
+ tags_all = {
+ "Name" = "Terraform-Demo"
}
+ tenancy = (known after apply)
+ user_data = (known after apply)
+ user_data_base64 = (known after apply)
+ user_data_replace_on_change = false
+ vpc_security_group_ids = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ public_ip = (known after apply)
──────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly
these actions if you run "terraform apply" now.
ubuntu@ip-172-31-35-115:~/Terra-day-3$
Task 2:
Check state files before running the plan and apply commands & Use the validate command to validate your tf file for errors and provide the Output generated by each command.
Validate the Terraform Configuration:
The terraform validate
command checks the configuration files for syntax errors:
ubuntu@ip-172-31-35-115:~/Terra-day-3$ terraform validate
Success! The configuration is valid.
ubuntu@ip-172-31-35-115:~/Terra-day-3$
Check State Files:
To check the state of your infrastructure with the terraform state
list command. since we haven't applied any changes yet, there won't be any resources in the state.
Apply the Changes:
After checking syntax by running terrafom plan
command, you can apply the changes using the terraform apply
command.
after running terraform apply
,it will prompt for your confirmation, Type 'yes' and proceed.
terraform state list
Task 3
Add a provisioner to the configuration file to configure the resource after it is created and use Terraform commands to apply for changes and destroy to remove resources.
After a resource has been established, it can be configured using a provisioner. In this example, when the AWS EC2 instance resource is established, a provisioner will be added to allow the installation of a basic web server (NGINX). Additionally, we'll utilize Terraform commands to implement the modifications and remove the resources once you're done.
We need to update Terraform configuration file that includes the provisioner:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "Terra-Demo" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = "Terraform-Demo"
}
provisioner "local-exec" {
command = "echo 'This resource is provisioned' > provisioned.txt"
}
}
output "public_ip" {
value = aws_instance.Terra-Demo.public_ip
}
Now, run terraform init then terraform apply comamnds.
You can see in above picture, 'provisioned.txt' file created and if we cat this file messege is also written there!
Task 4
Add lifecycle management configurations to the configuration file to control the creation, modification, and deletion of the resource and use Terraform commands to apply the changes.
The creation, modification, and destruction of resources can be managed by Terraform configuration using the lifecycle
block.
lifecycle {
create_before_destroy = true
prevent_destroy = false
}
Inside the lifecycle
block:
create_before_destroy = true
indicates that Terraform should create a new resource before destroying the existing one when updates are necessary.
prevent_destroy = false
allows the resource to be destroyed. If set to true
, it would prevent the resource from being destroyed by Terraform, useful for critical resources that should not be deleted.
Then user terraform init
then terraform apply
commands to apply the changes.
Terraform will create or update the EC2 instance based on configuration!
Conclusion:
In this Blog, We have explored how to manage resources using Terraform, focusing on various resource types and their configurations, such as AWS EC2 instances.
We’ve also discussed resource dependencies, and lifecycle management.
Thank you!
Happy learning...!