Skip to content

Commit

Permalink
feat(security): create organizations module (#27)
Browse files Browse the repository at this point in the history
* feat(security): create organizations module

* chore: remove redundant comment
  • Loading branch information
p5 authored Sep 1, 2024
1 parent cd2dcd3 commit 35fb508
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
57 changes: 57 additions & 0 deletions modules/aws/security/organizations/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Prepare locals so we have a shortcut to the exported properties of the organization
locals {
root_id = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].roots.0.id
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].roots.0.id
: null
)
)
organization_arn = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].arn
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].arn
: null
)
)
organization_id = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].id
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].id
: null
)
)
master_account_arn = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].master_account_arn
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].master_account_arn
: null
)
)
master_account_id = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].master_account_id
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].master_account_id
: null
)
)
master_account_email = (
length(aws_organizations_organization.root) > 0
? aws_organizations_organization.root[0].master_account_email
: (
length(data.aws_organizations_organization.root) > 0
? data.aws_organizations_organization.root[0].master_account_email
: null
)
)
}
31 changes: 31 additions & 0 deletions modules/aws/security/organizations/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Existing AWS Organization
data "aws_organizations_organization" "root" {
count = var.create_organization ? 0 : 1
}

# New AWS Organization
resource "aws_organizations_organization" "root" {
count = var.create_organization ? 1 : 0

aws_service_access_principals = var.organizations_aws_service_access_principals
enabled_policy_types = var.organizations_enabled_policy_types
feature_set = var.organizations_feature_set
}

resource "aws_organizations_account" "child_accounts" {
for_each = var.child_accounts

name = each.key
email = each.value["email"]

close_on_deletion = lookup(each.value, "close_on_deletion", false)
iam_user_access_to_billing = lookup(each.value, "iam_user_access_to_billing", var.default_iam_user_access_to_billing)
role_name = lookup(each.value, "role_name", var.default_role_name)
tags = merge(var.tags_all, lookup(each.value, "tags", {}))

parent_id = lookup(each.value, "parent_id", local.root_id)

lifecycle {
ignore_changes = [role_name]
}
}
34 changes: 34 additions & 0 deletions modules/aws/security/organizations/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
output "organization_arn" {
description = "ARN of the organization."
value = local.organization_arn
}

output "organization_id" {
description = "Identifier of the organization."
value = local.organization_id
}

output "organization_root_id" {
description = "Identifier of the root of this organization."
value = local.root_id
}

output "master_account_arn" {
description = "ARN of the master account."
value = local.master_account_arn
}

output "master_account_id" {
description = "Identifier of the master account."
value = local.master_account_id
}

output "master_account_email" {
description = "Email address of the master account."
value = local.master_account_email
}

output "child_accounts" {
description = "A map of all accounts created by this module (NOT including the root account). The keys are the names of the accounts and the values are the attributes for the account as defined in the aws_organizations_account resource."
value = aws_organizations_account.child_accounts
}
54 changes: 54 additions & 0 deletions modules/aws/security/organizations/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
variable "create_organization" {
description = "Whether to create an AWS organization"
type = bool
default = true
}

variable "organizations_aws_service_access_principals" {
description = "The service principals for which to enable access to the organization"
type = list(string)
default = []
}

variable "organizations_enabled_policy_types" {
description = "The policy types to enable in the organization"
type = list(string)
default = []
}

variable "organizations_feature_set" {
description = "The feature set to enable in the organization"
type = string
default = "ALL"
}

variable "child_accounts" {
description = "A map of child accounts to create"
type = map(object({
email = string
close_on_deletion = optional(bool, false)
iam_user_access_to_billing = optional(bool, true)
parent_id = optional(string, null)
role_name = optional(string, null)
tags = optional(map(string), {})
}))
default = {}
}

variable "default_iam_user_access_to_billing" {
description = "Whether to allow IAM users to access billing information for the account"
type = bool
default = true
}

variable "default_role_name" {
description = "The name of the role to create for the account"
type = string
default = "OrganizationAccountAccessRole"
}

variable "tags_all" {
description = "A map of tags to assign to the account"
type = map(string)
default = {}
}
12 changes: 12 additions & 0 deletions modules/aws/security/organizations/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is generated. Do not edit! Your changes will be lost.

terraform {
required_version = ">=1.3"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">=4.0"
}
}
}

0 comments on commit 35fb508

Please sign in to comment.