Skip to content

Commit 6fd47e4

Browse files
authored
Merge pull request #8 from infraspecdev/sso_groups
feat: SSO groups
2 parents 6e46b12 + 7d064b3 commit 6fd47e4

File tree

15 files changed

+231
-0
lines changed

15 files changed

+231
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
aws_region = "us-east-1"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module "sso_groups" {
2+
source = "../../modules/sso_groups/"
3+
4+
sso_groups = var.sso_groups
5+
user_groups_map = var.user_groups_map
6+
}

examples/create-sso-group-and-assign-users/outputs.tf

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = local.aws_region
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "sso_groups" {
2+
description = <<EOF
3+
(Recquired)A map of objects defining AWS SSO groups to be created. Each object contains:
4+
- group_name: The name of the SSO group.
5+
- group_description: A description for the SSO group.
6+
EOF
7+
type = map(object(
8+
{
9+
group_name = string
10+
group_description = string
11+
}
12+
))
13+
}
14+
15+
variable "user_groups_map" {
16+
type = map(list(string))
17+
description = "(Optional)Mapping of users to their respective sso groups within the Organisation. For example map of `user=[sso_groups]"
18+
default = {}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.4.6"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.65.0"
8+
}
9+
}
10+
}

modules/sso_groups/.header.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Terraform AWS Organizations SSO Groups module
2+
A Terraform module for creating sso groups and attaching users to the groups within the organisation.

modules/sso_groups/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Requirements
2+
3+
| Name | Version |
4+
|------|---------|
5+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4.6 |
6+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.65.0 |
7+
8+
## Providers
9+
10+
| Name | Version |
11+
|------|---------|
12+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.58.0 |
13+
14+
## Modules
15+
16+
No modules.
17+
18+
## Resources
19+
20+
| Name | Type |
21+
|------|------|
22+
| [aws_identitystore_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/identitystore_group) | resource |
23+
| [aws_identitystore_group_membership.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/identitystore_group_membership) | resource |
24+
| [aws_identitystore_user.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_user) | data source |
25+
| [aws_ssoadmin_instances.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
26+
27+
## Inputs
28+
29+
| Name | Description | Type | Default | Required |
30+
|------|-------------|------|---------|:--------:|
31+
| <a name="input_sso_groups"></a> [sso\_groups](#input\_sso\_groups) | A map of objects defining AWS SSO groups to be created. Each object contains:<br> - group\_name: The name of the SSO group.<br> - group\_description: A description for the SSO group. | <pre>map(object(<br> {<br> group_name = string<br> group_description = string<br> }<br> ))</pre> | n/a | yes |
32+
| <a name="input_user_groups_mapping"></a> [user\_groups\_mapping](#input\_user\_groups\_mapping) | Mapping of users to their respective sso groups within the Organisation. For example map of `user=[sso_groups]` | `map(list(string))` | `{}` | no |
33+
34+
## Outputs
35+
36+
| Name | Description |
37+
|------|-------------|
38+
| <a name="output_sso_group_ids"></a> [sso\_group\_ids](#output\_sso\_group\_ids) | A map of SSO groups IDs created by this module |

modules/sso_groups/data.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fetch existing SSO Instance
2+
data "aws_ssoadmin_instances" "default" {}
3+
4+
# - Fetch of SSO Users to be used for group membership assignment -
5+
data "aws_identitystore_user" "default" {
6+
for_each = local.users_and_their_groups
7+
8+
identity_store_id = local.sso_instance_id
9+
10+
alternate_identifier {
11+
# Filter users by user_name (nuzumaki, suchiha, dovis, etc.)
12+
unique_attribute {
13+
attribute_path = "UserName"
14+
attribute_value = each.value.user_name
15+
}
16+
}
17+
}

modules/sso_groups/locals.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# - Users and Groups
2+
locals {
3+
# Create a new local variable by flattening the complex type given in the variable "sso_users"
4+
flatten_user_data = flatten([
5+
for this_user in keys(var.user_groups_map) : [
6+
for group in var.user_groups_map[this_user] : {
7+
user_name = this_user
8+
group_name = group
9+
}
10+
]
11+
])
12+
13+
users_and_their_groups = {
14+
for s in local.flatten_user_data : format("%s_%s", s.user_name, s.group_name) => s
15+
}
16+
17+
# - Fetch SSO Instance ARN and SSO Instance ID -
18+
sso_instance_id = tolist(data.aws_ssoadmin_instances.default.identity_store_ids)[0]
19+
}

0 commit comments

Comments
 (0)