Hello I am trying to create AWS VPC Endpoint using terraform.
I am using a yaml file as input file. I am trying to create AWS VPC ep, which needs subnets and security_groups as lists. I am trying below but its not working. Could you please suggest a better approach
This is my Yaml Input File
vpc_endpoint:
prd:
jira:
service: XXXX
type: Interface
vpc: prd_vpc
security_groups:
- endpoint1
subnet_ids:
- prd_vpc_sub1
- prd_vpc_sub2
This is my vars.tf file for EP Section
vpc_eps = yamldecode(file("${var.config_yaml_file}"))["vpc_endpoint"]["${var.env}"]
vpc_ep_info = flatten([
for k,v in local.vpc_eps : {
ep_name = k
vpc_id = v.vpc
service_name = v.service
vpc_endpoint_type = v.type
security_group_ids = v.security_groups
subnet_ids = v.subnet_ids
}
])
I am trying this in vpc.tf file
resource "aws_vpc_endpoint" "aws_vpc_endpoint" {
for_each = {
for element in local.vpc_ep_info : element.ep_name => element
}
vpc_id = aws_vpc.aws_vpc[each.value.vpc_id].id
service_name = each.value.service_name
vpc_endpoint_type = each.value.vpc_endpoint_type
security_group_ids = formatlist("aws_security_group.aws_security_group[%s].id", each.value.security_group_ids)
subnet_ids = formatlist("aws_subnet.aws_subnet[%s].id", each.value.subnet_ids)
private_dns_enabled = true
tags = {
Name = each.value.ep_name
}
}
But it is treating formatlist output as string, instead of object.
So “terraform plan” works, but not “terraform apply”
Pls suggest a better way of doing this.