Skip to content

Commit

Permalink
chore: updating and adding some outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
gambol99 committed Feb 16, 2025
1 parent 42931dd commit be4bad0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
21 changes: 21 additions & 0 deletions modules/remote_state/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@ data "aws_caller_identity" "current" {}

## Lookup current region
data "aws_region" "current" {}

## Retrieve the remote state
data "terraform_remote_state" "this" {
backend = "s3"

config = {
bucket = local.tf_state_bucket
key = local.tf_state_key

## We can assume via the web identity token if it is provided
assume_role_with_web_identity = var.web_identity_token_file != null ? {
role_arn = local.role_arn
web_identity_token_file = var.web_identity_token_file
} : null

## We can assume via the reader role if no web identity token is provided
assume_role = var.web_identity_token_file == null ? {
role_arn = local.role_arn
} : null
}
}
5 changes: 4 additions & 1 deletion modules/remote_state/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ locals {
region = coalesce(var.region, data.aws_region.current.name)

## Terraform state bucket name
tf_state_bucket = format("%s-%s", local.account, local.region)
tf_state_bucket = format("%s-%s-tfstate", local.account, local.region)

## Terraform state bucket key
tf_state_key = format("%s.tfstate", var.repository)

## Remote state role
role_arn = var.reader_role != null ? format("arn:aws:iam::%s:role/%s", local.account, var.reader_role) : format("arn:aws:iam::%s:role/%s-sr", local.account, var.repository)
Expand Down
18 changes: 0 additions & 18 deletions modules/remote_state/main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
data "terraform_remote_state" "this" {
backend = "s3"

config = {
bucket = format("%s-tfstate", local.tf_state_bucket)
key = format("%s.tfstate", var.repository)

## We can assume via the web identity token if it is provided
assume_role_with_web_identity = var.web_identity_token_file != null ? {
role_arn = local.role_arn
web_identity_token_file = var.web_identity_token_file
} : null

## We can assume via the reader role if no web identity token is provided
assume_role = var.web_identity_token_file == null ? {
role_arn = local.role_arn
} : null
}
}
13 changes: 12 additions & 1 deletion modules/remote_state/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
output "outputs" {
value = data.terraform_remote_state.this.outputs
description = "The outputs from the terraform_remote_state data source."
value = data.terraform_remote_state.this.outputs
}

output "bucket_name" {
description = "The name of the S3 bucket where the Terraform state is stored."
value = local.tf_state_bucket
}

output "bucket_key" {
description = "The key of the S3 bucket where the Terraform state is stored."
value = local.tf_state_key
}

0 comments on commit be4bad0

Please sign in to comment.