Skip to content

Commit 9f86972

Browse files
committed
feat: create iam policy to access state backend
1 parent 3947dd3 commit 9f86972

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ To use this project run `make init`
1717
To verify it all worked, run `aws s3 ls s3://${TF_BUCKET}/tfstate-backend/terraform.tfstate` it should display the `terraform.tfstate` file and date when it was created.
1818

1919
You can overwrite where this module stores its state file by setting `TF_STATE_KEY` when running `make init`
20+
21+
### IAM
22+
23+
It might be useful to you to generate an IAM policy which grants access to the state backend and dynamodb table. This policy can be attached to users or roles. Additionally this module exposes an output called `tfstate_backend_access_policy_json` which contains the policy document. The `tfstate_backend_access_policy_arn` output contains the created IAM policy ARN.

iam.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
data "aws_iam_policy_document" "access" {
3+
count = var.create_iam_policy ? 1 : 0
4+
5+
statement {
6+
effect = "Allow"
7+
actions = ["s3:PutObject", "s3:GetObject"]
8+
resources = [format("%s/*", module.tfstate_backend.s3_bucket_arn)]
9+
}
10+
11+
statement {
12+
effect = "Allow"
13+
actions = ["s3:ListBucket"]
14+
resources = [module.tfstate_backend.s3_bucket_arn]
15+
}
16+
17+
statement {
18+
effect = "Allow"
19+
actions = ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:DeleteItem"]
20+
resources = [module.tfstate_backend.dynamodb_table_arn]
21+
}
22+
}
23+
24+
resource "aws_iam_policy" "state_access" {
25+
count = var.create_iam_policy ? 1 : 0
26+
name = module.label.id
27+
policy = join("", data.aws_iam_policy_document.access.*.json)
28+
description = "Grants access to State Bucket and Locking Table for backend ${module.tfstate_backend.s3_bucket_id}"
29+
}
30+

main.tf

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
terraform {
22
required_version = ">= 0.11.2"
33
backend "s3" {}
4+
5+
required_providers {
6+
aws = "~> 2.50"
7+
}
48
}
59

6-
provider "aws" {
7-
version = "~> 2.25"
10+
module "label" {
11+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0"
12+
namespace = var.namespace
13+
name = var.name
14+
attributes = var.attributes
15+
tags = var.tags
16+
stage = var.stage
817
}
918

1019
module "tfstate_backend" {
11-
source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=tags/0.9.0"
20+
source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=tags/0.16.0"
1221
namespace = var.namespace
1322
name = var.name
14-
attributes = var.attributes
1523
tags = var.tags
16-
region = var.aws_region
17-
force_destroy = var.force_destroy
1824
stage = var.stage
25+
context = module.label.context
26+
force_destroy = var.force_destroy
27+
region = var.aws_region
1928
profile = var.aws_profile
2029
}

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ output "tfstate_backend_dynamodb_table_arn" {
2222
value = module.tfstate_backend.dynamodb_table_arn
2323
}
2424

25+
output "tfstate_backend_access_policy_arn" {
26+
value = join("", aws_iam_policy.state_access.*.arn)
27+
}
28+
29+
output "tfstate_backend_access_policy_json" {
30+
value = join("", data.aws_iam_policy_document.access.*.json)
31+
}

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ variable "aws_profile" {
4343
description = "AWS profile name as set in the shared credentials file"
4444
}
4545

46+
variable "create_iam_policy" {
47+
type = bool
48+
default = false
49+
description = "Creates an IAM policy to attach to users or roles to grant access to dynamodb and s3 bucket"
50+
}

0 commit comments

Comments
 (0)