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

chore: do not use "synthesize" and "prepare" in the cdk #9410

Merged
merged 10 commits into from
Aug 4, 2020

Conversation

eladb
Copy link
Contributor

@eladb eladb commented Aug 3, 2020

In 2.x we plan to deprecate support for the synthesize() and prepare() hooks in Construct. See RFC for motivation.

This change does not remove support for these hooks, but it does remove any usage of these hooks from the AWS Construct Library.

  • aws-apigateway: the calculated logical ID of Deployment resources is now done through a Lazy instead of in prepare().
  • aws-lambda: the calculated logical ID of Version resources is now done through a Lazy instead of in prepare().
  • core: Stack.synthesize() is now called _synthesizeTemplate() and is explicitly called from app.synth().
  • core: TreeEtadata.synthesize() is now called _synthesizeTree() and is explicitly called from app.synth().

The logical IDs of Lambda Version and API Gateway Deployment resources will be different after this change due to a latent bug in the previous implementation. The prepare() methods are called before resource dependencies are resolved, which means that the hash in the logical ID did not include dependencies. Now it includes dependencies and therefore these IDs have changed. Since both of these resources are stateless, this does not introduce risk to production systems. See more details here.

Furthermore: all calls to ConstructNode.prepare() were converted to app.synth().

Related: aws/aws-cdk-rfcs#192

BREAKING CHANGE: lambda.Version and apigateway.Deployment resources with auto-generated IDs will be replaced as we fixed a bug which ignored resource dependencies when generating these logical IDs.


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

In 2.x we plan to deprecate support for the `synthesize()` and `prepare()` hooks in `Construct`. See [RFC] for motivation.

This change does not remove support for these hooks, but it does remove any usage of these hooks from the AWS Construct Library.

Namely:

- aws-apigateway: the calculated logical ID of Deployment resources is now done through a Lazy instead of in `prepare()`.
- aws-lambda: the calculated logical ID of Version resources is now done through a Lazy instead of in `prepare()`.
- core: `Stack.synthesize()` is now called `_synthesizeTemplate()` and is explicitly called from `app.synth()`.
- core: `TreeEtadata.synthesize()` is now called `_synthesizeTree()` and is explicitly called from `app.synth()`.

Furthermore: all calls to `ConstructNode.prepare()` were converted to `app.synth()`.

[RFC]: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0192-remove-constructs-compat.md
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 3, 2020
@eladb eladb changed the title feat(core): deprecate decentralized "synthesize" and "prepare" chore: do not utilize "synthesize" and "prepare" in the aws construct library Aug 3, 2020
@eladb eladb requested review from a team and RomainMuller August 3, 2020 12:42
@eladb eladb changed the title chore: do not utilize "synthesize" and "prepare" in the aws construct library chore: do not use "synthesize" and "prepare" in the aws construct library Aug 3, 2020
Copy link
Contributor

@iliapolo iliapolo left a comment

Choose a reason for hiding this comment

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

@eladb How come all the ID's changed?

@eladb
Copy link
Contributor Author

eladb commented Aug 4, 2020

@eladb How come all the ID's changed?

Good question. It's actually latent bug that we had in the previous version - dependencies were not included in the hash.

It seems like previously (when "prepare" was used), the config that used to calculate the hashes for Lambda version and APIGW deployment did not include dependencies because the construct's prepare() methods were called before dependencies were resolved. Now, that we use Lazy, the hash is calculated after dependencies & references are resolved, so we actually calculate it on a more complete function configuration.

Here's the CFN snippet that used to calculate the hash before this change:

{
  "Resources": {
    "Fn9270CBC0": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": "foo"
        },
        "Handler": "index.handler",
        "Role": {
          "Fn::GetAtt": [
            "FnServiceRoleB9001A96",
            "Arn"
          ]
        },
        "Runtime": "nodejs10.x"
      }
    }
  }
}

And after this change:

{
  "Resources": {
    "Fn9270CBC0": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": "foo"
        },
        "Handler": "index.handler",
        "Role": {
          "Fn::GetAtt": [
            "FnServiceRoleB9001A96",
            "Arn"
          ]
        },
        "Runtime": "nodejs10.x"
      },
      "DependsOn": [
        "FnServiceRoleB9001A96" // <--- this was not included
      ]
    }
  }
}

@eladb eladb requested a review from iliapolo August 4, 2020 12:03
@eladb eladb changed the title chore: do not use "synthesize" and "prepare" in the aws construct library chore: do not use "synthesize" and "prepare" in the cdk Aug 4, 2020
@iliapolo
Copy link
Contributor

iliapolo commented Aug 4, 2020

Got it. This might warrant a note in the changelog though no? It will cause a lot of resource replacements.

};

if (construct instanceof Stack) {
construct._synthesizeTemplate(session);
Copy link
Contributor

Choose a reason for hiding this comment

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

So, theoretically, if a user implemented synthesize on the stack level, we won't be calling it anymore - right? That sounds like a breaking change, am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are still calling synthesize through the onSynthesize() shim.

Copy link
Contributor

@iliapolo iliapolo Aug 4, 2020

Choose a reason for hiding this comment

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

Not sure I follow - where? I see we are calling onSynthesize on constructs that aren't a stack, but what about those who are?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm. okay let me double check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, nice catch!

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: de6c0fc
  • 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 Aug 4, 2020

Thank you for contributing! Your pull request will be updated from master 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 e3ae645 into master Aug 4, 2020
@mergify mergify bot deleted the benisrae/synth-prepare-deprecation branch August 4, 2020 15:02
eladb pushed a commit that referenced this pull request Aug 4, 2020
Follow up on #9410 and remove an additional usage of `prepare` in the API Gateway library. In this case we leveraged `prepare()` to record dependencies between all Deployment resources and all Method resources in the APIGW.

The solution is to perform two-sided bookkeeping while methods/deployments are defined and record their dependencies in-band.

We also refactored the way the code in `LatestDeployment` to be slightly more readable.
mergify bot pushed a commit that referenced this pull request Aug 5, 2020
Follow up on #9410 and remove a few additional usages of `prepare`:

- In the API Gateway library we leveraged `prepare()` to record dependencies between all Deployment resources and all Method resources in the APIGW. The solution is to perform two-sided bookkeeping while methods/deployments are defined and record their dependencies in-band. We also refactored the way the code in `LatestDeployment` to be slightly more readable.
- In the Pipelines library prepare was replaced with an aspect (which is technically the drop-in alternative to `prepare()` in v2.0, for lack of a better solution at the moment).
- In the IAM library, the `Policy` resource needs to be conditionally created only if the document contains statements. To address that, we added a new protected API to `CfnResource` which is called `shouldSynthesize()`. By default it returns `true` but if it returns `false` (in a subclass), the resource will not be rendered into the cloudformation template.

Related: aws/aws-cdk-rfcs#192

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
eladb pushed a commit that referenced this pull request Aug 10, 2020
In 2.x we plan to deprecate support for the `synthesize()` and `prepare()` hooks in `Construct`. See [RFC] for motivation.

This change does not remove support for these hooks, but it does remove any usage of these hooks from the AWS Construct Library.

- aws-apigateway: the calculated logical ID of Deployment resources is now done through a Lazy instead of in `prepare()`.
- aws-lambda: the calculated logical ID of Version resources is now done through a Lazy instead of in `prepare()`.
- core: `Stack.synthesize()` is now called `_synthesizeTemplate()` and is explicitly called from `app.synth()`.
- core: `TreeEtadata.synthesize()` is now called `_synthesizeTree()` and is explicitly called from `app.synth()`.

The logical IDs of Lambda Version and API Gateway Deployment resources will be different after this change due to a latent bug in the previous implementation. The `prepare()` methods are called _before_ resource dependencies are resolved, which means that the hash in the logical ID did not include dependencies. Now it includes dependencies and therefore these IDs have changed. Since both of these resources are stateless, this does not introduce risk to production systems. See more details [here].


Furthermore: all calls to `ConstructNode.prepare()` were converted to `app.synth()`.

Related: aws/aws-cdk-rfcs#192

[RFC]: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0192-remove-constructs-compat.md
[here]: #9410 (comment)

BREAKING CHANGE: `lambda.Version` and `apigateway.Deployment` resources with auto-generated IDs will be replaced as we fixed a bug which ignored resource dependencies when generating these logical IDs.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
eladb pushed a commit that referenced this pull request Aug 10, 2020
Follow up on #9410 and remove a few additional usages of `prepare`:

- In the API Gateway library we leveraged `prepare()` to record dependencies between all Deployment resources and all Method resources in the APIGW. The solution is to perform two-sided bookkeeping while methods/deployments are defined and record their dependencies in-band. We also refactored the way the code in `LatestDeployment` to be slightly more readable.
- In the Pipelines library prepare was replaced with an aspect (which is technically the drop-in alternative to `prepare()` in v2.0, for lack of a better solution at the moment).
- In the IAM library, the `Policy` resource needs to be conditionally created only if the document contains statements. To address that, we added a new protected API to `CfnResource` which is called `shouldSynthesize()`. By default it returns `true` but if it returns `false` (in a subclass), the resource will not be rendered into the cloudformation template.

Related: aws/aws-cdk-rfcs#192

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
curtiseppel pushed a commit to curtiseppel/aws-cdk that referenced this pull request Aug 11, 2020
In 2.x we plan to deprecate support for the `synthesize()` and `prepare()` hooks in `Construct`. See [RFC] for motivation.

This change does not remove support for these hooks, but it does remove any usage of these hooks from the AWS Construct Library.

- aws-apigateway: the calculated logical ID of Deployment resources is now done through a Lazy instead of in `prepare()`.
- aws-lambda: the calculated logical ID of Version resources is now done through a Lazy instead of in `prepare()`.
- core: `Stack.synthesize()` is now called `_synthesizeTemplate()` and is explicitly called from `app.synth()`.
- core: `TreeEtadata.synthesize()` is now called `_synthesizeTree()` and is explicitly called from `app.synth()`.

The logical IDs of Lambda Version and API Gateway Deployment resources will be different after this change due to a latent bug in the previous implementation. The `prepare()` methods are called _before_ resource dependencies are resolved, which means that the hash in the logical ID did not include dependencies. Now it includes dependencies and therefore these IDs have changed. Since both of these resources are stateless, this does not introduce risk to production systems. See more details [here].


Furthermore: all calls to `ConstructNode.prepare()` were converted to `app.synth()`.

Related: aws/aws-cdk-rfcs#192

[RFC]: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0192-remove-constructs-compat.md
[here]: aws#9410 (comment)

BREAKING CHANGE: `lambda.Version` and `apigateway.Deployment` resources with auto-generated IDs will be replaced as we fixed a bug which ignored resource dependencies when generating these logical IDs.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
curtiseppel pushed a commit to curtiseppel/aws-cdk that referenced this pull request Aug 11, 2020
Follow up on aws#9410 and remove a few additional usages of `prepare`:

- In the API Gateway library we leveraged `prepare()` to record dependencies between all Deployment resources and all Method resources in the APIGW. The solution is to perform two-sided bookkeeping while methods/deployments are defined and record their dependencies in-band. We also refactored the way the code in `LatestDeployment` to be slightly more readable.
- In the Pipelines library prepare was replaced with an aspect (which is technically the drop-in alternative to `prepare()` in v2.0, for lack of a better solution at the moment).
- In the IAM library, the `Policy` resource needs to be conditionally created only if the document contains statements. To address that, we added a new protected API to `CfnResource` which is called `shouldSynthesize()`. By default it returns `true` but if it returns `false` (in a subclass), the resource will not be rendered into the cloudformation template.

Related: aws/aws-cdk-rfcs#192

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants