Skip to content

Commit c2343ee

Browse files
authored
feat: Add ability for controlling whether or not to create a policy (#163)
1 parent 37d5168 commit c2343ee

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

examples/iam-policy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Run `terraform destroy` when you don't need these resources.
3434
|------|--------|---------|
3535
| <a name="module_iam_policy"></a> [iam\_policy](#module\_iam\_policy) | ../../modules/iam-policy | |
3636
| <a name="module_iam_policy_from_data_source"></a> [iam\_policy\_from\_data\_source](#module\_iam\_policy\_from\_data\_source) | ../../modules/iam-policy | |
37+
| <a name="module_iam_policy_optional"></a> [iam\_policy\_optional](#module\_iam\_policy\_optional) | ../../modules/iam-policy | |
3738

3839
## Resources
3940

examples/iam-policy/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ module "iam_policy_from_data_source" {
5353
PolicyDescription = "Policy created using example from data source"
5454
}
5555
}
56+
57+
module "iam_policy_optional" {
58+
source = "../../modules/iam-policy"
59+
60+
create_policy = false
61+
}

modules/iam-policy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ No modules.
3030

3131
| Name | Description | Type | Default | Required |
3232
|------|-------------|------|---------|:--------:|
33+
| <a name="input_create_policy"></a> [create\_policy](#input\_create\_policy) | Whether to create the IAM policy | `bool` | `true` | no |
3334
| <a name="input_description"></a> [description](#input\_description) | The description of the policy | `string` | `"IAM Policy"` | no |
3435
| <a name="input_name"></a> [name](#input\_name) | The name of the policy | `string` | `""` | no |
3536
| <a name="input_path"></a> [path](#input\_path) | The path of the policy in IAM | `string` | `"/"` | no |

modules/iam-policy/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
resource "aws_iam_policy" "policy" {
2+
count = var.create_policy ? 1 : 0
3+
24
name = var.name
35
path = var.path
46
description = var.description
@@ -7,4 +9,3 @@ resource "aws_iam_policy" "policy" {
79

810
tags = var.tags
911
}
10-

modules/iam-policy/outputs.tf

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
output "id" {
22
description = "The policy's ID"
3-
value = aws_iam_policy.policy.id
3+
value = element(concat(aws_iam_policy.policy.*.id, [""]), 0)
44
}
55

66
output "arn" {
77
description = "The ARN assigned by AWS to this policy"
8-
value = aws_iam_policy.policy.arn
8+
value = element(concat(aws_iam_policy.policy.*.arn, [""]), 0)
99
}
1010

1111
output "description" {
1212
description = "The description of the policy"
13-
value = aws_iam_policy.policy.description
13+
value = element(concat(aws_iam_policy.policy.*.description, [""]), 0)
1414
}
1515

1616
output "name" {
1717
description = "The name of the policy"
18-
value = aws_iam_policy.policy.name
18+
value = element(concat(aws_iam_policy.policy.*.name, [""]), 0)
1919
}
2020

2121
output "path" {
2222
description = "The path of the policy in IAM"
23-
value = aws_iam_policy.policy.path
23+
value = element(concat(aws_iam_policy.policy.*.path, [""]), 0)
2424
}
2525

2626
output "policy" {
2727
description = "The policy document"
28-
value = aws_iam_policy.policy.policy
28+
value = element(concat(aws_iam_policy.policy.*.policy, [""]), 0)
2929
}
30-

modules/iam-policy/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "create_policy" {
2+
description = "Whether to create the IAM policy"
3+
type = bool
4+
default = true
5+
}
6+
17
variable "name" {
28
description = "The name of the policy"
39
type = string

0 commit comments

Comments
 (0)