Skip to content

Commit

Permalink
feat(workflows): Add bucket name param to cleanup workflow
Browse files Browse the repository at this point in the history
Previously each synthesis of a blueprint using a cleanup workflow would
generate a different bucket name, causing conflicts when performing merges
during resynthesis. Add an optional parameter to the cleanup workflow to
set the bucket name. This way, a blueprint can store the random bucket name
as an option so that resynthesis can generate a workflow with the same bucket
name.
  • Loading branch information
aggagen committed Oct 11, 2023
1 parent bf03d24 commit 5e7b69a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion packages/blueprints/sam-serverless-app/src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
AutoDiscoverReportDefinition,
WorkflowBuilder,
emptyWorkflow,
DEFAULT_DELETE_RESOURCE_WORKFLOW_NAME,
} from '@amazon-codecatalyst/blueprint-component.workflows';
import { DEFAULT_DELETE_RESOURCE_WORKFLOW_NAME } from '@amazon-codecatalyst/blueprint-component.workflows/lib/actions/action-cfn-cleanup';
import { Blueprint as ParentBlueprint, Options as ParentOptions, MergeStrategies } from '@amazon-codecatalyst/blueprints.blueprint';
import { SampleDir, SampleFile } from 'projen';
import { getFilePermissions, writeFile } from 'projen/lib/util';
Expand Down Expand Up @@ -113,6 +113,17 @@ export interface Options extends ParentOptions {
* @hidden true
*/
uncommentCleanupWorkflow?: boolean;

/**
* The name of the temporary S3 bucket used in the cleanup workflow. This option is hidden and will be set by the wizard
* to a default bucket prefix followed by wizard generated entropy. This option allows subsequent resynthesis to
* generate the cleanup workflow using the same random bucket name as was generated by the original synthesis.
* @validationRegex /^[-.a-zA-Z0-9]{3,63}$/
* @validationMessage Must contain only alphanumeric characters, periods (.), dashes (-) and be between 3 and 63 characters in length.
* @defaultEntropy 32
* @hidden true
*/
cleanupWorkflowTemplateBucketName?: string;
}

/**
Expand Down Expand Up @@ -212,6 +223,7 @@ export class Blueprint extends ParentBlueprint {
},
stackName: this.options.code.cloudFormationStackName,
region: 'us-west-2',
templateBucketName: this.options.cleanupWorkflowTemplateBucketName,
});
const additionalComments = [
'The following workflow is intentionally disabled by the blueprint author to prevent project contributors from accidentally executing it.',
Expand Down
3 changes: 2 additions & 1 deletion packages/blueprints/sam-serverless-app/src/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
},
"lambda": {
"functionName": "SamFirstEndpoint"
}
},
"cleanupWorkflowTemplateBucketName": "tmp-cleanup-workflow-"
}
12 changes: 9 additions & 3 deletions packages/components/workflows/src/actions/action-cfn-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const generateUniqueS3BucketName = () => {
return bucketName;
};

export const cfnCleanupSteps = (stackName: string, region: string) => {
export const cfnCleanupSteps = (stackName: string, region: string, bucketName: string) => {
return [
`stack_name=${stackName}`,
`region=${region}`,
`cfn_template_upload_bucket=${generateUniqueS3BucketName()} # we need an S3 bucket to temporarily host the updated cloudformation template because template-body has a max length of 51,200 bytes which may not be enough in some cases`,
`cfn_template_upload_bucket=${bucketName} # we need an S3 bucket to temporarily host the updated cloudformation template because template-body has a max length of 51,200 bytes which may not be enough in some cases`,
'echo \'Update existing cloudformation template to change resources deletion policy to "Delete", and set deletion policy for S3 buckets and Elastic Container Registry to "Retain" which will be manually cleaned up later.\'',
'pip install cfn-flip',
'aws cloudformation get-template --stack-name $stack_name --region $region > existing-template-$stack_name.json',
Expand Down Expand Up @@ -64,6 +64,11 @@ export const cfnCleanupSteps = (stackName: string, region: string) => {
export interface CfnCleanupActionParameters extends Pick<BuildActionParameters, 'actionName' | 'environment' | 'dependsOn'> {
stackName: string;
region: string;
/**
* The S3 bucket name to use when creating a temporary bucket for the updated CloudFormation template.
* @default - a randomly generated bucket name
*/
templateBucketName?: string;
}

export const addGenericCloudFormationCleanupAction = (
Expand All @@ -74,11 +79,12 @@ export const addGenericCloudFormationCleanupAction = (
): string => {
const { blueprint, workflow, stackName, region, environment, dependsOn } = params;
const actionName = (params.actionName || 'CleanupCloudFormationStack').replace(new RegExp('-', 'g'), '_');
const bucketName = params.templateBucketName ?? generateUniqueS3BucketName();

const buildAction: ActionDefiniton = {
Identifier: getDefaultActionIdentifier(ActionIdentifierAlias.build, blueprint.context.environmentId),
Configuration: {
Steps: cfnCleanupSteps(stackName, region).map(step => {
Steps: cfnCleanupSteps(stackName, region, bucketName).map(step => {
return {
Run: step,
};
Expand Down
1 change: 1 addition & 0 deletions packages/components/workflows/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './environment/workflow-environment';

export * from './actions/action';
export * from './actions/action-build';
export * from './actions/action-cfn-cleanup';
export * from './actions/action-cfn-deploy';
export * from './actions/action-test-reports';
export * from './actions/action-cdk-deploy';
Expand Down

0 comments on commit 5e7b69a

Please sign in to comment.