chrisluiz16

Untitled

May 13th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 25.77 KB | None | 0 0
  1. ######### Task 1 destroy ######
  2.  
  3. [ec2-user@ip-172-31-32-32 lifecycle]$ terrafrom destroy -target=aws_instance.frontend
  4. -bash: terrafrom: command not found
  5. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform destroy -target=aws_instance.frontend
  6. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  7. aws_instance.frontend: Refreshing state... [id=i-06c5f8a151e0f6084]
  8.  
  9. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  10.   - destroy
  11.  
  12. Terraform will perform the following actions:
  13.  
  14.   # aws_instance.frontend will be destroyed
  15.   - resource "aws_instance" "frontend" {
  16.       - ami                                  = "ami-0b08bfc6ff7069aff" -> null
  17.       - arn                                  = "arn:aws:ec2:ap-south-1:945577359662:instance/i-06c5f8a151e0f6084" -> null
  18.       - associate_public_ip_address          = true -> null
  19.       - availability_zone                    = "ap-south-1a" -> null
  20.       - cpu_core_count                       = 1 -> null
  21.       - cpu_threads_per_core                 = 1 -> null
  22.       - disable_api_stop                     = false -> null
  23.       - disable_api_termination              = false -> null
  24.       - ebs_optimized                        = false -> null
  25.       - get_password_data                    = false -> null
  26.       - hibernation                          = false -> null
  27.       - id                                   = "i-06c5f8a151e0f6084" -> null
  28.       - instance_initiated_shutdown_behavior = "stop" -> null
  29.       - instance_state                       = "running" -> null
  30.       - instance_type                        = "t2.micro" -> null
  31.       - ipv6_address_count                   = 0 -> null
  32.       - ipv6_addresses                       = [] -> null
  33.       - key_name                             = "mumbai-new" -> null
  34.       - monitoring                           = false -> null
  35.       - placement_partition_number           = 0 -> null
  36.       - primary_network_interface_id         = "eni-0ff166951666c1c87" -> null
  37.       - private_dns                          = "ip-172-31-43-52.ap-south-1.compute.internal" -> null
  38.       - private_ip                           = "172.31.43.52" -> null
  39.       - public_dns                           = "ec2-65-0-29-201.ap-south-1.compute.amazonaws.com" -> null
  40.       - public_ip                            = "65.0.29.201" -> null
  41.       - secondary_private_ips                = [] -> null
  42.       - security_groups                      = [
  43.           - "Zomato-frontend-20230513070447874300000001",
  44.         ] -> null
  45.       - source_dest_check                    = true -> null
  46.       - subnet_id                            = "subnet-03d5cac27f2ede1f4" -> null
  47.       - tags                                 = {
  48.           - "Env"     = "dev"
  49.           - "Name"    = "zomato-webserver"
  50.           - "Project" = "Zomato"
  51.         } -> null
  52.       - tags_all                             = {
  53.           - "Env"     = "dev"
  54.           - "Name"    = "zomato-webserver"
  55.           - "Project" = "Zomato"
  56.         } -> null
  57.       - tenancy                              = "default" -> null
  58.       - user_data                            = "c528ca74069dac1affa7e37de7b5b163da227a87" -> null
  59.       - user_data_replace_on_change          = false -> null
  60.       - vpc_security_group_ids               = [
  61.           - "sg-0bd7051c44ead9832",
  62.         ] -> null
  63.  
  64.       - capacity_reservation_specification {
  65.           - capacity_reservation_preference = "open" -> null
  66.         }
  67.  
  68.       - cpu_options {
  69.           - core_count       = 1 -> null
  70.           - threads_per_core = 1 -> null
  71.         }
  72.  
  73.       - credit_specification {
  74.           - cpu_credits = "standard" -> null
  75.         }
  76.  
  77.       - enclave_options {
  78.           - enabled = false -> null
  79.         }
  80.  
  81.       - maintenance_options {
  82.           - auto_recovery = "default" -> null
  83.         }
  84.  
  85.       - metadata_options {
  86.           - http_endpoint               = "enabled" -> null
  87.           - http_put_response_hop_limit = 2 -> null
  88.           - http_tokens                 = "required" -> null
  89.           - instance_metadata_tags      = "disabled" -> null
  90.         }
  91.  
  92.       - private_dns_name_options {
  93.           - enable_resource_name_dns_a_record    = false -> null
  94.           - enable_resource_name_dns_aaaa_record = false -> null
  95.           - hostname_type                        = "ip-name" -> null
  96.         }
  97.  
  98.       - root_block_device {
  99.           - delete_on_termination = true -> null
  100.           - device_name           = "/dev/xvda" -> null
  101.           - encrypted             = false -> null
  102.           - iops                  = 3000 -> null
  103.           - tags                  = {} -> null
  104.           - throughput            = 125 -> null
  105.           - volume_id             = "vol-082f5a1974997b6e0" -> null
  106.           - volume_size           = 8 -> null
  107.           - volume_type           = "gp3" -> null
  108.         }
  109.     }
  110.  
  111. Plan: 0 to add, 0 to change, 1 to destroy.
  112. ╷
  113. │ Warning: Resource targeting is in effect
  114. │
  115. │ 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
  116. │ configuration.
  117. │
  118. │ 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
  119. │ specifically suggests to use it as part of an error message.
  120. ╵
  121.  
  122. Do you really want to destroy all resources?
  123.   Terraform will destroy all your managed infrastructure, as shown above.
  124.   There is no undo. Only 'yes' will be accepted to confirm.
  125.  
  126.   Enter a value: yes
  127.  
  128. aws_instance.frontend: Destroying... [id=i-06c5f8a151e0f6084]
  129. aws_instance.frontend: Still destroying... [id=i-06c5f8a151e0f6084, 10s elapsed]
  130. aws_instance.frontend: Still destroying... [id=i-06c5f8a151e0f6084, 20s elapsed]
  131. aws_instance.frontend: Destruction complete after 30s
  132. ╷
  133. │ Warning: Applied changes may be incomplete
  134. │
  135. │ 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
  136. │ not be fully updated. Run the following command to verify that no other changes are pending:
  137. │     terraform plan
  138. │
  139. │ 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,
  140. │ or when Terraform specifically suggests to use it as part of an error message.
  141. ╵
  142.  
  143. Destroy complete! Resources: 1 destroyed.
  144.  
  145.  
  146.  
  147. ########## Task2 userdata adding [Version 1] ##########
  148.  
  149. [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
  150. provider "aws" {
  151.   region = var.region
  152. }
  153.  
  154. terraform {
  155.   backend "s3" {
  156.     bucket = "terraform-s3bucket-zomato"
  157.     key    = "terraform.tfstate"
  158.     region = "ap-south-1"
  159.   }
  160. }
  161.  
  162. resource "aws_security_group" "frontend" {
  163.   name_prefix = "Zomato-frontend-"
  164.   description = "Allow http,https and ssh"
  165.   ingress {
  166.     description      = "HTTP port"
  167.     from_port        = 80
  168.     to_port          = 80
  169.     protocol         = "tcp"
  170.     cidr_blocks      = ["0.0.0.0/0"]
  171.     ipv6_cidr_blocks = ["::/0"]
  172.   }
  173.   egress {
  174.     from_port        = 0
  175.     to_port          = 0
  176.     protocol         = "-1"
  177.     cidr_blocks      = ["0.0.0.0/0"]
  178.     ipv6_cidr_blocks = ["::/0"]
  179.   }
  180.  
  181.   tags = {
  182.     "Name"  = "zomato-webserver",
  183.     Project = "Zomato",
  184.     Env     = "dev"
  185.   }
  186. }
  187.  
  188. resource "aws_instance" "frontend" {
  189.   ami                    = "ami-0b08bfc6ff7069aff"
  190.   instance_type          = "t2.micro"
  191.   key_name               = "mumbai-new"
  192.   vpc_security_group_ids = [aws_security_group.frontend.id]
  193.   user_data              = file("frontend.sh")
  194.   tags = {
  195.     "Name"  = "zomato-webserver",
  196.     Project = "Zomato",
  197.     Env     = "dev"
  198.   }
  199. }
  200.  
  201.  
  202. [ec2-user@ip-172-31-32-32 lifecycle]$ cat frontend.sh
  203. #!/bin/bash
  204.  
  205.  
  206. echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
  207. echo "LANG=en_US.utf-8" >> /etc/environment
  208. echo "LC_ALL=en_US.utf-8" >> /etc/environment
  209. systemctl restart sshd.service
  210.  
  211.  
  212. yum install httpd php -y
  213.  
  214. cat <<EOF > /var/www/html/index.php
  215. <?php
  216. \$output = shell_exec('echo $HOSTNAME');
  217. echo "<h1><center><pre>\$output</pre></center></h1>";
  218. echo "<h1><center>Terraform-server-version1</center></h1>"
  219. ?>
  220. EOF
  221.  
  222. systemctl restart httpd.service php-fpm.service
  223. systemctl enable httpd.service php-fpm.service
  224.  
  225.  
  226. ##### Output ####
  227.  
  228. c2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
  229. Plan: 1 to add, 0 to change, 0 to destroy.
  230. aws_instance.frontend: Creating...
  231. aws_instance.frontend: Still creating... [10s elapsed]
  232. aws_instance.frontend: Still creating... [20s elapsed]
  233. aws_instance.frontend: Creation complete after 21s [id=i-054b6fa7f947cad3f]
  234.  
  235. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  236.  
  237.  
  238. ########## Version 2 updated in userdata but not redflected in the instance [task 3] ############
  239.  
  240. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
  241. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  242. aws_instance.frontend: Refreshing state... [id=i-054b6fa7f947cad3f]
  243.  
  244. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  245.   ~ update in-place
  246.  
  247. Terraform will perform the following actions:
  248.  
  249.   # aws_instance.frontend will be updated in-place
  250.   ~ resource "aws_instance" "frontend" {
  251.         id                                   = "i-054b6fa7f947cad3f"
  252.         tags                                 = {
  253.             "Env"     = "dev"
  254.             "Name"    = "zomato-webserver"
  255.             "Project" = "Zomato"
  256.         }
  257.       ~ user_data                            = "c528ca74069dac1affa7e37de7b5b163da227a87" -> "c93eb6753e3b212aa9466fe196de87d74c57f4ff"
  258.         # (32 unchanged attributes hidden)
  259.  
  260.         # (8 unchanged blocks hidden)
  261.     }
  262.  
  263. Plan: 0 to add, 1 to change, 0 to destroy.
  264.  
  265.  
  266. ########## Version 3 updated in userdata  user_data_replace_on_change = true [Task 4] ############
  267.  
  268.  
  269. [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
  270. provider "aws" {
  271.   region = var.region
  272. }
  273.  
  274. terraform {
  275.   backend "s3" {
  276.     bucket = "terraform-s3bucket-zomato"
  277.     key    = "terraform.tfstate"
  278.     region = "ap-south-1"
  279.   }
  280. }
  281.  
  282. resource "aws_security_group" "frontend" {
  283.   name_prefix = "Zomato-frontend-"
  284.   description = "Allow http,https and ssh"
  285.   ingress {
  286.     description      = "HTTP port"
  287.     from_port        = 80
  288.     to_port          = 80
  289.     protocol         = "tcp"
  290.     cidr_blocks      = ["0.0.0.0/0"]
  291.     ipv6_cidr_blocks = ["::/0"]
  292.   }
  293.   egress {
  294.     from_port        = 0
  295.     to_port          = 0
  296.     protocol         = "-1"
  297.     cidr_blocks      = ["0.0.0.0/0"]
  298.     ipv6_cidr_blocks = ["::/0"]
  299.   }
  300.  
  301.   tags = {
  302.     "Name"  = "zomato-webserver",
  303.     Project = "Zomato",
  304.     Env     = "dev"
  305.   }
  306. }
  307.  
  308. resource "aws_instance" "frontend" {
  309.   ami                         = "ami-0b08bfc6ff7069aff"
  310.   instance_type               = "t2.micro"
  311.   key_name                    = "mumbai-new"
  312.   vpc_security_group_ids      = [aws_security_group.frontend.id]
  313.   user_data                   = file("frontend.sh")
  314.   user_data_replace_on_change = true
  315.   tags = {
  316.     "Name"  = "zomato-webserver",
  317.     Project = "Zomato",
  318.     Env     = "dev"
  319.   }
  320. }
  321.  
  322.  
  323. #### Output ####
  324.  
  325. Plan: 1 to add, 0 to change, 1 to destroy.
  326. aws_instance.frontend: Destroying... [id=i-054b6fa7f947cad3f]
  327. aws_instance.frontend: Still destroying... [id=i-054b6fa7f947cad3f, 10s elapsed]
  328. aws_instance.frontend: Still destroying... [id=i-054b6fa7f947cad3f, 20s elapsed]
  329. aws_instance.frontend: Destruction complete after 30s
  330. aws_instance.frontend: Creating...
  331. aws_instance.frontend: Still creating... [10s elapsed]
  332. aws_instance.frontend: Still creating... [20s elapsed]
  333. aws_instance.frontend: Creation complete after 21s [id=i-0df5a2c2ad8f73f1d]
  334.  
  335. Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
  336.  
  337.  
  338. ########## Terraform replace [task 5] ############
  339.  
  340.  
  341. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan -replace=aws_instance.frontend
  342. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  343. aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
  344.  
  345. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  346. -/+ destroy and then create replacement
  347.  
  348. Terraform will perform the following actions:
  349.  
  350.   # aws_instance.frontend will be replaced, as requested
  351. -/+ resource "aws_instance" "frontend" {
  352.       ~ arn                                  = "arn:aws:ec2:ap-south-1:945577359662:instance/i-0df5a2c2ad8f73f1d" -> (known after apply)
  353.       ~ associate_public_ip_address          = true -> (known after apply)
  354.       ~ availability_zone                    = "ap-south-1a" -> (known after apply)
  355.       ~ cpu_core_count                       = 1 -> (known after apply)
  356.       ~ cpu_threads_per_core                 = 1 -> (known after apply)
  357.       ~ disable_api_stop                     = false -> (known after apply)
  358.       ~ disable_api_termination              = false -> (known after apply)
  359.       ~ ebs_optimized                        = false -> (known after apply)
  360.       - hibernation                          = false -> null
  361.       + host_id                              = (known after apply)
  362.       + host_resource_group_arn              = (known after apply)
  363.       + iam_instance_profile                 = (known after apply)
  364.       ~ id                                   = "i-0df5a2c2ad8f73f1d" -> (known after apply)
  365.       ~ instance_initiated_shutdown_behavior = "stop" -> (known after apply)
  366.       ~ instance_state                       = "running" -> (known after apply)
  367.       ~ ipv6_address_count                   = 0 -> (known after apply)
  368.       ~ ipv6_addresses                       = [] -> (known after apply)
  369.       ~ monitoring                           = false -> (known after apply)
  370.       + outpost_arn                          = (known after apply)
  371.       + password_data                        = (known after apply)
  372.       + placement_group                      = (known after apply)
  373.       ~ placement_partition_number           = 0 -> (known after apply)
  374.       ~ primary_network_interface_id         = "eni-079ae9b0a23f9e4d5" -> (known after apply)
  375.       ~ private_dns                          = "ip-172-31-47-192.ap-south-1.compute.internal" -> (known after apply)
  376.       ~ private_ip                           = "172.31.47.192" -> (known after apply)
  377.       ~ public_dns                           = "ec2-43-205-192-117.ap-south-1.compute.amazonaws.com" -> (known after apply)
  378.       ~ public_ip                            = "43.205.192.117" -> (known after apply)
  379.       ~ secondary_private_ips                = [] -> (known after apply)
  380.       ~ security_groups                      = [
  381.           - "Zomato-frontend-20230513070447874300000001",
  382.         ] -> (known after apply)
  383.       ~ subnet_id                            = "subnet-03d5cac27f2ede1f4" -> (known after apply)
  384.         tags                                 = {
  385.             "Env"     = "dev"
  386.             "Name"    = "zomato-webserver"
  387.             "Project" = "Zomato"
  388.         }
  389.       ~ tenancy                              = "default" -> (known after apply)
  390.       + user_data_base64                     = (known after apply)
  391.         # (9 unchanged attributes hidden)
  392.  
  393.       - capacity_reservation_specification {
  394.           - capacity_reservation_preference = "open" -> null
  395.         }
  396.  
  397.       - cpu_options {
  398.           - core_count       = 1 -> null
  399.           - threads_per_core = 1 -> null
  400.         }
  401.  
  402.       - credit_specification {
  403.           - cpu_credits = "standard" -> null
  404.         }
  405.  
  406.       - enclave_options {
  407.           - enabled = false -> null
  408.         }
  409.  
  410.       - maintenance_options {
  411.           - auto_recovery = "default" -> null
  412.         }
  413.  
  414.       - metadata_options {
  415.           - http_endpoint               = "enabled" -> null
  416.           - http_put_response_hop_limit = 2 -> null
  417.           - http_tokens                 = "required" -> null
  418.           - instance_metadata_tags      = "disabled" -> null
  419.         }
  420.  
  421.       - private_dns_name_options {
  422.           - enable_resource_name_dns_a_record    = false -> null
  423.           - enable_resource_name_dns_aaaa_record = false -> null
  424.           - hostname_type                        = "ip-name" -> null
  425.         }
  426.  
  427.       - root_block_device {
  428.           - delete_on_termination = true -> null
  429.           - device_name           = "/dev/xvda" -> null
  430.           - encrypted             = false -> null
  431.           - iops                  = 3000 -> null
  432.           - tags                  = {} -> null
  433.           - throughput            = 125 -> null
  434.           - volume_id             = "vol-0639e167319403249" -> null
  435.           - volume_size           = 8 -> null
  436.           - volume_type           = "gp3" -> null
  437.         }
  438.     }
  439.  
  440. Plan: 1 to add, 0 to change, 1 to destroy.
  441.  
  442.  
  443.  
  444. ########## Terraform replace [task 6] ############
  445.  
  446.  
  447. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform taint aws_instance.frontend
  448. Resource instance aws_instance.frontend has been marked as tainted.
  449. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
  450. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  451. aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
  452.  
  453. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  454. -/+ destroy and then create replacement
  455.  
  456. Terraform will perform the following actions:
  457.  
  458.   # aws_instance.frontend is tainted, so must be replaced
  459.  
  460. Plan: 1 to add, 0 to change, 1 to destroy.
  461.  
  462. ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  463.  
  464. 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.
  465. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform untaint aws_instance.frontend
  466. Resource instance aws_instance.frontend has been successfully untainted.
  467. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
  468. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  469. aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
  470.  
  471. No changes. Your infrastructure matches the configuration.
  472.  
  473. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
  474. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform taint aws_instance.frontend
  475. Resource instance aws_instance.frontend has been marked as tainted.
  476. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
  477. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  478. aws_instance.frontend: Refreshing state... [id=i-0df5a2c2ad8f73f1d]
  479.  
  480. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  481. -/+ destroy and then create replacement
  482.  
  483. Terraform will perform the following actions:
  484.  
  485.   # aws_instance.frontend is tainted, so must be replaced
  486.  
  487.  
  488.  
  489. ### Output ###
  490.  
  491. Plan: 1 to add, 0 to change, 1 to destroy.
  492. aws_instance.frontend: Destroying... [id=i-0df5a2c2ad8f73f1d]
  493. aws_instance.frontend: Still destroying... [id=i-0df5a2c2ad8f73f1d, 10s elapsed]
  494. aws_instance.frontend: Still destroying... [id=i-0df5a2c2ad8f73f1d, 20s elapsed]
  495. aws_instance.frontend: Destruction complete after 29s
  496. aws_instance.frontend: Creating...
  497. aws_instance.frontend: Still creating... [10s elapsed]
  498. aws_instance.frontend: Still creating... [20s elapsed]
  499. aws_instance.frontend: Creation complete after 21s [id=i-0bf6ccd85971da77a]
  500.  
  501. Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
  502.  
  503.  
  504. ########## ignore_changes [task 7] ############
  505.  
  506.  
  507. [ec2-user@ip-172-31-32-32 lifecycle]$ cat main.tf
  508. provider "aws" {
  509.   region = var.region
  510. }
  511.  
  512. terraform {
  513.   backend "s3" {
  514.     bucket = "terraform-s3bucket-zomato"
  515.     key    = "terraform.tfstate"
  516.     region = "ap-south-1"
  517.   }
  518. }
  519.  
  520. resource "aws_security_group" "frontend" {
  521.   name_prefix = "Zomato-frontend-"
  522.   description = "Allow http,https and ssh"
  523.   ingress {
  524.     description      = "HTTP port"
  525.     from_port        = 80
  526.     to_port          = 80
  527.     protocol         = "tcp"
  528.     cidr_blocks      = ["0.0.0.0/0"]
  529.     ipv6_cidr_blocks = ["::/0"]
  530.   }
  531.   egress {
  532.     from_port        = 0
  533.     to_port          = 0
  534.     protocol         = "-1"
  535.     cidr_blocks      = ["0.0.0.0/0"]
  536.     ipv6_cidr_blocks = ["::/0"]
  537.   }
  538.  
  539.   tags = {
  540.     "Name"  = "zomato-webserver",
  541.     Project = "Zomato",
  542.     Env     = "dev"
  543.   }
  544. }
  545.  
  546. resource "aws_instance" "frontend" {
  547.   ami                         = "ami-0b08bfc6ff7069aff"
  548.   instance_type               = "t2.micro"
  549.   key_name                    = "mumbai-new"
  550.   vpc_security_group_ids      = [aws_security_group.frontend.id]
  551.   user_data                   = file("frontend.sh")
  552.   user_data_replace_on_change = true
  553.   tags = {
  554.     "Name"  = "zomato-webserver",
  555.     Project = "Zomato",
  556.     Env     = "dev"
  557.   }
  558.   lifecycle {
  559.     ignore_changes = [
  560.       tags,
  561.       vpc_security_group_ids,
  562.     ]
  563.  
  564.   }
  565. }
  566.  
  567. ### Output ###
  568.  
  569. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
  570. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  571. aws_instance.frontend: Refreshing state... [id=i-0bf6ccd85971da77a]
  572.  
  573. No changes. Your infrastructure matches the configuration.
  574.  
  575. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
  576.  
  577. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
  578.  
  579.  
  580. ########## replace_triggered_by = [task 8] ############
  581.  
  582.  
  583. provider "aws" {
  584.   region = var.region
  585. }
  586.  
  587. terraform {
  588.   backend "s3" {
  589.     bucket = "terraform-s3bucket-zomato"
  590.     key    = "terraform.tfstate"
  591.     region = "ap-south-1"
  592.   }
  593. }
  594.  
  595. resource "aws_security_group" "frontend" {
  596.   name_prefix = "Zomato-frontend-"
  597.   description = "Allow http,https and ssh"
  598.   ingress {
  599.     description      = "HTTP port"
  600.     from_port        = 80
  601.     to_port          = 80
  602.     protocol         = "tcp"
  603.     cidr_blocks      = ["0.0.0.0/0"]
  604.     ipv6_cidr_blocks = ["::/0"]
  605.   }
  606.   egress {
  607.     from_port        = 0
  608.     to_port          = 0
  609.     protocol         = "-1"
  610.     cidr_blocks      = ["0.0.0.0/0"]
  611.     ipv6_cidr_blocks = ["::/0"]
  612.   }
  613.  
  614.   tags = {
  615.     "Name"  = "zomato-webserver",
  616.     Project = "Zomato",
  617.     Env     = "dev",
  618.     owner   = "chris"
  619.   }
  620. }
  621.  
  622. resource "aws_instance" "frontend" {
  623.   ami                         = "ami-0b08bfc6ff7069aff"
  624.   instance_type               = "t2.micro"
  625.   key_name                    = "mumbai-new"
  626.   vpc_security_group_ids      = [aws_security_group.frontend.id]
  627.   user_data                   = file("frontend.sh")
  628.   user_data_replace_on_change = true
  629.   tags = {
  630.     "Name"  = "zomato-webserver",
  631.     Project = "Zomato",
  632.     Env     = "dev"
  633.   }
  634.   lifecycle {
  635.     ignore_changes = [
  636.       tags,
  637.       vpc_security_group_ids,
  638.     ]
  639.     replace_triggered_by = [
  640.       aws_securtiy_group.frontend.tags
  641.     ]
  642.   }
  643. }
  644.  
  645.  
  646. ### Output ###
  647. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform apply -auto-approve
  648. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  649. aws_instance.frontend: Refreshing state... [id=i-0bf6ccd85971da77a]
  650.  
  651. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  652.   ~ update in-place
  653. -/+ destroy and then create replacement
  654.  
  655. Terraform will perform the following actions:
  656.  
  657.   # aws_instance.frontend will be replaced due to changes in replace_triggered_by
  658. Plan: 1 to add, 1 to change, 1 to destroy.
  659. aws_instance.frontend: Destroying... [id=i-0bf6ccd85971da77a]
  660. aws_instance.frontend: Still destroying... [id=i-0bf6ccd85971da77a, 10s elapsed]
  661. aws_instance.frontend: Still destroying... [id=i-0bf6ccd85971da77a, 20s elapsed]
  662. aws_instance.frontend: Destruction complete after 29s
  663. aws_security_group.frontend: Modifying... [id=sg-0bd7051c44ead9832]
  664. aws_security_group.frontend: Modifications complete after 0s [id=sg-0bd7051c44ead9832]
  665. aws_instance.frontend: Creating...
  666. aws_instance.frontend: Still creating... [10s elapsed]
  667. aws_instance.frontend: Still creating... [20s elapsed]
  668. aws_instance.frontend: Creation complete after 21s [id=i-09873f2943b39317c]
  669.  
  670. Apply complete! Resources: 1 added, 1 changed, 1 destroyed.
  671.  
  672.  
  673. ###### Unamnaging the resource from terraform [ task 9]  ####
  674.  
  675. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform state list
  676. aws_instance.frontend
  677. aws_security_group.frontend
  678. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform state rm aws_instance.frontend
  679. Removed aws_instance.frontend
  680. Successfully removed 1 resource instance(s).
  681. [ec2-user@ip-172-31-32-32 lifecycle]$ vim main.tf
  682. [ec2-user@ip-172-31-32-32 lifecycle]$ terraform plan
  683. aws_security_group.frontend: Refreshing state... [id=sg-0bd7051c44ead9832]
  684.  
  685. No changes. Your infrastructure matches the configuration.
  686.  
  687. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
  688. [ec2-user@ip-172-31-32-32 lifecycle]$
  689.  
  690. ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  691.  
  692.  
Add Comment
Please, Sign In to add comment