I want to eliminate errors when calling variables from external files
terraform plan
causes the following error...
Error Contents
$terraform plan
Error:Error loading Desktop\terraform\network.tf:Error loading config for
aws_internet_gateway [${var.GW_NAME}]:parse error at 1:11:expected"}"but
found invalid sequence "$"
terraform apply-var-file=dev.tfvars
to create each environment.
$ls-l
total34
-rw-r --r --1 takahashi.kazuki 197121 1298 February 14 17:52 network.tf ★ For network setup
-rw -r --r --1 takahashi.kazuki 197121 1536 February 14 17:48 prod.tfvars ★Production Variables
-rw-r --r --1 takahashi.kazuki 197121 149 February 14 15:53 provider.tf ★ Provider Information
-rw -r --r --1 takahashi.kazuki 197121484 February 14 15:53 README.md
-rw -r --r --1 takahashi.kazuki 197121 1425 February 14 15:53 variables.tf
$cat network.tf
resource "aws_vpc" "${var.VPC_NAME}" {
cidr_block="${var.VPC_CIDR}"
instance_tenancy="default"
enable_dns_support="true"
enable_dns_hostnames="false"
tags{
Name = "${var.VPC_NAME}"
}
}
resource "aws_Internet_gateway" "${var.GW_NAME}" {
vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
}
resource "aws_subnet" "${var.SUBNET1_NAME}" {
vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
cidr_block="${var.SUBNET1_CIDR}"
availability_zone="${var.SUBMET1_AZZONE}"
}
resource "aws_subnet" "${var.SUBNET2_NAME}" {
vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
cidr_block="${var.SUBNET2_CIDR}"
availability_zone="${var.SUBNET2_AZZONE}"
}
resource "aws_route_table" "${var.INTERNET_ROUTE}" {
vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
US>route{
cidr_block="${var.INTERNET_ROUTE_CIDR}"
gateway_id="${aws_Internet_gateway.${var.GW_NAME}.id}"
}
}
resource "aws_route_table_association" "${var.SUBNET1_NAME}" {
subnet_id = "${aws_subnet.${var.SUBNET1_NAME}.id}"
route_table_id = "${aws_route_table.${var.INTERNET_ROUTE}.id}"
}
resource "aws_route_table_association" "${var.SUBNET2_NAME}" {
subnet_id = "${aws_subnet.${var.SUBNET2_MAME}.id}"
route_table_id = "${aws_route_table.${var.INTERNET_ROUTE}.id}"
}
$cat prod.tfvars
########
## network.tf
VPC_NAME="tf-test-network"
VPC_CIDR="192.168.0.0/16"
GW_NAME="tf-test-internet-gw"
SUBNET1_NAME="prod-tf-test-subnet-1a" #this subnet in ap-northeast-1a for Internet
SUBNET1_CIDR="192.168.10.0/24"
SUBNET1_AZZONE = "ap-northeast-1a"
SUBNET2_NAME="prod-tf-test-subnet-1c" #this subnet in ap-northeast-1c for Internet
SUBNET2_CIDR="192.168.110.0/24"
SUBNET2_AZZONE = "ap-northeast-1c"
INTERNET_ROUTE = "prod-tf-test-internet-route"
INTERNET_ROUTE_CIDR = "0.0.0.0/0"
$cat variables.tf
########
## network.tf
variable "VPC_NAME" {}
variable "VPC_CIDR" {}
variable "GW_NAME" {}
variable "SUBNET1_NAME" {}#this subnet in ap-northeast-1a for Internet
variable "SUBNET1_CIDR" {}
variable "SUBNET1_AZZONE" {}
variable "SUBNET2_NAME" {}#this subnet in ap-northeast-1c for Internet
variable "SUBNET2_CIDR" {}
variable "SUBNET2_AZZONE" {}
variable "INTERNET_ROUTE" {}
variable "INTERNET_ROUTE_CIDR" {}
Variables are nested in the following sections ("${aws_Internet_gateway.${var.GW_NAME}.id}"
), but
resource "aws_route_table" "${var.INTERNET_ROUTE}" {
vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
US>route{
cidr_block="${var.INTERNET_ROUTE_CIDR}"
gateway_id="${aws_Internet_gateway.${var.GW_NAME}.id}"
}
}
It seems that nesting variables is currently not possible in Terraform.
nested variables, or 'Templatable' terraform module?·Issue#1114 ·hashicorp/terraform
It will not be possible to interpret into variable names with in interpolation expressions; this would add far too much dynamic and make it hard or impossible to do the graph analysis Terraform requirements to do tasks work.
© 2024 OneMinuteCode. All rights reserved.