Table of contents
- Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure vm, Google Compute Engine, etc. (any one)
- Task 2: Check state files before running plan and apply commands & Use the validate command to validate your tf file for errors and provide the Output generated by each command.
- 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.
- 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.
- Thank You
Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure vm, Google Compute Engine, etc. (any one)
Now we will create the AWS EC2 instance and Azure storage account with the help of Terraform.
Create the file multi-providers.tf to create the AWS & Azure storage account.
I don't have azure account so I proceed with AWS Only:
Task 2: Check state files before running plan and apply commands & Use the validate command to validate your tf file for errors and provide the Output generated by each command.
Terraform validate: The terraform validate command validates the configuration files in a directory. Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state.
Now we will see how the terraform validate command works. as we do not mention the location in the file and try the terraform validate command
If our configuration is not correct it will show an error
Now we will correct the syntax and try to validate again: file successfully validated
Then run the terraform plan and apply it to create the EC2 instance:
terraform plan - Run terraform plan. Terraform will create an execution plan.
terraform apply - After the run terraform apply command, Terraform want conformation from you, Do you want to perform these actions? If you Enter a value: yes then terraform create the infrastructure:
Now verify the deployment of EC2 instance, Go to AWS EC2 dashboard you can see new EC2 instance created:
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.
In Terraform Provisioners are used to execute scripts or commands on the provisioned resources after they are created. Provisioners can be helpful for tasks such as configuring software, running initialization scripts, or performing custom actions that are not directly supported by Terraform's resource providers.
There are 3 types of provisioners:
file Provisioner:
The
file
provisioner is used to copy files or directories from the local machine to a remote machine. This is useful for deploying configuration files, scripts, or other assets to a provisioned instance.Example:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" } provisioner "file" { source = "local/path/to/localfile.txt" destination = "/path/on/remote/instance/file.txt" connection { type = "ssh" user = "ec2-user" private_key = file("~/.ssh/id_rsa") } }
remote-exec Provisioner:
The
remote-exec
provisioner is used to run scripts or commands on a remote machine over SSH or WinRM connections. It's often used to configure or install software on provisioned instances.Example:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" } provisioner "remote-exec" { inline = [ "sudo yum update -y", "sudo yum install -y httpd", "sudo systemctl start httpd", ] connection { type = "ssh" user = "ec2-user" private_key = file("~/.ssh/id_rsa") host = aws_instance.example.public_ip } }
local-exec Provisioner:
The
local-exec
provisioner is used to run scripts or commands locally on the machine where Terraform is executed. It is useful for tasks that don't require remote execution, such as initializing a local database or configuring local resources.Example:
resource "null_resource" "example" { triggers = { always_run = "${timestamp()}" } provisioner "local-exec" { command = "echo 'This is a local command'" } }
This is how we used the provisioner for local execution.
I will create another blog for Remote Exec:
Here we used local provisioner and we can see the output:
terraform destroy is a command used to tear down and destroy all the infrastructure resources that were previously created and managed by Terraform.
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.
In Terraform we can use the lifecycle block to configure various aspects of resource management, including controlling when and how resources are created, modified, and destroyed. The lifecycle block allows you to set attributes like create_before_destroy, prevent_destroy, and ignore_changes to customize the behavior of your resources.
In this example:
create_before_destroy = true: This configuration ensures that a new instance is created before the old one is destroyed during updates. This helps minimize downtime.
prevent_destroy = false: By default, Terraform allows resource deletion. If set to true, it would prevent the instance from being destroyed, providing an extra layer of protection. Use with caution.