Hi, I want to build a basic stack on AWS using terraform-aws-modules, I write an network/main.tf to start a network stack, this is my tf file:
Hi, I want to build a basic stack on AWS using terraform-aws-modules, I write an network/main.tf to start a network stack, this is my tf file:
provider "aws" { region = "us-west-1" profile = "dev" } terraform { backend "s3" { bucket = "my-state-bucket" key = "DR/network/terraform.tfstate" encrypt = true region = "us-east-1" } } module "vpc" { source = "terraform-aws-modules/vpc/aws" name = "my-vpc" cidr = "10.1.0.0/16" azs = ["us-west-1a", "us-west-1b"] private_subnets = ["10.1.1.0/19", "10.1.32.0/19"] public_subnets = ["10.1.96.0/19", "10.1.128.0/19"] enable_nat_gateway = true enable_vpn_gateway = true tags = { Terraform = "true" Environment = "DR" } } output "vpc_id" { description = "The ID of the VPC" value = "${module.vpc.vpc_id}" } output "private_subnets" { description = "List of IDs of private subnets" value = "${module.vpc.*.private_subnets}" } output "public_subnets" { description = "List of IDs of public subnets" value = "${module.vpc.*.public_subnets}" }
and here my ec2 instance tf file
provider "aws" { region = "us-west-1" profile = "dev" } terraform { backend "s3" { bucket = " key = "DR/instances/terraform.tfstate" encrypt = true region = "us-east-1" } } data "terraform_remote_state" "network" { backend = "s3" config = { bucket = "my-state-bucket" key = "DR/network/terraform.tfstate" region = "us-east-1" } } data "terraform_remote_state" "misc" { backend = "s3" config = { bucket = "my-state-bucket" key = "DR/misc/terraform.tfstate" region = "us-east-1" } } variable "ami_base" { default = "ami-0245d318c6788de52" } variable "instance_type" { default = "t2.micro" } module "ec2_cluster" { source = "terraform-aws-modules/ec2-instance/aws" version = "~> 2.0" name = "My EC2" instance_count = 1 ami = var.ami_base instance_type = var.instance_type key_name = data.terraform_remote_state.misc.outputs.keypair monitoring = true vpc_security_group_ids = ["sg-12345678"] subnet_id = element(data.terraform_remote_state.network.outputs.private_subnets, 0) tags = { Terraform = "true" Environment = "DR" } }
but when I run terraform plan
I got this error:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. data.terraform_remote_state.misc: Refreshing state... data.terraform_remote_state.network: Refreshing state... Error: Invalid value for module argument on ec2.tf line 53, in module "ec2_cluster": 53: subnet_id = element(data.terraform_remote_state.network.outputs.private_subnets, 0) The given value is not suitable for child module variable "subnet_id" defined at .terraform/modules/ec2_cluster/terraform-aws-modules-terraform-aws-ec2-instance-9fe491f/variables.tf:75,1-21: string required.
how can I select one of this two private subnets to my instance in Terraform 0.12.10? I’d like to use this with remote state.
Thanks