chrisluiz16

Untitled

May 18th, 2023
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.38 KB | None | 0 0
  1. #-------------------- terraform import ------------------------#
  2.  
  3.  
  4.   region     = "ap-south-1"
  5.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  6.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  7. }
  8.  
  9. resource "aws_instance" "frontend" {
  10.  ami = "ami-0b08bfc6ff7069aff"
  11.  instance_type = "t2.micro"
  12.     tags = {
  13.         "Name" = "webserver"
  14.         "Project" = "Zomato"
  15.          "Env" = "Prod"
  16.     }
  17. }
  18.  
  19.  
  20.  
  21.  
  22. #######----------Output------------------##############
  23.  
  24. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform import aws_instance.frontend i-0fbae2cb6c5639ed9
  25. aws_instance.frontend: Importing from ID "i-0fbae2cb6c5639ed9"...
  26. aws_instance.frontend: Import prepared!
  27.   Prepared aws_instance for import
  28. aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
  29.  
  30. Import successful!
  31.  
  32. The resources that were imported are shown above. These resources are now in
  33. your Terraform state and will henceforth be managed by Terraform.
  34.  
  35.  
  36. #-------------------------Terraform refresh --------------------------#
  37.  
  38. provider "aws" {
  39.   region     = "ap-south-1"
  40.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  41.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  42. }
  43.  
  44. resource "aws_instance" "frontend" {
  45.  ami = "ami-0b08bfc6ff7069aff"
  46.  instance_type = "t2.micro"
  47.     tags = {
  48.         "Name" = "webserver"
  49.         "Project" = "Zomato"
  50.          "Env" = "Prod"
  51.     }
  52. }
  53.  
  54. resource "aws_security_group" "webserver" {
  55.   name_prefix        = "frontend-access"
  56.   description = "Allow ssh and https trafiic"
  57.  
  58.   ingress {
  59.     description      = "HTTP"
  60.     from_port        = 443
  61.     to_port          = 443
  62.     protocol         = "tcp"
  63.     cidr_blocks      = ["0.0.0.0/0"]
  64.     ipv6_cidr_blocks = ["::/0"]
  65.   }
  66.  
  67. ingress {
  68.     description      = "SSH"
  69.     from_port        = 22
  70.     to_port          = 22
  71.     protocol         = "tcp"
  72.     cidr_blocks      = ["0.0.0.0/0"]
  73.     ipv6_cidr_blocks = ["::/0"]
  74.   }
  75.  
  76.   egress {
  77.     from_port        = 0
  78.     to_port          = 0
  79.     protocol         = "-1"
  80.     cidr_blocks      = ["0.0.0.0/0"]
  81.     ipv6_cidr_blocks = ["::/0"]
  82.   }
  83.  
  84.   tags = {
  85.     "Name" = "webserver"
  86.         "Project" = "Zomato"
  87.          "Env" = "Prod"
  88.   }
  89. }
  90.  
  91. output "Frontend-_IP" {
  92. value = aws_instance.frontend.public_ip
  93. }
  94.  
  95.  
  96.  
  97.  
  98. #########-----Output---------########
  99. terraform plan
  100. Plan: 0 to add, 1 to change, 0 to destroy.
  101.  
  102. Changes to Outputs:
  103.   ~ Frontend-_IP = "15.206.89.154" -> "13.126.26.6"
  104.  
  105. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform refresh -target=aws_instance.frontend
  106. aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
  107. ╷
  108. │ Warning: Resource targeting is in effect
  109. │
  110. │ You are creating a plan with the -target option, which means that the result of this plan may not represent all of
  111. │ the changes requested by the current configuration.
  112. │
  113. │ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from
  114. │ errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.
  115. ╵
  116.  
  117. Outputs:
  118.  
  119. Frontend-_IP = "13.126.26.6"
  120.  
  121.  
  122.  
  123. --------------------# Terraform dynamic blocks #--------------------------------
  124.  
  125.   region     = "ap-south-1"
  126.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  127.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  128. }
  129.  
  130. variable "ports" {
  131. type = list
  132. default = ["80","443"]
  133. }
  134.  
  135.  
  136. resource "aws_instance" "frontend" {
  137.  ami = "ami-0b08bfc6ff7069aff"
  138.  instance_type = "t2.micro"
  139.     tags = {
  140.         "Name" = "webserver"
  141.         "Project" = "Zomato"
  142.          "Env" = "Prod"
  143.     }
  144. }
  145.  
  146. resource "aws_security_group" "webserver" {
  147.   name_prefix        = "frontend-access"
  148.   description = "Allow ssh and https trafiic"
  149.  
  150. dynamic "ingress" {
  151. for_each = toset(var.ports)
  152.   content {
  153.     from_port        = ingress.value
  154.     to_port          = ingress.value
  155.     protocol         = "tcp"
  156.     cidr_blocks      = ["0.0.0.0/0"]
  157.     ipv6_cidr_blocks = ["::/0"]
  158.   }
  159. }
  160.  
  161.  
  162.   egress {
  163.     from_port        = 0
  164.     to_port          = 0
  165.     protocol         = "-1"
  166.     cidr_blocks      = ["0.0.0.0/0"]
  167.     ipv6_cidr_blocks = ["::/0"]
  168.   }
  169.  
  170.   tags = {
  171.     "Name" = "webserver"
  172.         "Project" = "Zomato"
  173.          "Env" = "Prod"
  174.   }
  175. }
  176.  
  177. output "Frontend-_IP" {
  178. value = aws_instance.frontend.public_ip
  179. }
  180.  
  181.  
  182. --------# Output # -------
  183.  
  184. Apply complete! Resources: 0 added, 2 changed, 0 destroyed.
  185.  
  186. Outputs:
  187.  
  188. Frontend-_IP = "13.126.26.6"
  189.  
  190.  
  191.  
  192. --------------------------------# terraform SG import #--------------------------------
  193.  
  194. resource "aws_security_group" "frontend" {}
  195. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform import aws_security_group.frontend sg-0abcc64bd10c47c44
  196. aws_security_group.frontend: Importing from ID "sg-0abcc64bd10c47c44"...
  197. aws_security_group.frontend: Import prepared!
  198.   Prepared aws_security_group for import
  199. aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
  200.  
  201. Import successful!
  202.  
  203. The resources that were imported are shown above. These resources are now in
  204. your Terraform state and will henceforth be managed by Terraform.
  205.  
  206. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)
  207.  
  208.  
  209. ###
  210.  
  211. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
  212. aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
  213. aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
  214. aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
  215.  
  216. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  217. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
  218. aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
  219. aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
  220. aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
  221.  
  222. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  223.  
  224. -/+ destroy and then create replacement
  225.  
  226. Terraform will perform the following actions:
  227.   # aws_security_group.frontend must be replaced
  228. -/+ resource "aws_security_group" "frontend" {
  229.       ~ arn                    = "arn:aws:ec2:ap-south-1:945577359662:security-group/sg-0abcc64bd10c47c44" -> (known after apply)
  230.       ~ description            = "Webser-import" -> "Managed by Terraform" # forces replacement
  231.       ~ egress                 = [
  232.           - {
  233.               - cidr_blocks      = [
  234.                   - "0.0.0.0/0",
  235.                 ]
  236.               - description      = ""
  237.               - from_port        = 0
  238.               - ipv6_cidr_blocks = []
  239.               - prefix_list_ids  = []
  240.               - protocol         = "-1"
  241.               - security_groups  = []
  242.               - self             = false
  243.               - to_port          = 0
  244.             },
  245.         ] -> (known after apply)
  246.       ~ id                     = "sg-0abcc64bd10c47c44" -> (known after apply)
  247.       ~ ingress                = [
  248.           - {
  249.               - cidr_blocks      = [
  250.                   - "0.0.0.0/0",
  251.                 ]
  252.               - description      = ""
  253.               - from_port        = 22
  254.               - ipv6_cidr_blocks = []
  255.               - prefix_list_ids  = []
  256.               - protocol         = "tcp"
  257.               - security_groups  = []
  258.               - self             = false
  259.               - to_port          = 22
  260.             },
  261.           - {
  262.               - cidr_blocks      = [
  263.                   - "0.0.0.0/0",
  264.                 ]
  265.               - description      = ""
  266.               - from_port        = 443
  267.               - ipv6_cidr_blocks = []
  268.               - prefix_list_ids  = []
  269.               - protocol         = "tcp"
  270.               - security_groups  = []
  271.               - self             = false
  272.               - to_port          = 443
  273.             },
  274.           - {
  275.               - cidr_blocks      = [
  276.                   - "0.0.0.0/0",
  277.                 ]
  278.               - description      = ""
  279.               - from_port        = 80
  280.               - ipv6_cidr_blocks = []
  281.               - prefix_list_ids  = []
  282.               - protocol         = "tcp"
  283.               - security_groups  = []
  284.               - self             = false
  285.               - to_port          = 80
  286.             },
  287.         ] -> (known after apply)
  288.       ~ name                   = "Webser-import" -> (known after apply)
  289.       + name_prefix            = (known after apply)
  290.       ~ owner_id               = "945577359662" -> (known after apply)
  291.       + revoke_rules_on_delete = false
  292.       - tags                   = {
  293.           - "Env"     = "Prod"
  294.           - "Name"    = "Webser-import"
  295.           - "Project" = "Zomato"
  296.         } -> null
  297.       ~ tags_all               = {
  298.           - "Env"     = "Prod"
  299.           - "Name"    = "Webser-import"
  300.           - "Project" = "Zomato"
  301.         } -> (known after apply)
  302.       ~ vpc_id                 = "vpc-06e13c7cdf3e917f5" -> (known after apply)
  303.     }
  304.  
  305. Plan: 1 to add, 1 to change, 1 to destroy.
  306.  
  307.  
  308. ----------------# Changed the resource block with the necessary value # ----------------
  309.  
  310. resource "aws_security_group" "frontend" {
  311. description = "Webser-import"
  312. tags ={
  313. "Name" = "Webser-import",
  314. "Env" = "Prod",
  315. "Project" = "Zomato"
  316.  
  317. }
  318. }
  319.  
  320. ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
  321. aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
  322. aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
  323. aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
  324.  
  325. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  326.   ~ update in-place
  327.  
  328. Terraform will perform the following actions:
  329.  
  330.   # aws_instance.frontend will be updated in-place
  331.   ~ resource "aws_instance" "frontend" {
  332.         id                                   = "i-0fbae2cb6c5639ed9"
  333.         tags                                 = {
  334.             "Env"     = "Prod"
  335.             "Name"    = "webserver"
  336.             "Project" = "Zomato"
  337.         }
  338.       ~ user_data                            = "418be94938d863a18bda8ba6455223e11307d4b4" -> "2cf6f6b539cfd6ed985841356376326216e1f751"
  339.         # (32 unchanged attributes hidden)
  340.  
  341.         # (8 unchanged blocks hidden)
  342.     }
  343.  
  344. Plan: 0 to add, 1 to change, 0 to destroy.
  345.  
  346.  
Advertisement
Add Comment
Please, Sign In to add comment