Skip to content

Commit 5baab5d

Browse files
committed
updates to examples/docs per pr feedback
1 parent 63a6924 commit 5baab5d

File tree

8 files changed

+110
-117
lines changed

8 files changed

+110
-117
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ To use this bucket to manage the state for other AWS accounts, you must create I
2929

3030
See [Use AssumeRole to Provision AWS Resources Across Accounts](https://learn.hashicorp.com/tutorials/terraform/aws-assumerole) for more information on this pattern.
3131

32+
This module is not intended to hold the state for the account in which it is created. If the account itself is also Terraform managed, it is recommended to create a separate bucket for its own state manually or via a different IaC method (e.g., CloudFormation).
33+
34+
This module will create a CloudFormation stack and an optional wrapper script to deploy it. This stack is suitable to run in any account that will store its Terraform state in this backend. It creates an IAM role with the AdministratorAccess policy attached and with an External ID.
35+
3236
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
3337
## Requirements
3438

@@ -95,6 +99,7 @@ No modules.
9599

96100
| Name | Description |
97101
|------|-------------|
102+
| <a name="output_external_id"></a> [external\_id](#output\_external\_id) | External ID attached to IAM role in managed accounts |
98103
| <a name="output_kms_key_arn"></a> [kms\_key\_arn](#output\_kms\_key\_arn) | ARN of KMS Key for S3 bucket |
99-
| <a name="output_s3_bucket_backend"></a> [s3\_bucket\_backend](#output\_s3\_bucket\_backend) | S3 bucket |
104+
| <a name="output_s3_bucket_backend"></a> [s3\_bucket\_backend](#output\_s3\_bucket\_backend) | S3 bucket used to store TF state |
100105
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/basic/README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
# Basic Backend Example
2-
Creates resources for a secure backend in AWS
1+
# Basic Backend Example
2+
Creates resources for a secure backend in AWS to support separate AWS accounts. To use this backend, use the following provider definition:
3+
4+
```
5+
provider "aws" {
6+
region = var.region
7+
8+
assume_role {
9+
role_arn = "arn:aws-us-gov:iam::012345678901:role/Terraform"
10+
external_id = "YourExternalID"
11+
}
12+
}
13+
14+
You will need to run Terraform with IAM credentials of the account that holds the state rather than the accounts that you are working on.
315
416
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
517
## Requirements
@@ -8,23 +20,17 @@ No requirements.
820
921
## Providers
1022
11-
| Name | Version |
12-
|------|---------|
13-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
23+
No providers.
1424
1525
## Modules
1626
1727
| Name | Source | Version |
1828
|------|--------|---------|
19-
| <a name="module_backend"></a> [backend](#module\_backend) | ../.. | |
20-
| <a name="module_tags"></a> [tags](#module\_tags) | rhythmictech/tags/terraform | 1.0.0 |
29+
| <a name="module_tfstate"></a> [tfstate](#module\_tfstate) | ../.. | n/a |
2130
2231
## Resources
2332
24-
| Name | Type |
25-
|------|------|
26-
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
27-
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
33+
No resources.
2834
2935
## Inputs
3036

examples/basic/main.tf

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
data "aws_caller_identity" "current" {}
2-
data "aws_region" "current" {}
3-
4-
locals {
5-
env = "sandbox"
6-
name = "example"
7-
namespace = "aws-rhythmic-sandbox"
8-
owner = "Rhythmictech Engineering"
9-
10-
extra_tags = {
11-
delete_me = "please"
12-
purpose = "testing"
13-
}
14-
}
15-
16-
module "tags" {
17-
source = "rhythmictech/tags/terraform"
18-
version = "1.0.0"
19-
20-
names = [local.name, local.env, local.namespace]
21-
22-
tags = merge({
23-
"Env" = local.env,
24-
"Namespace" = local.namespace,
25-
"Owner" = local.owner
26-
}, local.extra_tags)
27-
}
28-
29-
module "backend" {
1+
module "tfstate" {
302
source = "../.."
313

32-
bucket = "${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}-${module.tags.name}"
33-
kms_alias_name = "${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}-${module.tags.name}"
34-
tags = module.tags.tags
4+
bucket_name = "tf-state-remote"
5+
create_assumerole_template = true
6+
dynamo_locktable_name = "tf-locktable-remote"
357
}

examples/external-logging/README.md

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/external-logging/main.tf

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/workspaces/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Workspaces Backend Example
2+
Creates resources for a secure backend in AWS to support multiple AWS accounts. To use this backend with accounts managed by Terraform workspaces, use the following provider definition and variable:
3+
4+
```
5+
provider "aws" {
6+
region = var.region
7+
8+
assume_role {
9+
role_arn = var.workspace_iam_roles[terraform.workspace].role_arn
10+
external_id = var.workspace_iam_roles[terraform.workspace].external_id
11+
}
12+
}
13+
14+
variable "workspace_iam_roles" {
15+
description = "IAM roles to assume"
16+
type = any
17+
}
18+
19+
```
20+
21+
Then define variable entries for each account:
22+
23+
```
24+
workspace_iam_roles = {
25+
dev = {
26+
role_arn = "arn:aws-us-gov:iam::012345678901:role/Terraform"
27+
external_id = "YourExternalID"
28+
}
29+
test = {
30+
role_arn = "arn:aws:iam::012345678902:role/Terraform"
31+
external_id = "YourExternalID"
32+
}
33+
prod = {
34+
role_arn = "arn:aws-us-gov:iam::012345678903:role/Terraform"
35+
external_id = "YourExternalID"
36+
}
37+
}
38+
```
39+
40+
You will need to run Terraform with IAM credentials of the account that holds the state rather than the accounts that you are working on.
41+
42+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
43+
## Requirements
44+
45+
No requirements.
46+
47+
## Providers
48+
49+
No providers.
50+
51+
## Modules
52+
53+
| Name | Source | Version |
54+
|------|--------|---------|
55+
| <a name="module_tfstate"></a> [tfstate](#module\_tfstate) | ../.. | n/a |
56+
57+
## Resources
58+
59+
No resources.
60+
61+
## Inputs
62+
63+
No inputs.
64+
65+
## Outputs
66+
67+
No outputs.
68+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/workspaces/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module "tfstate" {
2+
source = "../.."
3+
4+
bucket_name = "tf-state-remote"
5+
create_assumerole_template = true
6+
dynamo_locktable_name = "tf-locktable-remote"
7+
}

outputs.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
output "s3_bucket_backend" {
2-
description = "S3 bucket"
3-
value = aws_s3_bucket.this.bucket
1+
output "external_id" {
2+
description = "External ID attached to IAM role in managed accounts"
3+
value = local.external_id
44
}
55

66
output "kms_key_arn" {
77
description = "ARN of KMS Key for S3 bucket"
88
value = try(aws_kms_key.this[0].arn, var.kms_key_id)
99
}
10+
11+
output "s3_bucket_backend" {
12+
description = "S3 bucket used to store TF state"
13+
value = aws_s3_bucket.this.bucket
14+
}

0 commit comments

Comments
 (0)