Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new synthesizer separates assets out per CDK application #24430

Merged
merged 137 commits into from
May 19, 2023

Conversation

kaizencc
Copy link
Contributor

@kaizencc kaizencc commented Mar 2, 2023

This PR introduces a new synthesizer inside the module app-staging-synthesizer-alpha. This new synthesizer produces staging resources alongside the CDK application and assets will be stored there. It removes the need for running cdk bootstrap before deploying a CDK app in a new account/region. Under the new synthesizer, assets between different CDK applications will be separated which means they can be cleaned up and lifecycle controlled independently.

To get started, add the following to your CDK application:

const app = new App({
  defaultStackSynthesizer: AppStagingSynthesizer.defaultResources({
    appId: 'my-app-id', // put a unique id here
  }),
});

The new format of staging resources will look something like this:

┌─────────────────────────────┐┌───────────────────────────────────────┐┌───────────────────────────────────────┐
│                             ││                                       ││                                       │
│      ┌───────────────┐      ││             ┌──────────────┐          ││             ┌──────────────┐          │
│      │Bootstrap Stack│      ││             │  CDK App 1   │          ││             │  CDK App 2   │          │
│      └───────────────┘      ││             └──────────────┘          ││             └──────────────┘          │
│                             ││┌──────────────────┐                   ││┌──────────────────┐                   │
│                             │││ ┌──────────────┐ │                   │││ ┌──────────────┐ │                   │
│                             │││ │Staging Stack │ │                   │││ │Staging Stack │ │                   │
│                             │││ └──────────────┘ │                   │││ └──────────────┘ │                   │
│                             │││                  │                   │││                  │                   │
│                             │││                  │                   │││                  │                   │
│                             │││┌────────────────┐│     ┌────────────┐│││┌────────────────┐│     ┌────────────┐│
│                             ││││  IAM Role for  ││ ┌───│  S3 Asset  │││││  IAM Role for  ││ ┌───│  S3 Asset  ││
│                             ││││File Publishing ││ │   └────────────┘││││File Publishing ││ │   └────────────┘│
│                             │││└────────────────┘│ │                 ││││  IAM Role for  ││ │                 │
│                             │││                  │ │                 ││││Image Publishing││ │                 │
│┌───────────────────────────┐│││                  │ │                 │││└────────────────┘│ │                 │
││IAM Role for CFN execution ││││                  │ │                 │││                  │ │                 │
││    IAM Role for lookup    ││││                  │ │                 │││                  │ │                 │
││  IAM Role for deployment  ││││┌────────────────┐│ │                 │││┌────────────────┐│ │                 │
│└───────────────────────────┘││││ S3 Bucket for  ││ │                 ││││ S3 Bucket for  ││ │                 │
│                             ││││ Staging Assets │◀─┘                 ││││ Staging Assets │◀─┘                 │
│                             │││└────────────────┘│                   │││└────────────────┘│      ┌───────────┐│
│                             │││                  │                   │││                  │  ┌───│ ECR Asset ││
│                             │││                  │                   │││┌────────────────┐│  │   └───────────┘│
│                             │││                  │                   ││││ ECR Repository ││  │                │
│                             │││                  │                   ││││  for Staging   │◀──┘                │
│                             │││                  │                   ││││     Assets     ││                   │
│                             │││                  │                   │││└────────────────┘│                   │
│                             │││                  │                   │││                  │                   │
│                             │││                  │                   │││                  │                   │
│                             │││                  │                   │││                  │                   │
│                             │││                  │                   │││                  │                   │
│                             │││                  │                   │││                  │                   │
│                             ││└──────────────────┘                   ││└──────────────────┘                   │
└─────────────────────────────┘└───────────────────────────────────────┘└───────────────────────────────────────┘

This feature is heavily experimental and the API may break in the future. It does not work with CDK Pipelines yet.

Depended on #25536.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 2, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/staging-stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/staging-stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/staging-stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/staging-stack.ts Outdated Show resolved Hide resolved
bucketName: this.stagingBucketName,
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO: lifecycle rules?

packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
Comment on lines 20 to 29
/**
* The app-scoped, environment-keyed bucket created in this staging stack.
*/
readonly stagingBucket?: s3.Bucket;

/**
* The app-scoped, environment-keyed repositories created in this staging stack.
* A repository is created per image asset family.
*/
readonly stagingRepos: Record<string, ecr.Repository>;
Copy link
Contributor

Choose a reason for hiding this comment

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

These don't need to be exposed on the interface, right?

packages/@aws-cdk/core-synthesizer/lib/staging-stack.ts Outdated Show resolved Hide resolved
* Staging Stack Properties
*/
export interface StagingStackProps extends StackProps {
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect an application name parameter here as well

*
* @default - a well-known name unique to this app/env.
*/
readonly fileAssetPublishingRoleName?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this one that's going to be created or reused? Maybe call it existingFileAssetPublishingRoleName ?

/**
* Default asset publishing role name for file (S3) assets.
*/
private static readonly DEFAULT_FILE_ASSET_PUBLISHING_ROLE_NAME = 'cdk-${Qualifier}-file-publishing-role-${AWS::AccountId}-${AWS::Region}';
Copy link
Contributor

Choose a reason for hiding this comment

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

This name is going to conflict with the default bootstrapped roles :).

I think we pick a prefix that contains the app name and base the rest off of it. And come to think of it, ${AccountId} only needs to be in the bucket name, not in any of the role names.

* Returns the well-known name of the file publishing role
*/
private getCreateFilePublishingRole() {
this.node.tryFindChild(this.fileAssetPublishingRoleName) as iam.Role ?? new iam.Role(this, this.fileAssetPublishingRoleName, {
Copy link
Contributor

Choose a reason for hiding this comment

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

The construct ID doesn't have to be its actual name. It could just be a symbolic identifier like "FileRole". Think of it as a variable name.

private getCreateFilePublishingRole() {
this.node.tryFindChild(this.fileAssetPublishingRoleName) as iam.Role ?? new iam.Role(this, this.fileAssetPublishingRoleName, {
roleName: DefaultStagingStack.DEFAULT_FILE_ASSET_PUBLISHING_ROLE_NAME,
assumedBy: new iam.ServicePrincipal('sts.amazonaws.com'), // TODO actually create correct role
Copy link
Contributor

Choose a reason for hiding this comment

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

Check the current assume role policies. I think the correct assumer here will be:

  • The Deploy Role if the user chooses a Deploy Role (should be a parameter to the stack synthesizer)
  • The current account root if not

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this needs to be configurable?

Comment on lines 114 to 116
partition: this.partition ?? Aws.PARTITION,
account: this.account ?? Aws.ACCOUNT_ID,
region: this.region ?? Aws.REGION,
Copy link
Contributor

Choose a reason for hiding this comment

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

You need special placeholders instead of tokens, to pass to the asset manifest. I think it's the literal string ${AWS::Region}, etc.

packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core-synthesizer/lib/synthesizer.ts Outdated Show resolved Hide resolved
@kaizencc kaizencc requested a review from rix0rrr May 16, 2023 22:28
@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label May 17, 2023
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Some final nitpicks on the README and naming, sorry :P and then let's ship it!

packages/@aws-cdk/app-staging-synthesizer-alpha/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/app-staging-synthesizer-alpha/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/app-staging-synthesizer-alpha/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/app-staging-synthesizer-alpha/README.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label May 19, 2023
@kaizencc kaizencc added pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested and removed pr/needs-cli-test-run This PR needs CLI tests run against it. pr/do-not-merge This PR should not be merged at this time. labels May 19, 2023
@aws-cdk-automation aws-cdk-automation dismissed their stale review May 19, 2023 21:08

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 93dad51
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented May 19, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit ae21ecc into main May 19, 2023
@mergify mergify bot deleted the conroy/bootstrap branch May 19, 2023 21:20
mergify bot pushed a commit that referenced this pull request May 24, 2023
Bump schema version to accompany #24430

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@RichiCoder1
Copy link
Contributor

Howdy! Probably not the best place to ask, but will there be a version of this where you can still share Buckets, but the rest is independent? At least two of our accounts are already close to bucket limit and it's not hard to hit depending on your usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. p2 pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants