๐Ÿ“‚ IaC/Terraform

[Terraform] ์˜์กด์„ฑ(dependency) ๊ด€๋ฆฌ

dhyuck 2022. 9. 5. 00:39
๋ฐ˜์‘ํ˜•

dependency(์˜์กด์„ฑ)

  • ๋ฆฌ์†Œ์Šค๋‚˜ ๋ชจ๋“ˆ๋กœ ์ƒ์„ฑํ•œ ์ž์› ๊ฐ„์—๋Š” ์˜์กด์„ฑ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ…Œ๋ผํผ์€ ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ ์ž์›์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆœ์„œ๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ์ข…์†์„ฑ์€ implicit dependency(์•”์‹œ์  ์˜์กด์„ฑ)๊ณผ explicit dependency(๋ช…์‹œ์  ์˜์กด์„ฑ)์ด ์žˆ์Šต๋‹ˆ๋‹ค.

implicit dependency(์•”์‹œ์  ์˜์กด์„ฑ)

  • ์•”์‹œ์  ์ข…์†์„ฑ์€ ํ…Œ๋ผํผ์ด ๋ฆฌ์†Œ์Šค๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด aws_eip.ip์—์„œ aws_instance.example_a.id๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ์•”์‹œ์  ์ข…์†์„ฑ์ž…๋‹ˆ๋‹ค.

      resource "aws_instance" "example_a" {
        ami           = "ami-05fa00d4c63e32376"
        instance_type = "t2.micro"
      }
    
      resource "aws_eip" "ip" {
        vpc      = true
        instance = aws_instance.example_a.id
      }

explicit dependency(๋ช…์‹œ์  ์˜์กด์„ฑ)

  • ๋ช…์‹œ์  ์˜์กด์„ฑ์€ depends_on์œผ๋กœ ๋ฆฌ์†Œ์Šค ๊ฐ„์˜ ์˜์กด์„ฑ์„ ๋ช…์‹œํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด EC2๊ฐ€ ํŠน์ • S3 ๋ฒ„ํ‚ท์„ ์‚ฌ์šฉํ•˜์—ฌ S3๊ฐ€ EC2๋ณด๋‹ค ๋จผ์ € ์ƒ์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด depends_on์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

      resource "aws_s3_bucket" "example" { }
    
      resource "aws_instance" "example_c" {
        ami           = data.aws_ami.amazon_linux.id
        instance_type = "t2.micro"
    
        depends_on = [aws_s3_bucket.example]
      }

๋ฐ˜์‘ํ˜•

'๐Ÿ“‚ IaC > Terraform' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Terraform 101 Study - 1์ฃผ์ฐจ(1) ๋‚ด์šฉ์ •๋ฆฌ  (0) 2023.07.09
[Terraform] Provider  (0) 2022.09.09
[Terraform] data source  (0) 2022.09.04
[Terraform] Output ์ถœ๋ ฅ ๋ณ€์ˆ˜  (0) 2022.09.03
[Terraform] Variable ์ž…๋ ฅ ๋ณ€์ˆ˜  (0) 2022.09.03