From 275a60d7eb5489538df1585a09e3fd06b3369bff Mon Sep 17 00:00:00 2001 From: Kyle Knight Date: Wed, 10 Jan 2024 15:49:57 +0000 Subject: [PATCH] feat: two examples for sharing data --- .../{ => using-remote-state}/README.md | 0 .../{ => using-remote-state}/backend.tm.hcl | 0 .../stacks/EC2/_backend.tf | 0 .../stacks/EC2/_remote-state.tf | 0 .../stacks/EC2/main.tf | 0 .../stacks/EC2/remote-state.tm.hcl | 0 .../stacks/EC2/stack.tm.hcl | 0 .../stacks/VPC/_backend.tf | 0 .../stacks/VPC/main.tf | 0 .../stacks/VPC/stack.tm.hcl | 0 .../using-resource-data/README.md | 5 ++++ .../using-resource-data/backend.tm.hcl | 30 +++++++++++++++++++ .../stacks/EC2/_backend.tf | 19 ++++++++++++ .../using-resource-data/stacks/EC2/_data.tf | 10 +++++++ .../using-resource-data/stacks/EC2/main.tf | 25 ++++++++++++++++ .../stacks/EC2/remote-state.tm.hcl | 12 ++++++++ .../stacks/EC2/stack.tm.hcl | 6 ++++ .../stacks/VPC/_backend.tf | 19 ++++++++++++ .../stacks/VPC/_vpc-and-subnet.tf | 16 ++++++++++ .../stacks/VPC/stack.tm.hcl | 6 ++++ .../stacks/VPC/subnet.tm.hcl | 20 +++++++++++++ 21 files changed, 168 insertions(+) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/README.md (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/backend.tm.hcl (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/EC2/_backend.tf (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/EC2/_remote-state.tf (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/EC2/main.tf (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/EC2/remote-state.tm.hcl (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/EC2/stack.tm.hcl (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/VPC/_backend.tf (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/VPC/main.tf (100%) rename 03-terraform-outputs-across-stacks/{ => using-remote-state}/stacks/VPC/stack.tm.hcl (100%) create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/README.md create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/backend.tm.hcl create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_backend.tf create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_data.tf create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/main.tf create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/remote-state.tm.hcl create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/stack.tm.hcl create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_backend.tf create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_vpc-and-subnet.tf create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/stack.tm.hcl create mode 100644 03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/subnet.tm.hcl diff --git a/03-terraform-outputs-across-stacks/README.md b/03-terraform-outputs-across-stacks/using-remote-state/README.md similarity index 100% rename from 03-terraform-outputs-across-stacks/README.md rename to 03-terraform-outputs-across-stacks/using-remote-state/README.md diff --git a/03-terraform-outputs-across-stacks/backend.tm.hcl b/03-terraform-outputs-across-stacks/using-remote-state/backend.tm.hcl similarity index 100% rename from 03-terraform-outputs-across-stacks/backend.tm.hcl rename to 03-terraform-outputs-across-stacks/using-remote-state/backend.tm.hcl diff --git a/03-terraform-outputs-across-stacks/stacks/EC2/_backend.tf b/03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/_backend.tf similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/EC2/_backend.tf rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/_backend.tf diff --git a/03-terraform-outputs-across-stacks/stacks/EC2/_remote-state.tf b/03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/_remote-state.tf similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/EC2/_remote-state.tf rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/_remote-state.tf diff --git a/03-terraform-outputs-across-stacks/stacks/EC2/main.tf b/03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/main.tf similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/EC2/main.tf rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/main.tf diff --git a/03-terraform-outputs-across-stacks/stacks/EC2/remote-state.tm.hcl b/03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/remote-state.tm.hcl similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/EC2/remote-state.tm.hcl rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/remote-state.tm.hcl diff --git a/03-terraform-outputs-across-stacks/stacks/EC2/stack.tm.hcl b/03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/stack.tm.hcl similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/EC2/stack.tm.hcl rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/EC2/stack.tm.hcl diff --git a/03-terraform-outputs-across-stacks/stacks/VPC/_backend.tf b/03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/_backend.tf similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/VPC/_backend.tf rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/_backend.tf diff --git a/03-terraform-outputs-across-stacks/stacks/VPC/main.tf b/03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/main.tf similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/VPC/main.tf rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/main.tf diff --git a/03-terraform-outputs-across-stacks/stacks/VPC/stack.tm.hcl b/03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/stack.tm.hcl similarity index 100% rename from 03-terraform-outputs-across-stacks/stacks/VPC/stack.tm.hcl rename to 03-terraform-outputs-across-stacks/using-remote-state/stacks/VPC/stack.tm.hcl diff --git a/03-terraform-outputs-across-stacks/using-resource-data/README.md b/03-terraform-outputs-across-stacks/using-resource-data/README.md new file mode 100644 index 0000000..e3841a1 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/README.md @@ -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. diff --git a/03-terraform-outputs-across-stacks/using-resource-data/backend.tm.hcl b/03-terraform-outputs-across-stacks/using-resource-data/backend.tm.hcl new file mode 100644 index 0000000..2fdd594 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/backend.tm.hcl @@ -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 + } + } +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_backend.tf b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_backend.tf new file mode 100644 index 0000000..0529b80 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_backend.tf @@ -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" +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_data.tf b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_data.tf new file mode 100644 index 0000000..acd64da --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/_data.tf @@ -0,0 +1,10 @@ +// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT + +data "aws_subnet" "vpc" { + filter { + name = "tag:Name" + values = [ + "terramate-example-data-sharing", + ] + } +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/main.tf b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/main.tf new file mode 100644 index 0000000..baa1004 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/main.tf @@ -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" + } +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/remote-state.tm.hcl b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/remote-state.tm.hcl new file mode 100644 index 0000000..d82ca59 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/remote-state.tm.hcl @@ -0,0 +1,12 @@ +generate_hcl "_data.tf" { + content { + data "aws_subnet" "vpc" { + filter { + name = "tag:Name" + values = [ + global.name, + ] + } + } + } +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/stack.tm.hcl b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/stack.tm.hcl new file mode 100644 index 0000000..9ff6574 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/EC2/stack.tm.hcl @@ -0,0 +1,6 @@ +stack { + name = "ec2" + description = "ec2" + id = "9d2b1b15-0ea2-45cd-9573-90fe67bdafda" + after = ["tag:vpc"] +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_backend.tf b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_backend.tf new file mode 100644 index 0000000..7453ab5 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_backend.tf @@ -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" +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_vpc-and-subnet.tf b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_vpc-and-subnet.tf new file mode 100644 index 0000000..2cb30f9 --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/_vpc-and-subnet.tf @@ -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 +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/stack.tm.hcl b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/stack.tm.hcl new file mode 100644 index 0000000..022d96b --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/stack.tm.hcl @@ -0,0 +1,6 @@ +stack { + name = "vpc" + description = "vpc" + id = "e4790733-c105-4870-bdf7-9ab67fcc655c" + tags = ["vpc"] +} diff --git a/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/subnet.tm.hcl b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/subnet.tm.hcl new file mode 100644 index 0000000..4f0f1bd --- /dev/null +++ b/03-terraform-outputs-across-stacks/using-resource-data/stacks/VPC/subnet.tm.hcl @@ -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 + } + } + } +}