Skip to content

Commit 9fa481f

Browse files
feat: Added direct policy attachment in iam-user module (#387)
Co-authored-by: Anton Babenko <[email protected]>
1 parent f18b333 commit 9fa481f

File tree

8 files changed

+53
-2
lines changed

8 files changed

+53
-2
lines changed

examples/iam-user/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ Run `terraform destroy` when you don't need these resources.
2525

2626
## Providers
2727

28-
No providers.
28+
| Name | Version |
29+
|------|---------|
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0 |
2931

3032
## Modules
3133

@@ -34,10 +36,13 @@ No providers.
3436
| <a name="module_iam_user"></a> [iam\_user](#module\_iam\_user) | ../../modules/iam-user | n/a |
3537
| <a name="module_iam_user2"></a> [iam\_user2](#module\_iam\_user2) | ../../modules/iam-user | n/a |
3638
| <a name="module_iam_user3"></a> [iam\_user3](#module\_iam\_user3) | ../../modules/iam-user | n/a |
39+
| <a name="module_iam_user4"></a> [iam\_user4](#module\_iam\_user4) | ../../modules/iam-user | n/a |
3740

3841
## Resources
3942

40-
No resources.
43+
| Name | Type |
44+
|------|------|
45+
| [aws_iam_policy.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy) | data source |
4146

4247
## Inputs
4348

@@ -64,4 +69,5 @@ No inputs.
6469
| <a name="output_keybase_secret_key_decrypt_command"></a> [keybase\_secret\_key\_decrypt\_command](#output\_keybase\_secret\_key\_decrypt\_command) | Decrypt access secret key command |
6570
| <a name="output_keybase_secret_key_pgp_message"></a> [keybase\_secret\_key\_pgp\_message](#output\_keybase\_secret\_key\_pgp\_message) | Encrypted access secret key |
6671
| <a name="output_pgp_key"></a> [pgp\_key](#output\_pgp\_key) | PGP key used to encrypt sensitive data for this user (if empty - secrets are not encrypted) |
72+
| <a name="output_policy_arns"></a> [policy\_arns](#output\_policy\_arns) | The list of ARNs of policies directly assigned to the IAM user |
6773
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/iam-user/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ module "iam_user3" {
4646
create_iam_access_key = true
4747
iam_access_key_status = "Inactive"
4848
}
49+
50+
###################################################################
51+
# IAM user with IAM policy attached
52+
###################################################################
53+
54+
data "aws_iam_policy" "example" {
55+
name = "AmazonS3ReadOnlyAccess"
56+
}
57+
58+
module "iam_user4" {
59+
source = "../../modules/iam-user"
60+
61+
name = "vasya.pupkin6"
62+
63+
create_iam_user_login_profile = false
64+
create_iam_access_key = true
65+
policy_arns = [data.aws_iam_policy.example.arn]
66+
}

examples/iam-user/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,8 @@ output "keybase_secret_key_pgp_message" {
8585
description = "Encrypted access secret key"
8686
value = module.iam_user.keybase_secret_key_pgp_message
8787
}
88+
89+
output "policy_arns" {
90+
description = "The list of ARNs of policies directly assigned to the IAM user"
91+
value = module.iam_user.policy_arns
92+
}

modules/iam-user/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ No modules.
4646
| [aws_iam_access_key.this_no_pgp](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_access_key) | resource |
4747
| [aws_iam_user.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
4848
| [aws_iam_user_login_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_login_profile) | resource |
49+
| [aws_iam_user_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy_attachment) | resource |
4950
| [aws_iam_user_ssh_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_ssh_key) | resource |
5051

5152
## Inputs
@@ -63,6 +64,7 @@ No modules.
6364
| <a name="input_path"></a> [path](#input\_path) | Desired path for the IAM user | `string` | `"/"` | no |
6465
| <a name="input_permissions_boundary"></a> [permissions\_boundary](#input\_permissions\_boundary) | The ARN of the policy that is used to set the permissions boundary for the user. | `string` | `""` | no |
6566
| <a name="input_pgp_key"></a> [pgp\_key](#input\_pgp\_key) | Either a base-64 encoded PGP public key, or a keybase username in the form `keybase:username`. Used to encrypt password and access key. | `string` | `""` | no |
67+
| <a name="input_policy_arns"></a> [policy\_arns](#input\_policy\_arns) | The list of ARNs of policies directly assigned to the IAM user | `list(string)` | `[]` | no |
6668
| <a name="input_ssh_key_encoding"></a> [ssh\_key\_encoding](#input\_ssh\_key\_encoding) | Specifies the public key encoding format to use in the response. To retrieve the public key in ssh-rsa format, use SSH. To retrieve the public key in PEM format, use PEM | `string` | `"SSH"` | no |
6769
| <a name="input_ssh_public_key"></a> [ssh\_public\_key](#input\_ssh\_public\_key) | The SSH public key. The public key must be encoded in ssh-rsa format or PEM format | `string` | `""` | no |
6870
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources. | `map(string)` | `{}` | no |
@@ -94,4 +96,5 @@ No modules.
9496
| <a name="output_keybase_ses_smtp_password_v4_decrypt_command"></a> [keybase\_ses\_smtp\_password\_v4\_decrypt\_command](#output\_keybase\_ses\_smtp\_password\_v4\_decrypt\_command) | Decrypt SES SMTP password command |
9597
| <a name="output_keybase_ses_smtp_password_v4_pgp_message"></a> [keybase\_ses\_smtp\_password\_v4\_pgp\_message](#output\_keybase\_ses\_smtp\_password\_v4\_pgp\_message) | Encrypted SES SMTP password |
9698
| <a name="output_pgp_key"></a> [pgp\_key](#output\_pgp\_key) | PGP key used to encrypt sensitive data for this user (if empty - secrets are not encrypted) |
99+
| <a name="output_policy_arns"></a> [policy\_arns](#output\_policy\_arns) | The list of ARNs of policies directly assigned to the IAM user |
97100
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/iam-user/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ resource "aws_iam_user_ssh_key" "this" {
4545
encoding = var.ssh_key_encoding
4646
public_key = var.ssh_public_key
4747
}
48+
49+
resource "aws_iam_user_policy_attachment" "this" {
50+
for_each = var.create_user ? toset(var.policy_arns) : []
51+
52+
user = aws_iam_user.this[0].name
53+
policy_arn = each.value
54+
}

modules/iam-user/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ output "iam_user_ssh_key_fingerprint" {
149149
description = "The MD5 message digest of the SSH public key"
150150
value = try(aws_iam_user_ssh_key.this[0].fingerprint, "")
151151
}
152+
153+
output "policy_arns" {
154+
description = "The list of ARNs of policies directly assigned to the IAM user"
155+
value = [for policy_attachment in aws_iam_user_policy_attachment.this : policy_attachment.policy_arn]
156+
}

modules/iam-user/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ variable "permissions_boundary" {
8181
default = ""
8282
}
8383

84+
variable "policy_arns" {
85+
description = "The list of ARNs of policies directly assigned to the IAM user"
86+
type = list(string)
87+
default = []
88+
}
89+
8490
variable "tags" {
8591
description = "A map of tags to add to all resources."
8692
type = map(string)

wrappers/iam-user/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ module "wrapper" {
1717
ssh_key_encoding = try(each.value.ssh_key_encoding, var.defaults.ssh_key_encoding, "SSH")
1818
ssh_public_key = try(each.value.ssh_public_key, var.defaults.ssh_public_key, "")
1919
permissions_boundary = try(each.value.permissions_boundary, var.defaults.permissions_boundary, "")
20+
policy_arns = try(each.value.policy_arns, var.defaults.policy_arns, [])
2021
tags = try(each.value.tags, var.defaults.tags, {})
2122
}

0 commit comments

Comments
 (0)