I will really appreciate some help here as I don’t think it could be so complicated.
terraform -v
Terraform v0.12.6
- provider.aws v2.25.0
I have a root which contains 3 other modules ( VPC, subnet and instances).
The VPC and 2 subnets are getting created just fine . But when I add the instances module I get the following error -
Error: Invalid value for module argument
on main.tf line 60, in module “instances”:
60: subnet_id = data.terraform_remote_state.network.outputs.pub_sub_id
The given value is not suitable for child module variable “subnet_id” defined
at …/mod/instances/ec2.tf:2,1-21: string required.
Here are my files. For the sake of simplicity I have merged the output and variables in one file.
Thanks !
main.tf
variable "vpc_region" {
description = "AWS region"
default = "us-east-1"
}
# VPC Config
variable "vpc_name" {
description = "VPC for building demos"
default = "Test"
}
variable "vpc_cidr_block" {
description = "IP addressing for demo Network"
default = "10.0.0.0/16"
}
provider "aws" {
region = "us-east-1"
profile = "work"
shared_credentials_file="/Users/jim/.aws/credentials"
}
terraform {
backend "s3" {
bucket = "terraform"
key = "terraform/terraform.tfstate"
region = "us-east-1"
profile="work"
shared_credentials_file="/Users/jim/.aws/credentials"
}
}
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "terraform"
key = "terraform/terraform.tfstate"
region = "us-east-1"
profile="work"
shared_credentials_file="/Users/jim/.aws/credentials"
}
}
module "vpc" {
source = "../mod/vpc"
vpc_region = "${var.vpc_region}"
vpc_name = "${var.vpc_name}"
vpc_cidr_block = "${var.vpc_cidr_block}"
}
module "public_subnet" {
source = "../mod/pub_sub"
vpc_id = "${module.vpc.id}"
}
module "instances" {
source = "../mod/instances"
subnet_id = data.terraform_remote_state.network.outputs.pub_sub_id
vpc_id = "${module.vpc.id}"
}
output "id" {
value = "${module.vpc.id}"
}
output "pub_sub_id" {
value = "${module.public_subnet.*}"
}
vpc.tf
variable "vpc_region" {}
variable "vpc_name" {}
variable "vpc_cidr_block" {}
resource "aws_vpc" "primary_vpc" {
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = true
enable_dns_support =true
tags = {
Name = "${var.vpc_name}"
}
}
output "id" {
value = "${aws_vpc.primary_vpc.id}"
}
output "region" {
value = "${var.vpc_region}"
}
**pub_subnet.tf**
variable "sub_cdr" {
type="list"
default=["10.0.0.0/28", "10.0.0.16/28"]
}
variable "azs" {
type="list"
default=["us-east-1a","us-east-1b"]
}
data "aws_availability_zones" azs {}
variable "vpc_id" { }
resource aws_subnet "public_subnet"{
count=2
vpc_id="${var.vpc_id}"
cidr_block="${var.sub_cdr[count.index]}"
availability_zone="${data.aws_availability_zones.azs.names[count.index]}"
tags= {Name = "pub-sub-${count.index + 1}"}
}
output "pub_sub_id" {
value = "${aws_subnet.public_subnet.*.id}"
}
ec2.tf
variable “vpc_id” { }
variable “subnet_id” {
type = “string”
}
variable "instance_count" {
default=1
}
resource "aws_security_group" "pub_sg" {
vpc_id = "${var.vpc_id}"
name = "public-sg"
ingress {
from_port = 22
protocol = "tcp"
to_port = 22
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
protocol = "-1"
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "web-srvs" {
count="${var.instance_count == "0" ? "1" : var.instance_count}"
ami = "ami-035b3c7efe6d061d5"
instance_type = "t2.nano"
key_name="XXXXX"
subnet_id = "${var.subnet_id}"
vpc_security_group_ids = ["${aws_security_group.pub_sg.id}"]
associate_public_ip_address=true
tags = {
# Name = "${var.name}${var.instance_count == "0" ? "" : format("%02d", count.index + 1)}-${var.environment}"
# Application = "${var.environment}"
# Project = "${var.project}"
CountIndex = "${var.instance_count == "0" ? "" : format("%02d", count.index + 1)}"
}
}