When I create an ECS cluster on the AWS UI, I have the option to select a VPC. But how do I select or attach an existing VPC while creating a cluster using Terraform?
resource "aws_ecs_cluster" "gtm" {
name = "gtm"
setting {
name = "containerInsights"
value = "enabled"
}
}
2
Answers
The VPC is not set at the cluster level, it is set at the ECS service level, by specifying the VPC subnets you want the service to use.
As @Mark mentioned, it is ECS service level.
network_configuration.subnets
helps to attach subnet. In the Terraform registry page (in Mark’s link), there is no explicit sample for network configuration, so I added it below.Sample Code: