We are trying to use terraform with remote state stored in S3.
Projects are broken down, for example, there is a βmainβ VPC project that creates only network infrastructure (vpc, subnets, IGW, NAT, routes, etc.), as well as subprojects that create certain resources on top of the main vpc (subnet), that is ec2 nodes.
Project folders / files:
.
βββ modules/
β βββ mod-vpc/
β βββ main.tf
β βββ outputs.tf
β βββ variables.tf
βββ projects/
β βββ top-level-project-name-goes-here/
β βββ env-dev/
β β βββ globals.tf
β β βββ test/
β β β βββ main.tf
β β β βββ variables.tf
β β βββ vpc/
β β βββ main.tf
β β βββ variables.tf
β βββ env-prod/
βββ terraform.tfvars
With the exception of the VPC project, all other projects use vpc_id, CIDR, etc. From the remote state of the VPC. Here's how our process is defined:
Step 1: Create a VPC.
There are no problems here, a VPC is created, and the output is printed and saved in the S3 bucket:
$ terraform init -backend=s3 -backend-config="region= us-west-2" -backend-config="bucket=xxx" -backend-config="key=xxx" -backend-config="acl=bucket-owner-full-control" $project_path
$ terraform remote pull
$ terraform get $project_path
$ terraform apply
...
Outputs:
cidr_block = 10.198.0.0/16
private_subnet_ids = subnet-d3f5029a,subnet-fbeb369c,subnet-7ad88622
public_subnet_ids = subnet-54f5021d
region = us-west-2
vpc_id = vpc-b31ca3d4
vpc_name = main_vpc
2. : VPC, ec2 ( VPC 1 ). /, ( /tmp/project/working, ):
$ terraform init -backend=s3 -backend-config="region= us-west-2" -backend-config="bucket=xxx" -backend-config="key=xxx" -backend-config="acl=bucket-owner-full-control" $project_path
$ terraform remote pull
$ terraform get $project_path
$ terraform apply
/tmp/project/ :
( /tmp/project/folder):
βββ .terraform
β βββ modules
β β βββ 7d29d4ce6c4f98d8bcaa8b3c0ca4f8f1 -> /pathto/modules/mod-cassandra
β β βββ aa8ffe05b5d08913f821fdb23ccdfd95
β βββ terraform.tfstate
βββ globals.tf
βββ main.tf
βββ terraform.tfvars
βββ variables.tf
main.tf :
resource "aws_instance" "test" {
instance_type = "${var.instance_type}"
ami = "${var.ami}"
subnet_id = "${data.terraform_remote_state.vpc_main.public_subnet_ids}"
vpc_security_group_ids = ["${aws_security_group.http_ext.id}"]
}
data.terraform_remote_state:
data "terraform_remote_state" "vpc_main" {
backend = "s3"
config {
region = "us-west-2"
bucket = "xxx"
key = "xxx/vpc.json"
}
}
, ( ) "data.terraform_remote_state.vpc_main", :
1. "data.terraform_remote_state", "test" (= main.tf), .
2. data.terraform_remote_state.vpc_main (= "globals.tf"), [terraform get $ project_path]:
$ terraform init -backend=s3 -backend-config="region= us-west-2" -backend-config="bucket=xxx" -backend-config="key=xxx" -backend-config="acl=bucket-owner-full-control" $project_path
$ terraform remote pull
$ terraform get $project_path
Error loading Terraform: module root: 4 error(s) occurred:
* module 'cassandra': unknown resource 'data.terraform_remote_state.vpc_main' referenced in variable data.terraform_remote_state.vpc_main.cidr_block
* module 'cassandra': unknown resource 'data.terraform_remote_state.vpc_main' referenced in variable data.terraform_remote_state.vpc_main.region
* module 'cassandra': unknown resource 'data.terraform_remote_state.vpc_main' referenced in variable data.terraform_remote_state.vpc_main.vpc_id
* module 'cassandra': unknown resource 'data.terraform_remote_state.vpc_main' referenced in variable data.terraform_remote_state.vpc_main.public_subnet_ids
, Terraform - data.terraform_remote_state.vpc_main.
3. ( "globals.tf" "main.tf"), [terraform apply]:
$ terraform init -backend=s3 -backend-config="region= us-west-2" -backend-config="bucket=xxx" -backend-config="key=xxx" -backend-config="acl=bucket-owner-full-control" $project_path
$ terraform remote pull
$ terraform get $project_path
$ terraform apply
module root: 1 error(s) occurred:
2017/01/14 14:02:50 [DEBUG] plugin: waiting for all plugin processes to complete...
β’ data.terraform_remote_state.vpc_main: resource repeated multiple times
, , .
Terraform , 2 ?
terraform *.tf , , terraform :
https://www.terraform.io/docs/configuration/load.html
, , .
" " , Terraform "" ?