Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/lambda_codebase/account_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
REGION_DEFAULT = os.environ["AWS_REGION"]
PARTITION = get_partition(REGION_DEFAULT)
LOGGER = configure_logger(__name__)
DEPLOY_TIME_IN_MS = 5 * 60 * 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this magic number?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeout for the Lambda function is set to 10 minutes. See https://github.com/awslabs/aws-deployment-framework/blob/24ba1e58b419796e7268d73b7691099f5a0509f2/src/template.yml

This lambda function is responsible for deploying the bootstrap stack inside different regions.
Deploying that takes about 2 minutes in ideal situations.
To make sure we don't fail to deploy because of a Lambda timeout midway, I put the limit to 5 minutes. So it will only continue to deploy if it has enough time to complete the execution. Otherwise it throws an error which will trigger it to run another time. The next run, it skips updating the regions it deployed to already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay doke! Thanks



def configure_generic_account(sts, event, region, role):
Expand Down Expand Up @@ -107,7 +108,7 @@ def is_inter_ou_account_move(event):
)


def lambda_handler(event, _):
def lambda_handler(event, context):
sts = STS()

account_id = event["account_id"]
Expand Down Expand Up @@ -136,9 +137,28 @@ def lambda_handler(event, _):
+ event["regions"]
)
)
LOGGER.debug(
"Looping through regions to deploy the base stack in %s, regions: %s",
event["account_id"],
regions,
)
for region in regions:
if context.get_remaining_time_in_millis() < DEPLOY_TIME_IN_MS:
LOGGER.info(
"Cannot deploy another region, as the time available for this "
"lambda execution is less than the time required to deploy."
)
raise GenericAccountConfigureError(
'Execution time remaining is not sufficient to deploy '
'another region, aborting this execution so it can restart.'
)
if not event["is_deployment_account"]:
configure_generic_account(sts, event, region, role)
LOGGER.info(
"Creating/updating base stack in %s %s",
event["account_id"],
region,
)
cloudformation = CloudFormation(
region=region,
deployment_account_region=event["deployment_account_region"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1228,101 +1228,135 @@ Resources:
StateMachineName: "EnableCrossAccountAccess"
DefinitionString: !Sub |-
{
"Comment": "Enable Cross Account Access from Deployment Account",
"StartAt": "DetermineEvent",
"States": {
"DetermineEvent": {
"Type": "Choice",
"Choices": [{
"Variable": "$.update_only",
"NumericEquals": 1,
"Next": "UpdateDeploymentPipelines"
},
{
"Not": {
"Variable": "$.error",
"NumericEquals": 0
},
"Next": "NotifyFailure"
}
],
"Default": "EnableCrossAccountAccessMap"
},
"EnableCrossAccountAccessMap": {
"Type": "Map",
"Next": "UpdateDeploymentPipelines",
"Iterator": {
"StartAt": "EnableCrossAccountAccess",
"States": {
"EnableCrossAccountAccess": {
"Type": "Task",
"Resource": "${EnableCrossAccountAccess.Arn}",
"TimeoutSeconds": 900,
"End": true,
"Retry": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"BackoffRate": 1.1,
"IntervalSeconds": 1,
"MaxAttempts": 10
}
]
}
}
},
"ItemsPath": "$.account_ids",
"Parameters": {
"deployment_account_region.$": "$.deployment_account_region",
"deployment_account_id.$": "$.deployment_account_id",
"account_id.$": "$$.Map.Item.Value",
"regions.$": "$.regions"
},
"ResultPath": null
},
"UpdateDeploymentPipelines": {
"Type": "Task",
"Resource": "${CheckPipelineStatus.Arn}",
"TimeoutSeconds": 60,
"Next": "NeedToNotifySuccess?"
},
"NeedToNotifySuccess?": {
"Type": "Choice",
"Choices": [{
"Variable": "$.update_only",
"NumericEquals": 1,
"Next": "Success"
}],
"Default": "NotifySuccess"
},
"Success": {
"Type": "Succeed"
},
"Failure": {
"Type": "Fail"
},
"NotifySuccess": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${PipelineSNSTopic.TopicName}",
"Message.$": "$.message",
"Subject": "Success - AWS Deployment Framework Bootstrap"
},
"Next": "Success"
},
"NotifyFailure": {
"Comment": "Enable Cross Account Access from Deployment Account",
"StartAt": "DetermineEvent",
"States": {
"DetermineEvent": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.update_only",
"NumericEquals": 1,
"Next": "UpdateDeploymentPipelines"
}, {
"Not": {
"Variable": "$.error",
"NumericEquals": 0
},
"Next": "NotifyFailure"
}
],
"Default": "EnableCrossAccountAccessMap"
},
"EnableCrossAccountAccessMap": {
"Type": "Map",
"Next": "UpdateDeploymentPipelines",
"Iterator": {
"StartAt": "EnableCrossAccountAccess",
"States": {
"EnableCrossAccountAccess": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${PipelineSNSTopic.TopicName}",
"Message.$": "$.error",
"Subject": "Failure - AWS Deployment Framework Bootstrap"
},
"Next": "Failure"
"Resource": "${EnableCrossAccountAccess.Arn}",
"TimeoutSeconds": 900,
"End": true,
"Retry": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"BackoffRate": 1.1,
"IntervalSeconds": 1,
"MaxAttempts": 10
}, {
"ErrorEquals": [
"Lambda.Unknown",
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException"
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 2,
"MaxAttempts": 6,
"BackoffRate": 2
}
]
}
}
},
"ItemsPath": "$.account_ids",
"Parameters": {
"deployment_account_region.$": "$.deployment_account_region",
"deployment_account_id.$": "$.deployment_account_id",
"account_id.$": "$$.Map.Item.Value",
"regions.$": "$.regions"
},
"ResultPath": null
},
"UpdateDeploymentPipelines": {
"Type": "Task",
"Resource": "${CheckPipelineStatus.Arn}",
"TimeoutSeconds": 60,
"Retry": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"BackoffRate": 1.1,
"IntervalSeconds": 1,
"MaxAttempts": 10
}, {
"ErrorEquals": [
"Lambda.Unknown",
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException"
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 2,
"MaxAttempts": 6,
"BackoffRate": 2
}
],
"Next": "NeedToNotifySuccess?"
},
"NeedToNotifySuccess?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.update_only",
"NumericEquals": 1,
"Next": "Success"
}
],
"Default": "NotifySuccess"
},
"Success": {
"Type": "Succeed"
},
"Failure": {
"Type": "Fail"
},
"NotifySuccess": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${PipelineSNSTopic.TopicName}",
"Message.$": "$.message",
"Subject": "Success - AWS Deployment Framework Bootstrap"
},
"Next": "Success"
},
"NotifyFailure": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${PipelineSNSTopic.TopicName}",
"Message.$": "$.error",
"Subject": "Failure - AWS Deployment Framework Bootstrap"
},
"Next": "Failure"
}
}
}
RoleArn: !GetAtt StatesExecutionRole.Arn

Expand Down
Loading