Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######### Task 1 destroy ######
- [ec2-user@ip-172-31-32-32 lifecycle]$ terrafrom destroy -target=aws_instance.frontend
- -bash: terrafrom: command not found
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform destroy -target=aws_instance.frontend
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-06c5f8a151e0f6084]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- - destroy
- Terraform will perform the following actions:
- # aws_instance.frontend will be destroyed
- - resource "aws_instance" "frontend" {
- - ami = "ami-0b08bfc6ff7069aff" -> null
- - arn = "arn:aws:ec2:ap-south-1:945577359662:instance/i-06c5f8a151e0f6084" -> null
- - associate_public_ip_address = true -> null
- - availability_zone = "ap-south-1a" -> null
- - cpu_core_count = 1 -> null
- - cpu_threads_per_core = 1 -> null
- - disable_api_stop = false -> null
- - disable_api_termination = false -> null
- - ebs_optimized = false -> null
- - get_password_data = false -> null
- - hibernation = false -> null
- - id = "i-06c5f8a151e0f6084" -> null
- - instance_initiated_shutdown_behavior = "stop" -> null
- - instance_state = "running" -> null
- - instance_type = "t2.micro" -> null
- - ipv6_address_count = 0 -> null
- - ipv6_addresses = [] -> null
- - key_name = "mumbai-new" -> null
- - monitoring = false -> null
- - placement_partition_number = 0 -> null
- - primary_network_interface_id = "eni-0ff166951666c1c87" -> null
- - private_dns = "ip-172-31-43-52.ap-south-1.compute.internal" -> null
- - private_ip = "172.31.43.52" -> null
- - public_dns = "ec2-65-0-29-201.ap-south-1.compute.amazonaws.com" -> null
- - public_ip = "65.0.29.201" -> null
- - secondary_private_ips = [] -> null
- - security_groups = [
- - "Zomato-frontend-20230513070447874300000001",
- ] -> null
- - source_dest_check = true -> null
- - subnet_id = "subnet-03d5cac27f2ede1f4" -> null
- - tags = {
- - "Env" = "dev"
- - "Name" = "zomato-webserver"
- - "Project" = "Zomato"
- } -> null
- - tags_all = {
- - "Env" = "dev"
- - "Name" = "zomato-webserver"
- - "Project" = "Zomato"
- } -> null
- - tenancy = "default" -> null
- - user_data = "c528ca74069dac1affa7e37de7b5b163da227a87" -> null
- - user_data_replace_on_change = false -> null
- - vpc_security_group_ids = [
- - "sg-0bd7051c44ead9832",
- ] -> null
- - capacity_reservation_specification {
- - capacity_reservation_preference = "open" -> null
- }
- - cpu_options {
- - core_count = 1 -> null
- - threads_per_core = 1 -> null
- }
- - credit_specification {
- - cpu_credits = "standard" -> null
- }
- - enclave_options {
- - enabled = false -> null
- }
- - maintenance_options {
- - auto_recovery = "default" -> null
- }
- - metadata_options {
- - http_endpoint = "enabled" -> null
- - http_put_response_hop_limit = 2 -> null
- - http_tokens = "required" -> null
- - instance_metadata_tags = "disabled" -> null
- }
- - private_dns_name_options {
- - enable_resource_name_dns_a_record = false -> null
- - enable_resource_name_dns_aaaa_record = false -> null
- - hostname_type = "ip-name" -> null
- }
- - root_block_device {
- - delete_on_termination = true -> null
- - device_name = "/dev/xvda" -> null
- - encrypted = false -> null
- - iops = 3000 -> null
- - tags = {} -> null
- - throughput = 125 -> null
- - volume_id = "vol-082f5a1974997b6e0" -> null
- - volume_size = 8 -> null
- - volume_type = "gp3" -> null
- }
- }
- Plan: 0 to add, 0 to change, 1 to destroy.
- ╷
- │ Warning: Resource targeting is in effect
- │
- │ You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current
- │ configuration.
- │
- │ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform
- │ specifically suggests to use it as part of an error message.
- ╵
- Do you really want to destroy all resources?
- Terraform will destroy all your managed infrastructure, as shown above.
- There is no undo. Only 'yes' will be accepted to confirm.
- Enter a value: yes
- aws_instance.frontend: Destroying... [id=i-06c5f8a151e0f6084]
- aws_instance.frontend: Still destroying... [id=i-06c5f8a151e0f6084, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-06c5f8a151e0f6084, 20s elapsed]
- aws_instance.frontend: Destruction complete after 30s
- ╷
- │ Warning: Applied changes may be incomplete
- │
- │ The plan was created with the -target option in effect, so some changes requested in the configuration may have been ignored and the output values may
- │ not be fully updated. Run the following command to verify that no other changes are pending:
- │ terraform plan
- │
- │ Note that the -target option is not suitable for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes,
- │ or when Terraform specifically suggests to use it as part of an error message.
- ╵
- Destroy complete! Resources: 1 destroyed.
- ########## Task2 userdata adding [Version 1] ##########
- [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
- provider "aws" {
- region = var.region
- }
- terraform {
- backend "s3" {
- bucket = "terraform-s3bucket-zomato"
- key = "terraform.tfstate"
- region = "ap-south-1"
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "Zomato-frontend-"
- description = "Allow http,https and ssh"
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- }
- [ec2-user@ip-172-31-32-32 lifecycle]$ cat frontend.sh
- #!/bin/bash
- echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
- echo "LANG=en_US.utf-8" >> /etc/environment
- echo "LC_ALL=en_US.utf-8" >> /etc/environment
- systemctl restart sshd.service
- yum install httpd php -y
- cat <<EOF > /var/www/html/index.php
- <?php
- \$output = shell_exec('echo $HOSTNAME');
- echo "<h1><center><pre>\$output</pre></center></h1>";
- echo "<h1><center>Terraform-server-version1</center></h1>"
- ?>
- EOF
- systemctl restart httpd.service php-fpm.service
- systemctl enable httpd.service php-fpm.service
- ##### Output ####
- c2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
- Plan: 1 to add, 0 to change, 0 to destroy.
- aws_instance.frontend: Creating...
- aws_instance.frontend: Still creating... [10s elapsed]
- aws_instance.frontend: Still creating... [20s elapsed]
- aws_instance.frontend: Creation complete after 21s [id=i-054b6fa7f947cad3f]
- Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
- ########## Version 2 updated in userdata but not redflected in the instance [task 3] ############
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-054b6fa7f947cad3f]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- ~ update in-place
- Terraform will perform the following actions:
- # aws_instance.frontend will be updated in-place
- ~ resource "aws_instance" "frontend" {
- id = "i-054b6fa7f947cad3f"
- tags = {
- "Env" = "dev"
- "Name" = "zomato-webserver"
- "Project" = "Zomato"
- }
- ~ user_data = "c528ca74069dac1affa7e37de7b5b163da227a87" -> "c93eb6753e3b212aa9466fe196de87d74c57f4ff"
- # (32 unchanged attributes hidden)
- # (8 unchanged blocks hidden)
- }
- Plan: 0 to add, 1 to change, 0 to destroy.
- ########## Version 3 updated in userdata user_data_replace_on_change = true [Task 4] ############
- [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
- provider "aws" {
- region = var.region
- }
- terraform {
- backend "s3" {
- bucket = "terraform-s3bucket-zomato"
- key = "terraform.tfstate"
- region = "ap-south-1"
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "Zomato-frontend-"
- description = "Allow http,https and ssh"
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- user_data_replace_on_change = true
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- }
- #### Output ####
- Plan: 1 to add, 0 to change, 1 to destroy.
- aws_instance.frontend: Destroying... [id=i-054b6fa7f947cad3f]
- aws_instance.frontend: Still destroying... [id=i-054b6fa7f947cad3f, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-054b6fa7f947cad3f, 20s elapsed]
- aws_instance.frontend: Destruction complete after 30s
- aws_instance.frontend: Creating...
- aws_instance.frontend: Still creating... [10s elapsed]
- aws_instance.frontend: Still creating... [20s elapsed]
- aws_instance.frontend: Creation complete after 21s [id=i-0df5a2c2ad8f73f1d]
- Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- ########## Terraform replace [task 5] ############
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan -replace=aws_instance.frontend
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- -/+ destroy and then create replacement
- Terraform will perform the following actions:
- # aws_instance.frontend will be replaced, as requested
- -/+ resource "aws_instance" "frontend" {
- ~ arn = "arn:aws:ec2:ap-south-1:945577359662:instance/i-0df5a2c2ad8f73f1d" -> (known after apply)
- ~ associate_public_ip_address = true -> (known after apply)
- ~ availability_zone = "ap-south-1a" -> (known after apply)
- ~ cpu_core_count = 1 -> (known after apply)
- ~ cpu_threads_per_core = 1 -> (known after apply)
- ~ disable_api_stop = false -> (known after apply)
- ~ disable_api_termination = false -> (known after apply)
- ~ ebs_optimized = false -> (known after apply)
- - hibernation = false -> null
- + host_id = (known after apply)
- + host_resource_group_arn = (known after apply)
- + iam_instance_profile = (known after apply)
- ~ id = "i-0df5a2c2ad8f73f1d" -> (known after apply)
- ~ instance_initiated_shutdown_behavior = "stop" -> (known after apply)
- ~ instance_state = "running" -> (known after apply)
- ~ ipv6_address_count = 0 -> (known after apply)
- ~ ipv6_addresses = [] -> (known after apply)
- ~ monitoring = false -> (known after apply)
- + outpost_arn = (known after apply)
- + password_data = (known after apply)
- + placement_group = (known after apply)
- ~ placement_partition_number = 0 -> (known after apply)
- ~ primary_network_interface_id = "eni-079ae9b0a23f9e4d5" -> (known after apply)
- ~ private_dns = "ip-172-31-47-192.ap-south-1.compute.internal" -> (known after apply)
- ~ private_ip = "172.31.47.192" -> (known after apply)
- ~ public_dns = "ec2-43-205-192-117.ap-south-1.compute.amazonaws.com" -> (known after apply)
- ~ public_ip = "43.205.192.117" -> (known after apply)
- ~ secondary_private_ips = [] -> (known after apply)
- ~ security_groups = [
- - "Zomato-frontend-20230513070447874300000001",
- ] -> (known after apply)
- ~ subnet_id = "subnet-03d5cac27f2ede1f4" -> (known after apply)
- tags = {
- "Env" = "dev"
- "Name" = "zomato-webserver"
- "Project" = "Zomato"
- }
- ~ tenancy = "default" -> (known after apply)
- + user_data_base64 = (known after apply)
- # (9 unchanged attributes hidden)
- - capacity_reservation_specification {
- - capacity_reservation_preference = "open" -> null
- }
- - cpu_options {
- - core_count = 1 -> null
- - threads_per_core = 1 -> null
- }
- - credit_specification {
- - cpu_credits = "standard" -> null
- }
- - enclave_options {
- - enabled = false -> null
- }
- - maintenance_options {
- - auto_recovery = "default" -> null
- }
- - metadata_options {
- - http_endpoint = "enabled" -> null
- - http_put_response_hop_limit = 2 -> null
- - http_tokens = "required" -> null
- - instance_metadata_tags = "disabled" -> null
- }
- - private_dns_name_options {
- - enable_resource_name_dns_a_record = false -> null
- - enable_resource_name_dns_aaaa_record = false -> null
- - hostname_type = "ip-name" -> null
- }
- - root_block_device {
- - delete_on_termination = true -> null
- - device_name = "/dev/xvda" -> null
- - encrypted = false -> null
- - iops = 3000 -> null
- - tags = {} -> null
- - throughput = 125 -> null
- - volume_id = "vol-0639e167319403249" -> null
- - volume_size = 8 -> null
- - volume_type = "gp3" -> null
- }
- }
- Plan: 1 to add, 0 to change, 1 to destroy.
- ########## Terraform replace [task 6] ############
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform taint aws_instance.frontend
- Resource instance aws_instance.frontend has been marked as tainted.
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- -/+ destroy and then create replacement
- Terraform will perform the following actions:
- # aws_instance.frontend is tainted, so must be replaced
- Plan: 1 to add, 0 to change, 1 to destroy.
- ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
- 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.
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform untaint aws_instance.frontend
- Resource instance aws_instance.frontend has been successfully untainted.
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
- No changes. Your infrastructure matches the configuration.
- Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform taint aws_instance.frontend
- Resource instance aws_instance.frontend has been marked as tainted.
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- -/+ destroy and then create replacement
- Terraform will perform the following actions:
- # aws_instance.frontend is tainted, so must be replaced
- ### Output ###
- Plan: 1 to add, 0 to change, 1 to destroy.
- aws_instance.frontend: Destroying... [id=i-0df5a2c2ad8f73f1d]
- aws_instance.frontend: Still destroying... [id=i-0df5a2c2ad8f73f1d, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-0df5a2c2ad8f73f1d, 20s elapsed]
- aws_instance.frontend: Destruction complete after 29s
- aws_instance.frontend: Creating...
- aws_instance.frontend: Still creating... [10s elapsed]
- aws_instance.frontend: Still creating... [20s elapsed]
- aws_instance.frontend: Creation complete after 21s [id=i-0bf6ccd85971da77a]
- Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- ########## ignore_changes [task 7] ############
- [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
- provider "aws" {
- region = var.region
- }
- terraform {
- backend "s3" {
- bucket = "terraform-s3bucket-zomato"
- key = "terraform.tfstate"
- region = "ap-south-1"
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "Zomato-frontend-"
- description = "Allow http,https and ssh"
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- user_data_replace_on_change = true
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- lifecycle {
- ignore_changes = [
- tags,
- vpc_security_group_ids,
- ]
- }
- }
- ### Output ###
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0bf6ccd85971da77a]
- No changes. Your infrastructure matches the configuration.
- Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
- Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
- ########## replace_triggered_by = [task 8] ############
- provider "aws" {
- region = var.region
- }
- terraform {
- backend "s3" {
- bucket = "terraform-s3bucket-zomato"
- key = "terraform.tfstate"
- region = "ap-south-1"
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "Zomato-frontend-"
- description = "Allow http,https and ssh"
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev",
- owner = "chris"
- }
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- user_data_replace_on_change = true
- tags = {
- "Name" = "zomato-webserver",
- Project = "Zomato",
- Env = "dev"
- }
- lifecycle {
- ignore_changes = [
- tags,
- vpc_security_group_ids,
- ]
- replace_triggered_by = [
- aws_securtiy_group.frontend.tags
- ]
- }
- }
- ### Output ###
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Refreshing state... [id=i-0bf6ccd85971da77a]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- ~ update in-place
- -/+ destroy and then create replacement
- Terraform will perform the following actions:
- # aws_instance.frontend will be replaced due to changes in replace_triggered_by
- Plan: 1 to add, 1 to change, 1 to destroy.
- aws_instance.frontend: Destroying... [id=i-0bf6ccd85971da77a]
- aws_instance.frontend: Still destroying... [id=i-0bf6ccd85971da77a, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-0bf6ccd85971da77a, 20s elapsed]
- aws_instance.frontend: Destruction complete after 29s
- aws_security_group.frontend: Modifying... [id=sg-0bd7051c44ead9832]
- aws_security_group.frontend: Modifications complete after 0s [id=sg-0bd7051c44ead9832]
- aws_instance.frontend: Creating...
- aws_instance.frontend: Still creating... [10s elapsed]
- aws_instance.frontend: Still creating... [20s elapsed]
- aws_instance.frontend: Creation complete after 21s [id=i-09873f2943b39317c]
- Apply complete! Resources: 1 added, 1 changed, 1 destroyed.
- ###### Unamnaging the resource from terraform [ task 9] ####
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform state list
- aws_instance.frontend
- aws_security_group.frontend
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform state rm aws_instance.frontend
- Removed aws_instance.frontend
- Successfully removed 1 resource instance(s).
- [ec2-user@ip-172-31-32-32 lifecycle]$ vim main.tf
- [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
- No changes. Your infrastructure matches the configuration.
- Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
- [ec2-user@ip-172-31-32-32 lifecycle]$
- ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Add Comment
Please, Sign In to add comment