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

fix(aws-autoscaling): notificationTargetArn should be optional in LifecycleHook #16187

Merged
merged 42 commits into from
Dec 11, 2021

Conversation

comcalvi
Copy link
Contributor

@comcalvi comcalvi commented Aug 23, 2021

This makes the notificationTargetArn optional in LifecycleHook. CloudFormation docs specify it as optional here. Closes #14641.
To achieve this, the role parameter was made optional. To avoid breaking users, a role is provided if users specify a notificationTarget (which they currently all do, as it is a required property) and is not provided otherwise.


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

@gitpod-io
Copy link

gitpod-io bot commented Aug 23, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 23, 2021
@comcalvi comcalvi requested a review from rix0rrr August 23, 2021 19:45
@comcalvi comcalvi added the pr/do-not-merge This PR should not be merged at this time. label Aug 23, 2021
@comcalvi comcalvi removed the pr/do-not-merge This PR should not be merged at this time. label Aug 26, 2021
packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts Outdated Show resolved Hide resolved
@@ -46,13 +46,15 @@ export interface BasicLifecycleHookProps {

/**
* The target of the lifecycle hook
*
* @default: No target.
Copy link
Contributor

Choose a reason for hiding this comment

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

Default is written is @default - No target. This comment applies multiple times.

I guess add a line explaining why this is useful...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it useful to not have a target? What does that do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The CloudFormation docs specify both role and the target as optional parameters, so to be consistent with CloudFormation it should be optional. Beyond that though, unless a user wants notifications there's no reason to specify a target or a role, because those two parameters exist only to allow for notifications.

But when they use CDK, users have to specify a notificationTarget, even if they don't need to set up notifications.

packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts Outdated Show resolved Hide resolved
});

const targetProps = props.notificationTarget.bind(this, this);
//let targetProps = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Leftover

packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts Outdated Show resolved Hide resolved
@comcalvi comcalvi requested a review from rix0rrr September 10, 2021 20:28
*/
readonly role: iam.IRole;
role: iam.IRole;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this should become read/write.

I know the API doesn't let you do what you need to do, which sucks. We'll have to find a way around that, but this is not the right way. We're going to either (a) have to break the api or (b) do type hackery.

Let's do (a), this once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean removing this logic

private readonly _role?: iam.IRole;

public get role(): iam.IRole {
  if (!this._role) { throw new Error('Oh no we don\'t have a role'); }
  return this._role;
}

and going back to having public readonly role?: iam.IRole; like we had previously?

Copy link
Contributor

@rix0rrr rix0rrr Oct 28, 2021

Choose a reason for hiding this comment

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

Let me clarify here, so we don't spread the discussion around different places.

It looks like the reason your made this writable is so that the hooks can reassign the value in the bind() method, in the knowledge that this gets used later on. This is a contract, of sorts, but I don't like it very much because it's not obvious that's what's happening or that's what you need to do.

Any time we get by by mutating some variable somewhere in a particular order, I get extremely frowny because those kinds of ordering constraints are non-obvious, and cannot be compiler-checked (and are therefore easy to accidentally break).

Instead, by changing some types (and yes, eating the breakage that comes with that) we can also achieve the same declaratively. I outlined a better solution here: #16187 (comment)

masterKey: this.encryptionKey,
});
try { lifecycleHook.role; } catch (noRoleError) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ew :/

How about we change the signature of bind() to:

public bind(scope: Construct, options: BindHookTargetOptions): BindHookTargetResult;

Then we will have:

interface BindHookTargetOptions {
  readonly lifecycleHook: autoscaling.ILifecycleHook;
  readonly role?: iam.IRole;
}

interface BindHookTargetResult {
  readonly createdRole?: iam.IRole;
}

The linter will yell at you that it's a breaking change, but you have my permission to silence it (by adding it to allowed-breaking-changes.txt).

@comcalvi comcalvi added the pr/do-not-merge This PR should not be merged at this time. label Oct 29, 2021
@comcalvi comcalvi force-pushed the makeNotificationTargetOptional branch from 00c8c39 to 8815c83 Compare October 29, 2021 18:19
@comcalvi comcalvi force-pushed the makeNotificationTargetOptional branch from 867a3f9 to 4c5f614 Compare December 9, 2021 04:59
@comcalvi comcalvi requested a review from rix0rrr December 9, 2021 15:30
@comcalvi comcalvi removed the pr/do-not-merge This PR should not be merged at this time. label Dec 9, 2021
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.

Thanks! Conditional approval based on a tiny change.

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Dec 10, 2021
@comcalvi comcalvi removed the pr/do-not-merge This PR should not be merged at this time. label Dec 10, 2021
@mergify
Copy link
Contributor

mergify bot commented Dec 11, 2021

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).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 60131cc
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify mergify bot merged commit 4e7a275 into aws:master Dec 11, 2021
@mergify
Copy link
Contributor

mergify bot commented Dec 11, 2021

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).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
…ecycleHook (aws#16187)

This makes the notificationTargetArn optional in LifecycleHook. CloudFormation docs specify it as optional [here](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-as-lifecyclehook.html). Closes aws#14641. 
To achieve this, the `role` parameter was made optional. To avoid breaking users, a role is provided if users specify a `notificationTarget` (which they currently all do, as it is a required property) and is not provided otherwise.

----

*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.

(aws-autoscaling): notificationTarget shouldn't be compulsory in LifecycleHook
3 participants