Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = global.name
Name = local.name
}
}

resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"

tags = {
Name = global.name
Name = "terramate-example-data-sharing"
}
vpc_id = aws_vpc.main.id
}

output "subnet_id" {
value = aws_subnet.main.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
generate_hcl "_terramate_generated_main.tf" {
content {
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = local.name
}
}

resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"

tags = {
Name = global.name
}
}

output "subnet_id" {
value = aws_subnet.main.id
}
}
}
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.
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
}
}
}
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"
}
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",
]
}
}
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"
}
}
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,
]
}
}
}
}
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"]
}
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"
}
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
}
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"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
generate_hcl "_terramate_generated_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
}
}
}
}