[Terraform docs : AWS] 07. Query Data with outputs

박종배·2023년 1월 22일
0
post-thumbnail

서론

아래에 글은 공식문서를 공부하며 정리한 내용입니다.

참고


요약

  • output을 활용하면 infra의 정보들을 변수로 사용할 수 있음.
  • tf 파일에 output 블록으로 사용하면 됨.
  • terraform apply 후에 정의한 output들을 수집하여 state 파일에 저장함.
  • terraform output 명령어로도 확인 가능.

배경과 목표

이전에 input variable을 사용해 Terraform 구성을 매개 변수화했었음.

이번에는 output(출력값)을 사용하여 Terraform 사용자에게 유용한 정보를 확인해보자.

전제

이전 과정들에서 수행한 input variable 사용 관련 설정들.

initial configuration

앞서 learn-terraform-aws-instance 디렉토리에 아래와 같이 파일들을 생성했었고 init 한 후 apply 했음.

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "ap-northeast-2"
  profile = "learning-terraform"
}

resource "aws_instance" "app_server" {
  ami           = "ami-035233c9da2fabf52"
  instance_type = "t2.micro"

  tags = {
    # Name = "ExampleAppServerInstance"
    Name = var.instance_name
  }
}

# variables.tf
variable "instance_name" {
    description = "Value of the Name tag for the EC2 instance"
    type = string
    default = "ExampleAppServerInstance"
}
$ terraform init

$ terraform apply

EC2 instance conf의 output 정의하기

learn-terraform-aws-instance 디렉터리에 outputs.tf라는 파일을 생성하자.

아래 구성을 outputs.tf에 추가하여 EC2 인스턴스의 ID 및 IP 주소에 대한 출력을 정의하자.

outputs.tf

output "instance_id" {
  description = "ID of the EC2 instance"
  value       = aws_instance.app_server.id
}

output "instance_public_ip" {
  description = "Public IP address of the EC2 instance"
  value       = aws_instance.app_server.public_ip
}

output value 살펴보기

apply를 수행하고 받게되는 output을 살펴보자.

$ terraform apply 
aws_instance.app_server: Refreshing state... [id=i-04619ce49d8b2e9fc]

Changes to Outputs:
  + instance_id        = "i-04619ce49d8b2e9fc"
  + instance_public_ip = "1.2.3.4"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

instance_id = "i-04619ce49d8b2e9fc"
instance_public_ip = "1.2.3.4"

Terraform은 구성을 적용할 때 출력 값을 화면에 출력해줌.

apply 명령어에 의해 output 값이 state에 업데이트 되면 terraform output 명령으로 output을 쿼리할 수 있음.

$ terraform output
instance_id = "i-04619ce49d8b2e9fc"
instance_public_ip = "1.2.3.4"

infra 삭제

$ terraform destroy
aws_instance.app_server: Refreshing state... [id=i-04619ce49d8b2e9fc]

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.app_server will be destroyed
  - resource "aws_instance" "app_server" {
      - ami                                  = "ami-035233c9da2fabf52" -> null
...
          - volume_type           = "gp2" -> null
        }
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Changes to Outputs:
  - instance_id        = "i-04619ce49d8b2e9fc" -> null
  - instance_public_ip = "1.2.3.4" -> null

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.app_server: Destroying... [id=i-04619ce49d8b2e9fc]
aws_instance.app_server: Still destroying... [id=i-04619ce49d8b2e9fc, 10s elapsed]
aws_instance.app_server: Still destroying... [id=i-04619ce49d8b2e9fc, 20s elapsed]
aws_instance.app_server: Still destroying... [id=i-04619ce49d8b2e9fc, 30s elapsed]
aws_instance.app_server: Destruction complete after 30s

Destroy complete! Resources: 1 destroyed.
  • 앞서 설정한 output도 삭제됨.
profile
기록하는 엔지니어 되기 💪

0개의 댓글