# 解決したいこと

外部ファイルから変数を呼ぶ際のエラーをなくしたい  
  

# 事象

`terraform plan`を実行すると下記エラーが出現。。。

**エラー内容**

```

    $ terraform plan    
    Error: Error loading Desktop\terraform\network.tf: Error reading config for 
    aws_internet_gateway[${var.GW_NAME}]: parse error at 1:11: expected "}" but 
    found invalid sequence "$"
    
```

  

# やりたいこと

  * variable.tf、prod.tfvarsに定義した変数をnetwork.tfが読み込む。
  * prod.tfvarsの変数を編集すれば、VPC名やCIDRブロックを任意の値に変更できるようにしたい。 
  * ゆくゆくはstg.tfvars, dev.tfvarsを作成し、`terraform apply -var-file=dev.tfvars`で環境毎にvar-fileを変えることで、各環境を作成したい。

  

# 構成

```

    $ ls -l
    total 34
    -rw-r--r-- 1 takahashi.kazuki 197121 1298 2月  14 17:52 network.tf　★ネットワーク設定用
    -rw-r--r-- 1 takahashi.kazuki 197121 1536 2月  14 17:48 prod.tfvars　★本番環境用変数
    -rw-r--r-- 1 takahashi.kazuki 197121  149 2月  14 15:53 provider.tf　★プロバイダー情報
    -rw-r--r-- 1 takahashi.kazuki 197121  484 2月  14 15:53 README.md
    -rw-r--r-- 1 takahashi.kazuki 197121 1425 2月  14 15:53 variables.tf　
    
```

  
  

# 対象ファイルの中身

## network.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}"
        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}"
        }
    
```

## prod.tfvars

```

    $ 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"
    
```

## variables.tf

```

    $ 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" {}
    
```
CC BY-SA 3.0
2018-02-14T09:18:13.927
41706
2020-05-01T00:04:20.407
27402
question
aws
Terraform planを実行するとエラーになる。（変数を呼び出せない？）
下記部分で変数の入れ子が行われています（ `"${aws_internet_gateway.${var.GW_NAME}.id}"` ）が、

```

    resource "aws_route_table" "${var.INTERNET_ROUTE}" {
        vpc_id = "${aws_vpc.${var.VPC_NAME}.id}"
        route {
            cidr_block = "${var.INTERNET_ROUTE_CIDR}"
            gateway_id = "${aws_internet_gateway.${var.GW_NAME}.id}"
        }
    }
    
```

Terraformでは変数の入れ子は現在不可能なようです。  
[nested variables, or 'Templatable' terraform module? · Issue #1114 ·
hashicorp/terraform](https://github.com/hashicorp/terraform/issues/1114#issuecomment-303475977)

> It will not be possible to interpolate into variable names within
> interpolation expressions; this would add far too much dynamism and make it
> hard or impossible to do the graph analysis Terraform requires to do its
> work.
CC BY-SA 4.0
2018-08-23T08:10:32.470
47761
2018-08-23T08:10:32.470
29826
41706
answer