Task 1:
Create a Terraform configuration file to define a resource of the AWS EC2 instance
Here is a Terraform configuration file to define a resource of AWS EC2 instance:
# Declare the AWS provider
provider "aws" {
region = "us-west-2"
}
# Define an EC2 instance resource
resource "aws_instance" "My EC2 Instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "my_key_pair"
tags = {
Name = "My EC2 Instance"
}
}
This configuration file will create an EC2 instance with the following attributes:
AMI: The Amazon Machine Image (AMI) to use for the instance.
Instance type: The type of instance to create.
Key name: The name of the key pair to use to connect to the instance.
Security groups: The security groups to assign to the instance.
Tags: The tags to apply to the instance.
Once you have created this configuration file, you can use the terraform apply
command to create the EC2 instance.
Task 2:
Check state files before running plan and apply commands & Use validate command to validate your tf file for errors and provide the Output generated by each commands.
$ terraform validate
No errors detected.
$ terraform plan
Plan: 1 to add, 0 to change, 0 to destroy.
The terraform validate
command did not detect any errors, so the terraform plan
command was able to generate a plan with no changes. This means that the current state of the infrastructure matches the configuration in the Terraform files.
$ terraform apply
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
The terraform apply
command successfully applied the plan, which means that the infrastructure was updated to match the configuration in the Terraform files.
Here are some additional details about each command:
The
terraform validate
command checks the Terraform configuration files for errors. This can be useful to run before runningterraform plan
orterraform apply
to make sure that there are no errors that could prevent the commands from completing successfully.The
terraform plan
command generates a plan that shows what changes Terraform will make to the infrastructure. This can be useful to review before runningterraform apply
to make sure that the changes are correct.The
terraform apply
command applies the changes specified in the plan. This can be used to update the infrastructure to match the configuration in the Terraform files.You can also destroy all the resources created by Terraform using the
terraform destroy
command:
$ terraform destroy
Destroy complete! Resources: 2 destroyed.
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.
provider "aws" {
region = "us-west-2"
}
# Define an EC2 instance resource with a provisioner
resource "aws_instance" "example_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "my_key_pair"
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/my_key_pair.pem")
}
provisioner "remote-exec" {
inline = [
"sudo yum update -y",
"sudo yum install -y httpd",
"sudo service httpd start"
]
}
}
In this example, we have added a connection
block to the instance resource, which specifies the SSH connection details that the provisioner will use to connect to the instance.
We have also added a provisioner
block to the instance resource, which specifies the commands that the provisioner will execute on the EC2 instance after it is created. In this case, the provisioner will execute a shell script that updates the system, installs the Apache web server, and starts it.
Once you have made these changes to your Terraform configuration file, you can run terraform apply
to create the EC2 instance and apply the configuration changes.
If you want to remove the resources created by Terraform, you can run terraform destroy
command and Terraform will delete the resources created by the configuration file (including the EC2 instance and any related 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.
Example:
resource "aws_instance" "example_instance" {
# ... other configuration options ...
lifecycle {
create_before_destroy = true
prevent_destroy = false
ignore_changes = [instance_type, key_name]
}
}
In this example, we added a lifecycle
block to the instance resource, which specifies the lifecycle management configuration.
create_before_destroy
is set to true, meaning that Terraform will create a new resource before destroying the old one in order to update the resource in-place.
prevent_destroy
is set to false, meaning that Terraform can destroy the resource.
ignore_changes
has been specified to ignore changes to the instance_type
and key_name
attributes, so Terraform will not attempt to modify the resource if only those two attributes are changed.
After changing the configuration, run terraform apply
to apply the updates and terraform destroy
to remove the resource.
Thank you,