Skip to content

Commit

Permalink
feat: two examples for sharing data
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmlr committed Jan 10, 2024
1 parent 5f465ed commit 275a60d
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 0 deletions.
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 "_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
}
}
}
}

0 comments on commit 275a60d

Please sign in to comment.