Skip to content

Commit 5a5f2cc

Browse files
committed
Add account_permission_assignment sub module
1 parent 6e46b12 commit 5a5f2cc

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "null_resource" "sso_group_dependency" {
2+
triggers = {
3+
dependency_id = join(",", var.identitystore_group_depends_on)
4+
}
5+
}
6+
7+
data "aws_identitystore_group" "this" {
8+
for_each = local.group_list
9+
identity_store_id = local.identity_store_id
10+
11+
alternate_identifier {
12+
unique_attribute {
13+
attribute_path = "DisplayName"
14+
attribute_value = each.key
15+
}
16+
}
17+
18+
depends_on = [null_resource.sso_group_dependency]
19+
}
20+
21+
data "aws_ssoadmin_instances" "this" {
22+
23+
}
24+
25+
resource "null_resource" "sso_permission_set_dependency" {
26+
triggers = {
27+
dependency_id = join(",", var.identitystore_permission_set_depends_on)
28+
}
29+
}
30+
31+
data "aws_ssoadmin_permission_set" "this" {
32+
for_each = local.permission_set_list
33+
34+
instance_arn = local.sso_instance_arn
35+
name = each.value
36+
depends_on = [null_resource.sso_permission_set_dependency]
37+
}
38+
39+
40+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
locals {
3+
assignment_map = {
4+
for a in var.account_assignments :
5+
format("%v-%v-%v-%v", a.account_id, substr(a.principal_type, 0, 1), a.principal_name, a.permission_set_name) => a
6+
}
7+
8+
identity_store_id = tolist(data.aws_ssoadmin_instances.this.identity_store_ids)[0]
9+
sso_instance_arn = tolist(data.aws_ssoadmin_instances.this.arns)[0]
10+
11+
group_list = toset([for mapping in var.account_assignments : mapping.principal_name])
12+
permission_set_list = toset([for mapping in var.account_assignments : mapping.permission_set_name])
13+
14+
}
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
locals {
2+
target_type = "AWS_ACCOUNT"
3+
}
4+
resource "aws_ssoadmin_account_assignment" "this" {
5+
for_each = local.assignment_map
6+
7+
instance_arn = local.sso_instance_arn
8+
permission_set_arn = data.aws_ssoadmin_permission_set.this[each.value.permission_set_name].arn
9+
principal_id = data.aws_identitystore_group.this[each.value.principal_name].group_id
10+
principal_type = each.value.principal_type
11+
target_id = each.value.account_id
12+
target_type = local.target_type
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "assignments" {
2+
value = aws_ssoadmin_account_assignment.this
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
provider "aws" {
3+
region = "us-east-1"
4+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
variable "account_assignments" {
2+
description = <<EOF
3+
A list of objects representing permission assignments for AWS accounts. Each object contains the following attributes:
4+
- account_id: The AWS account ID where the permissions will be applied.
5+
- permission_set_name: The name of the permission set to be used.
6+
- permission_set_arn: The Amazon Resource Name (ARN) of the permission set.
7+
- principal_name: An identifier for an object in AWS SSO, such as the name of an SSO group.
8+
- principal_type: The type of entity for which the assignment will be created. Should be set to 'GROUP' only. Defaults to 'GROUP'."
9+
EOF
10+
type = list(object({
11+
account_id = string
12+
permission_set_name = string
13+
principal_name = string
14+
principal_type = optional(string, "GROUP")
15+
}))
16+
default = [{
17+
account_id = "471112575944"
18+
permission_set_name = "Core_Dev"
19+
principal_name = "Read_Only"
20+
}]
21+
22+
validation {
23+
condition = alltrue([for a in var.account_assignments : (a.principal_type == "GROUP")])
24+
error_message = "Principal type should be 'GROUP' only"
25+
}
26+
}
27+
28+
variable "identitystore_group_depends_on" {
29+
description = "A list of parameters to use for data resources to depend on. This is to avoid module depends_on as that will unnecessarily create the module resources"
30+
type = list(string)
31+
default = []
32+
}
33+
34+
variable "identitystore_permission_set_depends_on" {
35+
description = "A list of parameters to use for data resources to depend on. This is to avoid module depends_on as that will unnecessarily create the module resources"
36+
type = list(string)
37+
default = []
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
null = {
10+
source = "hashicorp/null"
11+
version = "~> 3.2.2"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)