Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------- terraform import ------------------------#
- region = "ap-south-1"
- access_key = "AKIA5YKGNPEXMXI3AEUM"
- secret_key = "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"
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- tags = {
- "Name" = "webserver"
- "Project" = "Zomato"
- "Env" = "Prod"
- }
- }
- #######----------Output------------------##############
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform import aws_instance.frontend i-0fbae2cb6c5639ed9
- aws_instance.frontend: Importing from ID "i-0fbae2cb6c5639ed9"...
- aws_instance.frontend: Import prepared!
- Prepared aws_instance for import
- aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
- Import successful!
- The resources that were imported are shown above. These resources are now in
- your Terraform state and will henceforth be managed by Terraform.
- #-------------------------Terraform refresh --------------------------#
- provider "aws" {
- region = "ap-south-1"
- access_key = "AKIA5YKGNPEXMXI3AEUM"
- secret_key = "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"
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- tags = {
- "Name" = "webserver"
- "Project" = "Zomato"
- "Env" = "Prod"
- }
- }
- resource "aws_security_group" "webserver" {
- name_prefix = "frontend-access"
- description = "Allow ssh and https trafiic"
- ingress {
- description = "HTTP"
- from_port = 443
- to_port = 443
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "SSH"
- from_port = 22
- to_port = 22
- 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" = "webserver"
- "Project" = "Zomato"
- "Env" = "Prod"
- }
- }
- output "Frontend-_IP" {
- value = aws_instance.frontend.public_ip
- }
- #########-----Output---------########
- terraform plan
- Plan: 0 to add, 1 to change, 0 to destroy.
- Changes to Outputs:
- ~ Frontend-_IP = "15.206.89.154" -> "13.126.26.6"
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform refresh -target=aws_instance.frontend
- aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
- ╷
- │ 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.
- ╵
- Outputs:
- Frontend-_IP = "13.126.26.6"
- --------------------# Terraform dynamic blocks #--------------------------------
- region = "ap-south-1"
- access_key = "AKIA5YKGNPEXMXI3AEUM"
- secret_key = "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"
- }
- variable "ports" {
- type = list
- default = ["80","443"]
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- tags = {
- "Name" = "webserver"
- "Project" = "Zomato"
- "Env" = "Prod"
- }
- }
- resource "aws_security_group" "webserver" {
- name_prefix = "frontend-access"
- description = "Allow ssh and https trafiic"
- dynamic "ingress" {
- for_each = toset(var.ports)
- content {
- from_port = ingress.value
- to_port = ingress.value
- 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" = "webserver"
- "Project" = "Zomato"
- "Env" = "Prod"
- }
- }
- output "Frontend-_IP" {
- value = aws_instance.frontend.public_ip
- }
- --------# Output # -------
- Apply complete! Resources: 0 added, 2 changed, 0 destroyed.
- Outputs:
- Frontend-_IP = "13.126.26.6"
- --------------------------------# terraform SG import #--------------------------------
- resource "aws_security_group" "frontend" {}
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform import aws_security_group.frontend sg-0abcc64bd10c47c44
- aws_security_group.frontend: Importing from ID "sg-0abcc64bd10c47c44"...
- aws_security_group.frontend: Import prepared!
- Prepared aws_security_group for import
- aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
- Import successful!
- The resources that were imported are shown above. These resources are now in
- your Terraform state and will henceforth be managed by Terraform.
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)
- ###
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
- aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
- aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
- aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
- aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
- aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
- 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_security_group.frontend must be replaced
- -/+ resource "aws_security_group" "frontend" {
- ~ arn = "arn:aws:ec2:ap-south-1:945577359662:security-group/sg-0abcc64bd10c47c44" -> (known after apply)
- ~ description = "Webser-import" -> "Managed by Terraform" # forces replacement
- ~ egress = [
- - {
- - cidr_blocks = [
- - "0.0.0.0/0",
- ]
- - description = ""
- - from_port = 0
- - ipv6_cidr_blocks = []
- - prefix_list_ids = []
- - protocol = "-1"
- - security_groups = []
- - self = false
- - to_port = 0
- },
- ] -> (known after apply)
- ~ id = "sg-0abcc64bd10c47c44" -> (known after apply)
- ~ ingress = [
- - {
- - cidr_blocks = [
- - "0.0.0.0/0",
- ]
- - description = ""
- - from_port = 22
- - ipv6_cidr_blocks = []
- - prefix_list_ids = []
- - protocol = "tcp"
- - security_groups = []
- - self = false
- - to_port = 22
- },
- - {
- - cidr_blocks = [
- - "0.0.0.0/0",
- ]
- - description = ""
- - from_port = 443
- - ipv6_cidr_blocks = []
- - prefix_list_ids = []
- - protocol = "tcp"
- - security_groups = []
- - self = false
- - to_port = 443
- },
- - {
- - cidr_blocks = [
- - "0.0.0.0/0",
- ]
- - description = ""
- - from_port = 80
- - ipv6_cidr_blocks = []
- - prefix_list_ids = []
- - protocol = "tcp"
- - security_groups = []
- - self = false
- - to_port = 80
- },
- ] -> (known after apply)
- ~ name = "Webser-import" -> (known after apply)
- + name_prefix = (known after apply)
- ~ owner_id = "945577359662" -> (known after apply)
- + revoke_rules_on_delete = false
- - tags = {
- - "Env" = "Prod"
- - "Name" = "Webser-import"
- - "Project" = "Zomato"
- } -> null
- ~ tags_all = {
- - "Env" = "Prod"
- - "Name" = "Webser-import"
- - "Project" = "Zomato"
- } -> (known after apply)
- ~ vpc_id = "vpc-06e13c7cdf3e917f5" -> (known after apply)
- }
- Plan: 1 to add, 1 to change, 1 to destroy.
- ----------------# Changed the resource block with the necessary value # ----------------
- resource "aws_security_group" "frontend" {
- description = "Webser-import"
- tags ={
- "Name" = "Webser-import",
- "Env" = "Prod",
- "Project" = "Zomato"
- }
- }
- ec2-user@DESKTOP-Q259SHJ ~/test/dynamic-test (default)$ terraform plan
- aws_security_group.webserver: Refreshing state... [id=sg-0dac461afb3883622]
- aws_security_group.frontend: Refreshing state... [id=sg-0abcc64bd10c47c44]
- aws_instance.frontend: Refreshing state... [id=i-0fbae2cb6c5639ed9]
- 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-0fbae2cb6c5639ed9"
- tags = {
- "Env" = "Prod"
- "Name" = "webserver"
- "Project" = "Zomato"
- }
- ~ user_data = "418be94938d863a18bda8ba6455223e11307d4b4" -> "2cf6f6b539cfd6ed985841356376326216e1f751"
- # (32 unchanged attributes hidden)
- # (8 unchanged blocks hidden)
- }
- Plan: 0 to add, 1 to change, 0 to destroy.
Advertisement
Add Comment
Please, Sign In to add comment