Skip to content

Commit c0df3d6

Browse files
authored
Custom IAM roles (#125)
* Allows creating custom IAM roles within the account
1 parent 19eeaf4 commit c0df3d6

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ for dxw's Dalmatian hosting platform.
4545
| [aws_glue_catalog_table.cloudtrail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/glue_catalog_table) | resource |
4646
| [aws_iam_policy.cloudtrail_cloudwatch_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
4747
| [aws_iam_policy.cloudwatch_slack_alerts_logs_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
48+
| [aws_iam_policy.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
4849
| [aws_iam_policy.delete_default_resources_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
4950
| [aws_iam_policy.delete_default_resources_vpc_delete_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
5051
| [aws_iam_policy.ssm_dhmc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
5152
| [aws_iam_role.cloudtrail_cloudwatch_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
5253
| [aws_iam_role.cloudwatch_slack_alerts_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
54+
| [aws_iam_role.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
5355
| [aws_iam_role.delete_default_resources_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
5456
| [aws_iam_role.ssm_dhmc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
5557
| [aws_iam_role_policy_attachment.cloudtrail_cloudwatch_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
5658
| [aws_iam_role_policy_attachment.cloudwatch_slack_alerts_logs_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
59+
| [aws_iam_role_policy_attachment.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
5760
| [aws_iam_role_policy_attachment.delete_default_resources_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
5861
| [aws_iam_role_policy_attachment.delete_default_resources_vpc_delete_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
5962
| [aws_iam_role_policy_attachment.ssm_dhmc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -126,6 +129,7 @@ for dxw's Dalmatian hosting platform.
126129
| <a name="input_cloudwatch_slack_alerts_kms_encryption"></a> [cloudwatch\_slack\_alerts\_kms\_encryption](#input\_cloudwatch\_slack\_alerts\_kms\_encryption) | Use KMS encryption with the Slack Alerts SNS topic and logs | `bool` | n/a | yes |
127130
| <a name="input_cloudwatch_slack_alerts_log_retention"></a> [cloudwatch\_slack\_alerts\_log\_retention](#input\_cloudwatch\_slack\_alerts\_log\_retention) | Cloudwatch Slack Alerts log retention. Set to 0 to keep all logs | `number` | n/a | yes |
128131
| <a name="input_codestar_connections"></a> [codestar\_connections](#input\_codestar\_connections) | CodeStar connections to create | <pre>map(<br/> object({<br/> provider_type = string,<br/> })<br/> )</pre> | n/a | yes |
132+
| <a name="input_custom_iam_roles"></a> [custom\_iam\_roles](#input\_custom\_iam\_roles) | Configure custom IAM roles/policies | <pre>map(object({<br/> description = string<br/> policies = map(object({<br/> description = string<br/> Version = string<br/> Statement = list(object({<br/> Action = list(string)<br/> Effect = string<br/> Resource = string<br/> }))<br/> }))<br/> assume_role_policy = object({<br/> Version = string<br/> Statement = list(object({<br/> Action = list(string)<br/> Effect = string<br/> Principal = map(string)<br/> }))<br/> })<br/> }))</pre> | n/a | yes |
129133
| <a name="input_delete_default_resources_lambda_kms_encryption"></a> [delete\_default\_resources\_lambda\_kms\_encryption](#input\_delete\_default\_resources\_lambda\_kms\_encryption) | Conditionally encrypt the Delete Default Resources Lambda logs with KMS | `bool` | n/a | yes |
130134
| <a name="input_delete_default_resources_log_retention"></a> [delete\_default\_resources\_log\_retention](#input\_delete\_default\_resources\_log\_retention) | Log retention for the Delete Default Resources Lambda | `number` | n/a | yes |
131135
| <a name="input_enable_cloudtrail"></a> [enable\_cloudtrail](#input\_enable\_cloudtrail) | Enable Cloudtrail | `bool` | n/a | yes |

iam-custom-roles.tf

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
resource "aws_iam_role" "custom" {
2+
for_each = local.custom_iam_roles
3+
4+
name = each.key
5+
description = each.value["description"]
6+
assume_role_policy = jsonencode(each.value["assume_role_policy"])
7+
}
8+
9+
resource "aws_iam_policy" "custom" {
10+
for_each = merge(flatten([
11+
for role_name, role in local.custom_iam_roles : {
12+
for policy_name, policy in role.policies :
13+
"${role_name}_${policy_name}" => {
14+
role_name = role_name
15+
policy_name = policy_name
16+
policy = policy
17+
}
18+
}
19+
])...)
20+
21+
name = each.value["policy_name"]
22+
description = each.value["policy"]["description"]
23+
policy = jsonencode({
24+
Version = each.value["policy"]["Version"],
25+
Statement = each.value["policy"]["Statement"]
26+
})
27+
}
28+
29+
resource "aws_iam_role_policy_attachment" "custom" {
30+
for_each = aws_iam_policy.custom
31+
32+
role = aws_iam_role.custom[split("_", each.key)[0]].name
33+
policy_arn = each.value.arn
34+
}

locals.tf

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ locals {
6666

6767
enable_ssm_dhmc = var.enable_ssm_dhmc
6868

69+
custom_iam_roles = var.custom_iam_roles
70+
6971
enable_logs_bucket = local.cloudtrail_s3_access_logs || local.cloudtrail_athena_glue_tables
7072
logging_bucket_retention = var.logging_bucket_retention
7173
logs_bucket_source_arns = concat(

variables.tf

+24
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,27 @@ variable "logging_bucket_retention" {
166166
description = "Logging bucket retention in days. Set to 0 to keep all logs."
167167
type = number
168168
}
169+
170+
variable "custom_iam_roles" {
171+
type = map(object({
172+
description = string
173+
policies = map(object({
174+
description = string
175+
Version = string
176+
Statement = list(object({
177+
Action = list(string)
178+
Effect = string
179+
Resource = string
180+
}))
181+
}))
182+
assume_role_policy = object({
183+
Version = string
184+
Statement = list(object({
185+
Action = list(string)
186+
Effect = string
187+
Principal = map(string)
188+
}))
189+
})
190+
}))
191+
description = "Configure custom IAM roles/policies"
192+
}

0 commit comments

Comments
 (0)