Skip to content

Commit 14c1069

Browse files
authored
Fix CloudFormation StackSet creation when deployed to the management account (#91)
1 parent e584933 commit 14c1069

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

main.tf

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
locals {
22
# Internal
3-
account_id = data.aws_caller_identity.current.account_id
4-
organization_id = data.aws_organizations_organization.org.id
5-
partition_id = data.aws_partition.current.partition
6-
region = data.aws_region.current.region
7-
deployment_regions = [local.region]
3+
account_id = data.aws_caller_identity.current.account_id
4+
organization_id = data.aws_organizations_organization.org.id
5+
organization_management_account_id = data.aws_organizations_organization.org.master_account_id
6+
partition_id = data.aws_partition.current.partition
7+
region = data.aws_region.current.region
8+
deployment_regions = [local.region]
89

910
# Member account deployment role names are templated here but used throughout this module and submodules.
1011
member_account_deployment_helper_role_name_template = "${var.member_account_resource_name_prefix}<SERVICE>-deployment-helper-<REGION>"
@@ -40,10 +41,11 @@ module "deployment" {
4041
retained_vaults = each.value.retained_vaults
4142

4243
current = {
43-
account_id = local.account_id
44-
organization_id = local.organization_id
45-
partition = local.partition_id
46-
region = local.region
44+
account_id = local.account_id
45+
organization_id = local.organization_id
46+
organization_management_account_id = local.organization_management_account_id
47+
partition = local.partition_id
48+
region = local.region
4749
}
4850
central_account_resource_name_prefix = var.central_account_resource_name_prefix
4951
central_backup_service_linked_role_arn = local.backup_service_linked_role_arn

modules/service-deployment/cloudformation.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ locals {
55
[for i in var.admin_role_names : { "Fn::Sub" : "arn:$${AWS::Partition}:iam::$${AWS::AccountId}:role/${i}" }],
66
{ "Ref" : "CentralBackupServiceRoleArn" }
77
])
8+
cfn_call_as = var.current.organization_management_account_id == var.current.account_id ? "SELF" : "DELEGATED_ADMIN"
89
}
910

1011
resource "aws_cloudformation_stack_set" "member_account_deployments" {
1112
name = local.member_account_resource_name_prefix
1213
description = "Centralised AWS Backup for ${var.service_name}."
1314
capabilities = ["CAPABILITY_NAMED_IAM"]
1415
permission_model = "SERVICE_MANAGED"
15-
call_as = "DELEGATED_ADMIN"
16+
call_as = local.cfn_call_as
1617

1718
# Try to do as much as possible in native CloudFormation, but some things, like dynamic lists, are only possible in Terraform.
1819
# jsonencode(jsondecode(...)) used to minify the file.
@@ -60,7 +61,7 @@ resource "aws_cloudformation_stack_set" "member_account_deployments" {
6061

6162
resource "aws_cloudformation_stack_instances" "member_account_deployments" {
6263
stack_set_name = aws_cloudformation_stack_set.member_account_deployments.name
63-
call_as = "DELEGATED_ADMIN"
64+
call_as = local.cfn_call_as
6465
regions = var.deployment_regions
6566
deployment_targets {
6667
organizational_unit_ids = var.deployment_targets

modules/service-deployment/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ variable "current" {
4949
type = object({
5050
account_id : string
5151
organization_id : string
52+
organization_management_account_id : string
5253
partition : string
5354
region : string
5455
})

0 commit comments

Comments
 (0)