Terraform 스터디그룹 1주차 정리 - 3

DevOps Engineer·2023년 9월 2일
0

T101-1week

목록 보기
3/3

과제1) Terraform으로 AWS EC2 생성해보기

provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_instance" "example" {
  ami                    = "ami-0c9c942bd7bf113a2"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.instance.id]

  user_data = <<-EOF
              #!/bin/bash
              echo "t101-1week-captaincheoni" > index.html
              nohup busybox httpd -f -p 80 &
              EOF

  user_data_replace_on_change = true

  tags = {
    Name = "Single-WebSrv"
  }
}

resource "aws_security_group" "instance" {
  name = var.security_group_name

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

variable "security_group_name" {
  description = "The name of the security group"
  type        = string
  default     = "terraform-example-instance"
}

output "public_ip" {
  value       = aws_instance.example.public_ip
  description = "The public IP of the Instance"
}

위와 같이 만들어서 접속해봤음

위를 추가적으로 main.tf에 작성하고 user-data 변경하고 apply 해보세요!

리소스에 추가적으로 정의하는 인수

병렬적으로 동작하는 경우

resource "local_file" "abc" {
  content  = "123!"
  filename = "${path.module}/abc.txt"
}

resource "local_file" "def" {
  content  = "456!"
  filename = "${path.module}/def.txt"
}

종속적인 관계 resource

resource "local_file" "abc" {
  content  = "123!"
  filename = "${path.module}/abc.txt"
}

resource "local_file" "def" {
  content  = local_file.abc.content    # 123! 
  filename = "${path.module}/def.txt"
}

abc가 생성이 되어야 그 안의 content 값이 def의 content를 결정함
참조는 인수와 속성만 가능하며 terraform에서 인수와 속성은
resource "리소스 유형" "이름" {
인수(arguments) = 값
속성(attributes) = 값
}
리소스 유형.이름.인수
리소스 유형.이름.속성

depends_on으로 작성할 경우

resource "local_file" "abc" {
  content  = "123!"
  filename = "${path.module}/abc.txt"
}

resource "local_file" "def" {
  depends_on = [
    local_file.abc
  ]

  content  = "456!"
  filename = "${path.module}/def.txt"
}

수명주기

  • create_before_destroy: 리소스 생성 시 신규 리소스 생성 후 기존 리소스 삭제
  • prevent_destroy: 해당 리소스를 삭제하려할 때 명시적으로 거부
  • ignore_change: 리소스 요소에 선언된 인수의 변경사항을 테라폼 실행 시 거부
  • precondition: 리소스 요소에 선언해 인수의 조건을 검증
  • postcondition: Apply 이후의 결과를 속성 값으로 검증

create_before_destroy

  • true 적용했을 때
resource "local_file" "abc" {
  content  = "lifecycle - step 2222222"
  filename = "${path.module}/abc.txt"

  lifecycle {
    create_before_destroy = true
  }
}
  • false 적용했을 때
resource "local_file" "abc" {
  content  = "lifecycle - step 22222223333"
  filename = "${path.module}/abc.txt"

  lifecycle {
    create_before_destroy = false
  }
}

prvent_destroy

  • true
resource "local_file" "abc" {
  content  = "lifecycle - step change"
  filename = "${path.module}/abc.txt"

  lifecycle {
    prevent_destroy = true
  }
}

ignore_changes

resource "local_file" "abc" {
  content  = "lifecycle - step 44"
  filename = "${path.module}/abc.txt"

  lifecycle {
    ignore_changes = []
  }
}

ignore_change 적용하기 전에 apply

resource "local_file" "abc" {
  content  = "lifecycle - step 666"
  filename = "${path.module}/abc.txt"

  lifecycle {
    ignore_changes = [
        content
    ]
  }
}

적용하고
응답값을 확인하면

변화에 대한 결과값을 무시하고 적용됨 정상적임!

precondition

variable "file_name" {
  default = "step0.txt"
}

resource "local_file" "abc" {
  content  = "lifecycle - step 6"
  filename = "${path.module}/${var.file_name}"

  lifecycle {
    precondition {
      condition     = var.file_name == "step6.txt"
      error_message = "file name is not \"step6.txt\""
    }
  }
}

file_name.default= step6.txt여야 정상적으로 apply 됨

postcondition

resource "local_file" "abc" {
  content  = "step7채워보기"
  filename = "${path.module}/step7.txt"

  lifecycle {
    postcondition {
      condition     = self.content != ""
      error_message = "content cannot empty"
    }
  }
}

output "step7_content" {
  value = local_file.abc.id
}

에러

content 값을 채운 후

값을 무조건 넣어야 동작함!
재밌따!

profile
madame의 Techblog

0개의 댓글