Terraform을 활용한 자동화 구성 관리(22.04.25)

박민선·2022년 4월 25일
0

반복

https://www.terraform.io/language/meta-arguments/count

resource "aws_instance" "server" {
  count = 4 # create four similar EC2 instances

  ami           = "ami-a1b2c3d4"
  instance_type = "t2.micro"

  tags = {
    Name = "Server ${count.index}"
  }
}

count.index

resource "aws_eip" "app_server_eip" {
  count = var.instance_count

  instance = aws_instance.app_server[count.index].id
  vpc      = true

  tags = local.common_tags
}

output 블록에서는 count 아규먼트를 사용할 수 없음

modulo / modular

%

M % N = R(emain: 나머지)
M mod N = R

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3

  subnet_id              = module.app_vpc.public_subnets[count.index % length(module.app_vpc.public_subnets)]

데이터 소스

프로바이더에서 정보를 가져온다.

data "aws_ami" "example" {
  executable_users = ["self"]
  most_recent      = true
  name_regex       = "^myami-\\d{3}"
  owners           = ["self"]

  filter {
    name   = "name"
    values = ["myami-*"]
  }

  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

참조

data.aws_ami.example.id

백엔드(Backend)

현재 사용하고 있는 백엔드: Local Backend

s3, dynamo DB 를 이용한 원격 백엔드
https://www.terraform.io/language/settings/backends/s3

테라폼 클라우드 백엔드

terraform {
  backend "remote" {
    organization = "example_corp"

    workspaces {
      name = "my-app-prod"
    }
  }
}

profile
클라우드신생아

0개의 댓글