Skip to content

Commit 9f25d90

Browse files
authored
feat: Add identitystore group and user data source dependency null resources (#17)
* Add identitystore group and user data source dependency null resources * Add example
1 parent 67e866c commit 9f25d90

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module "sso" {
7373
| Name | Version |
7474
|------|---------|
7575
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.40 |
76+
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
7677

7778
## Modules
7879

@@ -87,6 +88,8 @@ No modules.
8788
| [aws_ssoadmin_managed_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_managed_policy_attachment) | resource |
8889
| [aws_ssoadmin_permission_set.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_permission_set) | resource |
8990
| [aws_ssoadmin_permission_set_inline_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_permission_set_inline_policy) | resource |
91+
| [null_resource.group_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
92+
| [null_resource.user_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
9093
| [aws_identitystore_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_group) | data source |
9194
| [aws_identitystore_user.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_user) | data source |
9295
| [aws_ssoadmin_instances.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
@@ -96,6 +99,8 @@ No modules.
9699
| Name | Description | Type | Default | Required |
97100
|------|-------------|------|---------|:--------:|
98101
| <a name="input_account_assignments"></a> [account\_assignments](#input\_account\_assignments) | List of maps containing mapping between user/group, permission set and assigned accounts list. See account\_assignments description in README for more information about map values. | <pre>list(object({<br> principal_name = string,<br> principal_type = string,<br> permission_set = string,<br> account_ids = list(string)<br> }))</pre> | `[]` | no |
102+
| <a name="input_identitystore_group_data_source_depends_on"></a> [identitystore\_group\_data\_source\_depends\_on](#input\_identitystore\_group\_data\_source\_depends\_on) | List of parameters that identitystore group data sources depend on, for example new SSO group IDs. | `list(string)` | `[]` | no |
103+
| <a name="input_identitystore_user_data_source_depends_on"></a> [identitystore\_user\_data\_source\_depends\_on](#input\_identitystore\_user\_data\_source\_depends\_on) | List of parameters that identitystore user data sources depend on, for example new SSO user IDs. | `list(string)` | `[]` | no |
99104
| <a name="input_permission_sets"></a> [permission\_sets](#input\_permission\_sets) | Map of maps containing Permission Set names as keys. See permission\_sets description in README for information about map values. | `any` | <pre>{<br> "AdministratorAccess": {<br> "description": "Provides full access to AWS services and resources.",<br> "managed_policies": [<br> "arn:aws:iam::aws:policy/AdministratorAccess"<br> ],<br> "session_duration": "PT2H"<br> }<br>}</pre> | no |
100105

101106
## Outputs
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Simple
2+
Example showing how to create SSO users and groups in the same state file as `terraform-aws-sso` module resources and without adding `depends_on` argument at the module level. Using null resource, it will only recreate account assignments, when new SSO group is added. It will NOT re-create all module resources, after a new AWS Account is added.
3+
4+
## Pre-requisites
5+
Before this example can be used, please ensure that the following pre-requisites are met:
6+
- Enable AWS Organizations and add AWS Accounts you want to be managed by SSO. [Documentation](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_tutorials_basic.html)
7+
- Enable AWS SSO. [Documentation](https://docs.aws.amazon.com/singlesignon/latest/userguide/step1.html).
8+
- Ensure that Terraform is using a role with permissions required for AWS SSO management. [Documentation](https://docs.aws.amazon.com/singlesignon/latest/userguide/iam-auth-access-using-id-policies.html#requiredpermissionsconsole).
9+
10+
11+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
12+
## Requirements
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.23 |
17+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.27 |
18+
19+
## Providers
20+
21+
| Name | Version |
22+
|------|---------|
23+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.27 |
24+
25+
## Modules
26+
27+
| Name | Source | Version |
28+
|------|--------|---------|
29+
| <a name="module_aws_identitystore"></a> [aws\_identitystore](#module\_aws\_identitystore) | avlcloudtechnologies/identitystore/aws | 0.1.1 |
30+
| <a name="module_sso"></a> [sso](#module\_sso) | avlcloudtechnologies/sso/aws | |
31+
32+
## Resources
33+
34+
| Name | Type |
35+
|------|------|
36+
| [aws_organizations_organization.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/organizations_organization) | data source |
37+
38+
## Inputs
39+
40+
No inputs.
41+
42+
## Outputs
43+
44+
| Name | Description |
45+
|------|-------------|
46+
| <a name="output_aws_ssoadmin_permission_sets"></a> [aws\_ssoadmin\_permission\_sets](#output\_aws\_ssoadmin\_permission\_sets) | Maps of permission sets with attributes listed in Terraform resource aws\_ssoadmin\_permission\_set documentation. |
47+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/with-dependencies/main.tf

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
data "aws_organizations_organization" "this" {}
6+
7+
locals {
8+
all_active_accounts_map = { for account in toset(data.aws_organizations_organization.this.accounts) : account.name => account if account.status == "ACTIVE" }
9+
non_management_active_accounts_map = { for account in toset(data.aws_organizations_organization.this.non_master_accounts) : account.name => account if account.status == "ACTIVE" }
10+
sso_groups = {
11+
management = {
12+
description = "Group with Administrator access to all accounts including Management account"
13+
},
14+
admins = {
15+
description = "Group with Administrator access to all accounts excluding Management account"
16+
},
17+
readonly = {
18+
description = "Group for Read only access"
19+
}
20+
}
21+
sso_users = {
22+
aurimas = {
23+
display_name = "aurimas"
24+
given_name = "Aurimas"
25+
family_name = "Mickevicius"
26+
sso_groups = ["management", "readonly"]
27+
},
28+
john = {
29+
display_name = "john"
30+
given_name = "John"
31+
family_name = "Smith"
32+
sso_groups = ["admins", "readonly"]
33+
}
34+
}
35+
}
36+
37+
module "sso" {
38+
source = "avlcloudtechnologies/sso/aws"
39+
40+
permission_sets = {
41+
AdministratorAccess = {
42+
description = "Provides full access to AWS services and resources.",
43+
session_duration = "PT2H",
44+
managed_policies = ["arn:aws:iam::aws:policy/AdministratorAccess"]
45+
},
46+
ViewOnlyAccess = {
47+
description = "View resources and basic metadata across all AWS services.",
48+
session_duration = "PT2H",
49+
managed_policies = ["arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"]
50+
},
51+
}
52+
account_assignments = [
53+
{
54+
principal_name = "management"
55+
principal_type = "GROUP"
56+
permission_set = "AdministratorAccess"
57+
account_ids = [for account in local.all_active_accounts_map : account.id]
58+
},
59+
{
60+
principal_name = "admins"
61+
principal_type = "GROUP"
62+
permission_set = "AdministratorAccess"
63+
account_ids = [for account in local.non_management_active_accounts_map : account.id]
64+
},
65+
{
66+
principal_name = "readonly"
67+
principal_type = "GROUP"
68+
permission_set = "ViewOnlyAccess"
69+
account_ids = [for account in local.non_management_active_accounts_map : account.id]
70+
},
71+
]
72+
identitystore_group_data_source_depends_on = [for group in module.aws_identitystore.groups : group.group_id]
73+
}
74+
75+
module "aws_identitystore" {
76+
source = "avlcloudtechnologies/identitystore/aws"
77+
version = "0.1.1"
78+
79+
sso_users = var.sso_users
80+
sso_groups = var.sso_groups
81+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "aws_ssoadmin_permission_sets" {
2+
description = "Maps of permission sets with attributes listed in Terraform resource aws_ssoadmin_permission_set documentation."
3+
value = module.sso.aws_ssoadmin_permission_sets
4+
}

examples/with-dependencies/variables.tf

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 0.12.23"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 3.27"
7+
}
8+
}
9+
}

main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ locals {
3434
users = [for assignment in var.account_assignments : assignment.principal_name if assignment.principal_type == "USER"]
3535
}
3636

37+
resource "null_resource" "group_dependency" {
38+
triggers = {
39+
dependency_id = join(",", var.identitystore_group_data_source_depends_on)
40+
}
41+
}
42+
43+
resource "null_resource" "user_dependency" {
44+
triggers = {
45+
dependency_id = join(",", var.identitystore_user_data_source_depends_on)
46+
}
47+
}
48+
3749
data "aws_ssoadmin_instances" "this" {}
3850

3951
data "aws_identitystore_group" "this" {
@@ -45,6 +57,7 @@ data "aws_identitystore_group" "this" {
4557
attribute_value = each.value
4658
}
4759
}
60+
depends_on = [null_resource.group_dependency]
4861
}
4962

5063
data "aws_identitystore_user" "this" {
@@ -56,6 +69,7 @@ data "aws_identitystore_user" "this" {
5669
attribute_value = each.value
5770
}
5871
}
72+
depends_on = [null_resource.user_dependency]
5973
}
6074

6175
resource "aws_ssoadmin_permission_set" "this" {

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ variable "account_assignments" {
2020
}))
2121

2222
default = []
23+
}
24+
25+
variable "identitystore_group_data_source_depends_on" {
26+
description = "List of parameters that identitystore group data sources depend on, for example new SSO group IDs."
27+
type = list(string)
28+
default = []
29+
}
30+
31+
variable "identitystore_user_data_source_depends_on" {
32+
description = "List of parameters that identitystore user data sources depend on, for example new SSO user IDs."
33+
type = list(string)
34+
default = []
2335
}

0 commit comments

Comments
 (0)