Skip to content

Commit fdc40f8

Browse files
committed
refactor: Use data source for Terraform policy
1 parent 8deb15d commit fdc40f8

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ See [the official document](https://www.terraform.io/docs/backends/types/s3.html
7777

7878
- Starting from v1.0, this module requires [Terraform Provider for AWS](https://github.com/terraform-providers/terraform-provider-aws) v4.0 or later. [Version 1.0 Upgrade Guide](./docs/upgrade-1.0.md) described the recommended procedure after the upgrade.
7979

80-
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
80+
<!-- BEGIN_TF_DOCS -->
8181
## Requirements
8282

8383
| Name | Version |
@@ -112,8 +112,8 @@ See [the official document](https://www.terraform.io/docs/backends/types/s3.html
112112
| <a name="input_kms_key_deletion_window_in_days"></a> [kms\_key\_deletion\_window\_in\_days](#input\_kms\_key\_deletion\_window\_in\_days) | Duration in days after which the key is deleted after destruction of the resource, must be between 7 and 30 days. | `number` | no |
113113
| <a name="input_kms_key_description"></a> [kms\_key\_description](#input\_kms\_key\_description) | The description of the key as viewed in AWS console. | `string` | no |
114114
| <a name="input_kms_key_enable_key_rotation"></a> [kms\_key\_enable\_key\_rotation](#input\_kms\_key\_enable\_key\_rotation) | Specifies whether key rotation is enabled. | `bool` | no |
115-
| <a name="input_noncurrent_version_expiration"></a> [noncurrent\_version\_expiration](#input\_noncurrent\_version\_expiration) | Specifies when noncurrent object versions expire. See the aws\_s3\_bucket document for detail. | <pre>object({<br> days = number<br> })</pre> | no |
116-
| <a name="input_noncurrent_version_transitions"></a> [noncurrent\_version\_transitions](#input\_noncurrent\_version\_transitions) | Specifies when noncurrent object versions transitions. See the aws\_s3\_bucket document for detail. | <pre>list(object({<br> days = number<br> storage_class = string<br> }))</pre> | no |
115+
| <a name="input_noncurrent_version_expiration"></a> [noncurrent\_version\_expiration](#input\_noncurrent\_version\_expiration) | Specifies when noncurrent object versions expire. See the aws\_s3\_bucket document for detail. | <pre>object({<br/> days = number<br/> })</pre> | no |
116+
| <a name="input_noncurrent_version_transitions"></a> [noncurrent\_version\_transitions](#input\_noncurrent\_version\_transitions) | Specifies when noncurrent object versions transitions. See the aws\_s3\_bucket document for detail. | <pre>list(object({<br/> days = number<br/> storage_class = string<br/> }))</pre> | no |
117117
| <a name="input_override_iam_policy_name"></a> [override\_iam\_policy\_name](#input\_override\_iam\_policy\_name) | override iam policy name to disable policy\_prefix and create policy with static name | `bool` | no |
118118
| <a name="input_override_iam_role_name"></a> [override\_iam\_role\_name](#input\_override\_iam\_role\_name) | override iam role name to disable role\_prefix and create role with static name | `bool` | no |
119119
| <a name="input_override_s3_bucket_name"></a> [override\_s3\_bucket\_name](#input\_override\_s3\_bucket\_name) | override s3 bucket name to disable bucket\_prefix and create bucket with static name | `bool` | no |
@@ -141,4 +141,4 @@ See [the official document](https://www.terraform.io/docs/backends/types/s3.html
141141
| <a name="output_replica_bucket"></a> [replica\_bucket](#output\_replica\_bucket) | The S3 bucket to replicate the state S3 bucket. |
142142
| <a name="output_state_bucket"></a> [state\_bucket](#output\_state\_bucket) | The S3 bucket to store the remote state file. |
143143
| <a name="output_terraform_iam_policy"></a> [terraform\_iam\_policy](#output\_terraform\_iam\_policy) | The IAM Policy to access remote state environment. |
144-
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
144+
<!-- END_TF_DOCS -->

policy.tf

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,56 @@
77
# https://github.com/nozaq/terraform-aws-remote-state-s3-backend/issues/74
88
#---------------------------------------------------------------------------------------------------
99

10+
data "aws_iam_policy_document" "terraform" {
11+
count = var.terraform_iam_policy_create ? 1 : 0
12+
13+
statement {
14+
actions = [
15+
"s3:ListBucket",
16+
"s3:GetBucketVersioning"
17+
]
18+
resources = [aws_s3_bucket.state.arn]
19+
}
20+
21+
statement {
22+
actions = [
23+
"s3:GetObject",
24+
"s3:PutObject"
25+
]
26+
resources = ["${aws_s3_bucket.state.arn}/*"]
27+
}
28+
29+
statement {
30+
actions = [
31+
"dynamodb:GetItem",
32+
"dynamodb:PutItem",
33+
"dynamodb:DeleteItem",
34+
"dynamodb:DescribeTable"
35+
]
36+
resources = [aws_dynamodb_table.lock[0].arn]
37+
}
38+
39+
statement {
40+
actions = ["kms:ListKeys"]
41+
resources = ["*"]
42+
}
43+
44+
statement {
45+
actions = [
46+
"kms:Encrypt",
47+
"kms:Decrypt",
48+
"kms:DescribeKey",
49+
"kms:GenerateDataKey"
50+
]
51+
resources = [aws_kms_key.this.arn]
52+
}
53+
}
54+
1055
resource "aws_iam_policy" "terraform" {
1156
count = var.terraform_iam_policy_create ? 1 : 0
1257

1358
name_prefix = var.override_terraform_iam_policy_name ? null : var.terraform_iam_policy_name_prefix
1459
name = var.override_terraform_iam_policy_name ? var.terraform_iam_policy_name : null
15-
policy = <<POLICY
16-
{
17-
"Version": "2012-10-17",
18-
"Statement": [
19-
{
20-
"Effect": "Allow",
21-
"Action": ["s3:ListBucket", "s3:GetBucketVersioning"],
22-
"Resource": "${aws_s3_bucket.state.arn}"
23-
},
24-
{
25-
"Effect": "Allow",
26-
"Action": ["s3:GetObject", "s3:PutObject"],
27-
"Resource": "${aws_s3_bucket.state.arn}/*"
28-
},
29-
{
30-
"Effect": "Allow",
31-
"Action": [
32-
"dynamodb:GetItem",
33-
"dynamodb:PutItem",
34-
"dynamodb:DeleteItem",
35-
"dynamodb:DescribeTable"
36-
],
37-
"Resource": "${aws_dynamodb_table.lock.arn}"
38-
},
39-
{
40-
"Effect": "Allow",
41-
"Action": [
42-
"kms:ListKeys"
43-
],
44-
"Resource": "*"
45-
},
46-
{
47-
"Effect": "Allow",
48-
"Action": [
49-
"kms:Encrypt",
50-
"kms:Decrypt",
51-
"kms:DescribeKey",
52-
"kms:GenerateDataKey"
53-
],
54-
"Resource": "${aws_kms_key.this.arn}"
55-
}
56-
]
57-
}
58-
POLICY
59-
60-
tags = var.tags
60+
policy = data.aws_iam_policy_document.terraform[0].json
61+
tags = var.tags
6162
}

0 commit comments

Comments
 (0)