Skip to content

Commit f67dc65

Browse files
authored
update ec2 ebs encrypt for ct optional (#174)
* update ec2 ebs encrypt for ct optional * fix isort error
1 parent 220c90d commit f67dc65

File tree

7 files changed

+63
-27
lines changed

7 files changed

+63
-27
lines changed

aws_sra_examples/solutions/ec2/ec2_default_ebs_encryption/lambda/src/app.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from mypy_boto3_organizations.type_defs import AccountTypeDef, DescribeAccountResponseTypeDef, TagTypeDef
3232
from mypy_boto3_sns import SNSClient
3333
from mypy_boto3_sns.type_defs import PublishBatchResponseTypeDef, PublishResponseTypeDef
34+
from mypy_boto3_ssm.client import SSMClient
3435
from mypy_boto3_sts import STSClient
3536

3637
# Setup Default Logger
@@ -55,6 +56,7 @@
5556
CFN_CLIENT: CloudFormationClient = MANAGEMENT_ACCOUNT_SESSION.client("cloudformation", config=BOTO3_CONFIG)
5657
ORG_CLIENT: OrganizationsClient = MANAGEMENT_ACCOUNT_SESSION.client("organizations", config=BOTO3_CONFIG)
5758
SNS_CLIENT: SNSClient = MANAGEMENT_ACCOUNT_SESSION.client("sns", config=BOTO3_CONFIG)
59+
SSM_CLIENT: SSMClient = MANAGEMENT_ACCOUNT_SESSION.client("ssm")
5860
except Exception as error:
5961
LOGGER.error({"Unexpected_Error": error})
6062
raise ValueError("Unexpected error executing Lambda function. Review CloudWatch logs for details.") from None
@@ -92,30 +94,14 @@ def assume_role(role: str, role_session_name: str, account: str = None, session:
9294

9395

9496
def get_control_tower_regions() -> list: # noqa: CCR001
95-
"""Query 'AWSControlTowerBP-BASELINE-CLOUDWATCH' CloudFormation stack to identify customer regions.
97+
"""Query ssm to identify customer regions.
9698
9799
Returns:
98100
Customer regions chosen in Control Tower
99101
"""
100-
paginator = CFN_CLIENT.get_paginator("list_stack_instances")
101-
customer_regions = set()
102-
aws_account = ""
103-
all_regions_identified = False
104-
for page in paginator.paginate(StackSetName="AWSControlTowerBP-BASELINE-CLOUDWATCH", PaginationConfig={"PageSize": CLOUDFORMATION_PAGE_SIZE}):
105-
for instance in page["Summaries"]:
106-
if not aws_account:
107-
aws_account = instance["Account"]
108-
customer_regions.add(instance["Region"])
109-
continue
110-
if aws_account == instance["Account"]:
111-
customer_regions.add(instance["Region"])
112-
continue
113-
all_regions_identified = True
114-
break
115-
if all_regions_identified:
116-
break
117-
sleep(CLOUDFORMATION_THROTTLE_PERIOD)
118-
102+
customer_regions = []
103+
ssm_response = SSM_CLIENT.get_parameter(Name="/sra/regions/customer-control-tower-regions")
104+
customer_regions = ssm_response["Parameter"]["Value"].split(",")
119105
return list(customer_regions)
120106

121107

@@ -332,7 +318,6 @@ def process_accounts(event: Union[CloudFormationCustomResourceEvent, dict], para
332318
sns_messages = []
333319
accounts = get_active_organization_accounts()
334320
for account in accounts:
335-
336321
if is_account_with_exclude_tags(account, params):
337322
continue
338323

aws_sra_examples/solutions/ec2/ec2_default_ebs_encryption/templates/sra-ec2-default-ebs-encryption-global-events.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Resources:
5353
source:
5454
- aws.organizations
5555
detail-type:
56-
- AWS API Call via CloudTrail
56+
- AWS Service Event via CloudTrail
5757
detail:
5858
eventSource:
5959
- organizations.amazonaws.com

aws_sra_examples/solutions/ec2/ec2_default_ebs_encryption/templates/sra-ec2-default-ebs-encryption-main-ssm.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Metadata:
2323
- pRootOrganizationalUnitId
2424
- pSRAAlarmEmail
2525

26+
- Label:
27+
default: IAM Properties
28+
Parameters:
29+
- pStackSetAdminRole
30+
- pStackExecutionRole
31+
2632
- Label:
2733
default: EC2 Default EBS Encryption Properties
2834
Parameters:
@@ -45,6 +51,10 @@ Metadata:
4551
- pComplianceFrequency
4652

4753
ParameterLabels:
54+
pStackSetAdminRole:
55+
default: Stack Set Role
56+
pStackExecutionRole:
57+
default: Stack execution role
4858
pComplianceFrequency:
4959
default: Frequency to Check for Organizational Compliance
5060
pControlTowerRegionsOnly:
@@ -75,6 +85,16 @@ Metadata:
7585
default: SRA Staging S3 Bucket Name
7686

7787
Parameters:
88+
pStackSetAdminRole:
89+
AllowedValues: [sra-stackset]
90+
Default: sra-stackset
91+
Description: The administration role name that is used in the stackset.
92+
Type: String
93+
pStackExecutionRole:
94+
AllowedValues: [sra-execution]
95+
Default: sra-execution
96+
Description: The execution role name that is used in the stack.
97+
Type: String
7898
pComplianceFrequency:
7999
ConstraintDescription: Compliance Frequency must be a number between 1 and 30, inclusive.
80100
Default: 7
@@ -256,13 +276,13 @@ Resources:
256276
DependsOn: rEC2DefaultEBSEncryptionStack
257277
Properties:
258278
StackSetName: sra-ec2-default-ebs-encryption-global-events
259-
AdministrationRoleARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AWSControlTowerStackSetRole
279+
AdministrationRoleARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${pStackSetAdminRole}
260280
CallAs: SELF
261281
Capabilities:
262282
- CAPABILITY_NAMED_IAM
263283
Description:
264284
!Sub ${pSRASolutionVersion} - Deploys EventBridge Rules via ${pSRASolutionName} for capturing global events forwarding to the home region.
265-
ExecutionRoleName: AWSControlTowerExecution
285+
ExecutionRoleName: !Ref pStackExecutionRole
266286
ManagedExecution:
267287
Active: true
268288
OperationPreferences:

aws_sra_examples/solutions/ec2/ec2_default_ebs_encryption/templates/sra-ec2-default-ebs-encryption-main.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Metadata:
2323
- pRootOrganizationalUnitId
2424
- pSRAAlarmEmail
2525

26+
- Label:
27+
default: IAM Properties
28+
Parameters:
29+
- pStackSetAdminRole
30+
- pStackExecutionRole
31+
2632
- Label:
2733
default: EC2 Default EBS Encryption Properties
2834
Parameters:
@@ -45,6 +51,10 @@ Metadata:
4551
- pComplianceFrequency
4652

4753
ParameterLabels:
54+
pStackSetAdminRole:
55+
default: Stack Set Role
56+
pStackExecutionRole:
57+
default: Stack execution role
4858
pComplianceFrequency:
4959
default: Frequency to Check for Organizational Compliance
5060
pControlTowerRegionsOnly:
@@ -75,6 +85,16 @@ Metadata:
7585
default: SRA Staging S3 Bucket Name
7686

7787
Parameters:
88+
pStackSetAdminRole:
89+
AllowedValues: [sra-stackset]
90+
Default: sra-stackset
91+
Description: The administration role name that is used in the stackset.
92+
Type: String
93+
pStackExecutionRole:
94+
AllowedValues: [sra-execution]
95+
Default: sra-execution
96+
Description: The execution role name that is used in the stack.
97+
Type: String
7898
pComplianceFrequency:
7999
ConstraintDescription: Compliance Frequency must be a number between 1 and 30, inclusive.
80100
Default: 7
@@ -251,13 +271,13 @@ Resources:
251271
DependsOn: rEC2DefaultEBSEncryptionStack
252272
Properties:
253273
StackSetName: sra-ec2-default-ebs-encryption-global-events
254-
AdministrationRoleARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AWSControlTowerStackSetRole
274+
AdministrationRoleARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${pStackSetAdminRole}
255275
CallAs: SELF
256276
Capabilities:
257277
- CAPABILITY_NAMED_IAM
258278
Description:
259279
!Sub ${pSRASolutionVersion} - Deploys EventBridge Rules via ${pSRASolutionName} for capturing global events forwarding to the home region.
260-
ExecutionRoleName: AWSControlTowerExecution
280+
ExecutionRoleName: !Ref pStackExecutionRole
261281
ManagedExecution:
262282
Active: true
263283
OperationPreferences:

aws_sra_examples/solutions/ec2/ec2_default_ebs_encryption/templates/sra-ec2-default-ebs-encryption.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ Resources:
326326
Action: cloudformation:ListStackInstances
327327
Resource: !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stackset/AWSControlTowerBP-*
328328

329+
- PolicyName: "ssm-access"
330+
PolicyDocument:
331+
Version: "2012-10-17"
332+
Statement:
333+
- Effect: "Allow"
334+
Action:
335+
- ssm:GetParameter
336+
- ssm:GetParameters
337+
Resource:
338+
- !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/sra*"
339+
329340
- PolicyName: sra-ec2-default-ebs-encryption-policy-iam
330341
PolicyDocument:
331342
Version: 2012-10-17
@@ -483,7 +494,7 @@ Resources:
483494
source:
484495
- aws.organizations
485496
detail-type:
486-
- AWS API Call via CloudTrail
497+
- AWS Service Event via CloudTrail
487498
detail:
488499
eventSource:
489500
- organizations.amazonaws.com

0 commit comments

Comments
 (0)