-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
03-terraform-outputs-across-stacks/using-resource-data/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Example of sharing data between stacks with Terramate | ||
|
||
This example shows dynamic generation of the Terraform backend allowing us to use a `terraform_remote_state` data block that references a different stack by ID. Combined with using tags for ordering, this means we can easily reorganise our stacks without any breakage. | ||
|
||
To try this example you will need to edit the file `backend.tm.hcl` and change the bucket name to an existing bucket you have access to. |
30 changes: 30 additions & 0 deletions
30
03-terraform-outputs-across-stacks/using-resource-data/backend.tm.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
globals { | ||
aws_region = "us-west-2" | ||
name = "terramate-example-data-sharing" | ||
state_bucket = "CHANGE-THIS" | ||
} | ||
|
||
# generates the backend.tf in each stack | ||
generate_hcl "_backend.tf" { | ||
content { | ||
terraform { | ||
backend "s3" { | ||
bucket = global.state_bucket | ||
key = "state-files/stacks/${terramate.stack.id}/terraform.state" | ||
region = global.aws_region | ||
encrypt = true | ||
} | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
} | ||
|
||
provider "aws" { | ||
region = global.aws_region | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_backend.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT | ||
|
||
terraform { | ||
backend "s3" { | ||
bucket = "CHANGE-THIS" | ||
encrypt = true | ||
key = "state-files/stacks/9d2b1b15-0ea2-45cd-9573-90fe67bdafda/terraform.state" | ||
region = "us-west-2" | ||
} | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
} | ||
provider "aws" { | ||
region = "us-west-2" | ||
} |
10 changes: 10 additions & 0 deletions
10
03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_data.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT | ||
|
||
data "aws_subnet" "vpc" { | ||
filter { | ||
name = "tag:Name" | ||
values = [ | ||
"terramate-example-data-sharing", | ||
] | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] | ||
} | ||
|
||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
|
||
owners = ["099720109477"] # Canonical | ||
} | ||
|
||
resource "aws_instance" "web" { | ||
ami = data.aws_ami.ubuntu.id | ||
subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id | ||
instance_type = "t3.micro" | ||
|
||
tags = { | ||
Name = "HelloWorld" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/remote-state.tm.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
generate_hcl "_data.tf" { | ||
content { | ||
data "aws_subnet" "vpc" { | ||
filter { | ||
name = "tag:Name" | ||
values = [ | ||
global.name, | ||
] | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/stack.tm.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
stack { | ||
name = "ec2" | ||
description = "ec2" | ||
id = "9d2b1b15-0ea2-45cd-9573-90fe67bdafda" | ||
after = ["tag:vpc"] | ||
} |
19 changes: 19 additions & 0 deletions
19
03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_backend.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT | ||
|
||
terraform { | ||
backend "s3" { | ||
bucket = "CHANGE-THIS" | ||
encrypt = true | ||
key = "state-files/stacks/e4790733-c105-4870-bdf7-9ab67fcc655c/terraform.state" | ||
region = "us-west-2" | ||
} | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
} | ||
provider "aws" { | ||
region = "us-west-2" | ||
} |
16 changes: 16 additions & 0 deletions
16
03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_vpc-and-subnet.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT | ||
|
||
resource "aws_vpc" "main" { | ||
cidr_block = "10.0.0.0/16" | ||
instance_tenancy = "default" | ||
tags = { | ||
Name = "terramate-example-data-sharing" | ||
} | ||
} | ||
resource "aws_subnet" "main" { | ||
cidr_block = "10.0.1.0/24" | ||
tags = { | ||
Name = "terramate-example-data-sharing" | ||
} | ||
vpc_id = aws_vpc.main.id | ||
} |
6 changes: 6 additions & 0 deletions
6
03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/stack.tm.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
stack { | ||
name = "vpc" | ||
description = "vpc" | ||
id = "e4790733-c105-4870-bdf7-9ab67fcc655c" | ||
tags = ["vpc"] | ||
} |
20 changes: 20 additions & 0 deletions
20
03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/subnet.tm.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
generate_hcl "_vpc-and-subnet.tf" { | ||
content { | ||
resource "aws_vpc" "main" { | ||
cidr_block = "10.0.0.0/16" | ||
instance_tenancy = "default" | ||
|
||
tags = { | ||
Name = global.name | ||
} | ||
} | ||
resource "aws_subnet" "main" { | ||
vpc_id = aws_vpc.main.id | ||
cidr_block = "10.0.1.0/24" | ||
|
||
tags = { | ||
Name = global.name | ||
} | ||
} | ||
} | ||
} |