본문 바로가기

퍼블릭 클라우드

[Terraform] 테라폼 concat 함수(function)

반응형

테라폼 concat 함수(function)

 : concat은 둘 이상의 목록을 가져와서 단일 목록으로 결합합니다.

instance 모듈

instance.tf 편집

resource "aws_instance" "instance" {
  count = var.instance_count
  instance_type = var.instance_type
  ami = lookup(var.aws_amis, var.aws_region)
  key_name = var.key_name
  subnet_id = element(var.subnet_id, count.index)
  vpc_security_group_ids = var.vpc_security_group_ids
  associate_public_ip_address = var.associate_public_ip_address
  monitoring = true
  root_block_device {
    volume_size = var.disk_size
  }
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    Name = "${var.name}${count.index+1}"
    Group = "group-${var.group_name}"
    Environment = "env-${var.env}"
    CreateUser = "terraform"
    Owner = "admin@4wxyz.com"
    Role = "ec2"
    Service = "instance"
    CreatedDate = timestamp()
  }
}



###################################################
##################### OUTPUTS #####################
###################################################
output "instance_info" {
value = concat([aws_instance.instance.*.private_ip, aws_instance.instance.*.public_ip, aws_instance.instance.*.id])
}

output "instance_info" {
value = concat([aws_instance.instance.*.private_ip, aws_instance.instance.*.public_ip, aws_instance.instance.*.id])
}

instance 메인

main.tf 편집

module "ec2-web" {
    source = "../../../module/ec2/instance/"
    env = "prod"
    instance_count = 2
    name = "web1"
    group_name = "web"
    #aws_amis = "ami-0742b4e673072066f"
    aws_region = "us-east-1"
    instance_type = "t2.micro"
    disk_size = "8"
    key_name = "4wxyz"
    subnet_id = ["subnet-0381d3", "subnet-00be79"]
    vpc_security_group_ids = ["sg-077e2a", "sg-0c261f"]
    associate_public_ip_address = true
}

###################################################
##################### OUTPUTS #####################
###################################################
#####EC2 정보
output "aws_instance_info" {
    description = "인스턴스 정보"
    value = module.ec2-web.instance_info
}

output "aws_instance_info" {
    description = "인스턴스 정보"
    value = module.ec2-web.instance_info
}

terraform apply

$ terraform apply
...
Outputs:

aws_instance_info = [
  [
    "10.31.xxx.xxx",
    "10.31.xxx.xxx",
  ],
  [
    "23.228.xxx.xxx",
    "52.54.xxx.xxx",
  ],
  [
    "i-0cbcf7",
    "i-0a4d89",
  ],
]

 

concat Function

 

concat - Functions - Configuration Language - Terraform by HashiCorp

The concat function combines two or more lists into a single list.

www.terraform.io

Built-in Functions

 

Functions - Configuration Language - Terraform by HashiCorp

The Terraform language has a number of built-in functions that can be called from within expressions to transform and combine values.

www.terraform.io

 

 

728x90
반응형