I would like to create a simple configuration of VPC and Subnet in the AWS environment with Terraform.
It seems that the vpc_id created by VPC is not passed well to Subnet, so
I would appreciate it if you could give me some advice on how to solve this problem.
For configuration, assume multiple resources are created. We are calling using for_each and lookup.
$terraform plan-var-file=prd/resource-prd-vars.tfvars
module.create_vpc_module.aws_vpc.vpc ["vpc01"]—Refreshing state... [id=vpc-0419f19a0980c568f]
Error: Invalid index
on..\modules\vpc\vpc-main.tf line 15, in resource "aws_subnet" "subnet":
15: vpc_id =aws_vpc.vpc [each.key].id
|----------------
| aws_vpc.vpc is object with 1 attribute "vpc01"
| each.key is "subnet01"
The given key does not identify an element in this collection value.
■ directory configuration
V - VPC
--modules
│ v-vpc
│ │ output.tf
│ │ vpc-main.tf
│ │ vpc-variavles.tf
│
--resource
│ │ backend.tf
│ │ provider.tf
│ │ resource-main.tf
│ │ resource-variables.tf
│ terraform.tfstate
│ terraform.tfstate.backup
--prd
resource-prd-vars.tfvars
■modules/vpc/vpc-main.tf
#-----------------------------------------------
# VPC
# ---------------------------------------------
resource "aws_vpc" "vpc" {
for_each=var.vpc
cidr_block=lookup(each.value, "cidr_block", null)
tags = lookup (each.value, "tags", null)
}
# ---------------------------------------------
# Subnet
# ---------------------------------------------
resource "aws_subnet" "subnet" {
for_each=var.subnet
vpc_id =aws_vpc.vpc [each.key].id
availability_zone=lookup(each.value, "availability_zone", null)
cidr_block=lookup(each.value, "cidr_block", null)
map_public_ip_on_launch=lookup(each.value, "map_public_ip_on_launch", null)
tags = lookup (each.value, "tags", null)
}
■modules/vpc/vpc-variables.tf
#-----------------------------------------------
# Variables Used When Creating VPCs
# ---------------------------------------------
variable "vpc" {
default={}
}
# ---------------------------------------------
# Variables Used When Creating Subnet
# ---------------------------------------------
variable "subnet" {
default={}
}
■ resource /resource-main.tf
#-----------------------------------------------
# Invoking Various Modules
# ---------------------------------------------
module "create_vpc_module" {
source="../modules/vpc"
vpc = var.vpc
subnet=var.subnet
}
■ resource /resource-variables.tf
#-----------------------------------------------
# Invoking Variables
# ---------------------------------------------
variable "vpc" {}
variable "subnet" {}
■ resource /prd /resource-prd-vars.tfvars
#vpctfvars
# actual variable value declaration
# ---------------------------------------------
# VPC
# ---------------------------------------------
vpc = {
vpc01 = {
description="vpc for testing"
cidr_block="10.0.0.0/16"
tags = {
"Name": "prd-vpc",
"Env": "prd"
}
}
}
subnet = {
subnet01 = {
availability_zone="ap-northeast-1a"
cidr_block="10.0.0.0/20"
map_public_ip_on_launch="true"
tags = {
"Name": "subnet",
"Env": "prd"
}
}
}
Use output to pass values to module, data format, etc.
I tried, but it doesn't seem to have been received properly.
terraform_version": "0.14.6"
aws terraform
resource "aws_subnet" "subnet" {
for_each=var.subnet
vpc_id =aws_vpc.vpc [each.key].id
The variable subnet
key is subnet01
, so each.key
returns subnet01
.
Therefore, vpc_id=aws_vpc.vpc["subnet01"].id
but vpc_id=aws_vpc.vpc["vpc01"].id
I don't know if it fits the questioner's design intentions, but if you modify .tfvars and match the key of the variable vpc
with the subnet
, it should work (not verified).
vpc={
01 = {
...
}
}
subnet = {
01 = {
...
}
}
© 2024 OneMinuteCode. All rights reserved.