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(redshift-alpha): use same role for database-query singleton function #32363

Merged
merged 13 commits into from
Jan 3, 2025

Conversation

5d
Copy link
Contributor

@5d 5d commented Dec 2, 2024

Issue # (if applicable)

Closes #32089.

Reason for this change

The Redshift tables use a singleton function as the invoker for various custom resource onEvent Lambda functions. Currently, each custom resource lambda function has a dedicated IAM role to assume. However, since it’s the same singleton function, a shared role could achieve the same outcome.

Description of changes

Use the same IAM role for the singleton invoker function to assume.

Description of how you validated changes

deployed to my local stack

Checklist


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

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Dec 2, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Dec 2, 2024
Copy link

codecov bot commented Dec 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.96%. Comparing base (de04742) to head (751a303).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #32363   +/-   ##
=======================================
  Coverage   66.96%   66.96%           
=======================================
  Files         329      329           
  Lines       18663    18663           
  Branches     3258     3258           
=======================================
  Hits        12497    12497           
  Misses       5839     5839           
  Partials      327      327           
Flag Coverage Δ
suite.unit 66.96% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 80.71% <ø> (ø)
packages/aws-cdk-lib/core 82.08% <ø> (ø)

@aws-cdk-automation aws-cdk-automation requested a review from a team December 2, 2024 22:22
@5d 5d force-pushed the 5d/redshift-iam-role branch from c1402b3 to e5a6c7b Compare December 9, 2024 23:50
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.

@5d 5d force-pushed the 5d/redshift-iam-role branch 2 times, most recently from e9a9b6b to ea8b8f0 Compare December 12, 2024 00:44
@5d 5d changed the base branch from main to 5d/fix-31817 December 12, 2024 00:44
@5d 5d force-pushed the 5d/redshift-iam-role branch from ea8b8f0 to cc05f22 Compare December 12, 2024 01:49
@5d 5d marked this pull request as ready for review December 12, 2024 01:54
@aws-cdk-automation aws-cdk-automation dismissed their stale review December 12, 2024 17:39

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

Base automatically changed from 5d/fix-31817 to main December 13, 2024 19:14
@5d 5d force-pushed the 5d/redshift-iam-role branch from a63c2a2 to 4331a07 Compare December 13, 2024 19:45
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 13, 2024
@godwingrs22 godwingrs22 self-requested a review January 3, 2025 00:16
Copy link
Member

@godwingrs22 godwingrs22 left a comment

Choose a reason for hiding this comment

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

Thanks @5d for fixing this. Left some nit comments.

/**
* The name of the singleton function. It acts as a unique ID within its CDK stack.
* */
public readonly constructName: string;
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think we can use the existing property itself using functionName to get the name of the function. Don't think we need a new property here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my understanding, the functionName of a singleton Lambda differs from the constructName used to identify the function within the stack. Additionally, the functionName is often a token and is typically only resolved at deployment time.

* We only need one function since it's just acting as a trigger.
* */
private getProviderRole(handler: lambda.SingletonFunction): iam.IRole {
const id = handler.constructName + 'ProviderRole';
Copy link
Member

Choose a reason for hiding this comment

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

nit: we could get the function name like below

const id = handler.functionName + 'ProviderRole';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

* Get or create the IAM role for the singleton lambda function.
* We only need one function since it's just acting as a trigger.
* */
private getProviderRole(handler: lambda.SingletonFunction): iam.IRole {
Copy link
Member

Choose a reason for hiding this comment

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

  1. nit: Since the method returns either an existing role or creates a new role, i think we can rename the method name to be getOrCreateLambdaProviderRole for better readability.
  2. Can we have a unit test for this method to get an existing role and create a new role?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, will update the function name. I will add the unit test case for it.

@@ -112,6 +112,8 @@ abstract class UserBase extends Construct implements IUser {
...this.databaseProps,
user: this,
});

this.privileges.node.addDependency(table);
Copy link
Member

Choose a reason for hiding this comment

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

just for my understanding, why do we need this to be add as dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've encountered errors during stack deletion when the table is removed before its associated privileges are deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest adding the reason as a code comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, will add a comment to it.

@@ -64,6 +64,7 @@ export class DatabaseQuery<HandlerProps> extends Construct implements iam.IGrant

const provider = new customresources.Provider(this, 'Provider', {
onEventHandler: handler,
role: this.getProviderRole(handler),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use handler.role (so everything about the singleton lambda is nicely scoped inside the SingletonFunction instance and you don't need to add the constructName)?

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 handler in this case is the singleton lambda function. However, the role is not associated to the handler. Instead, the role is assumed by the custom resource provider, which is responsible for triggering the handler.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see! Thank you.

@@ -112,6 +112,8 @@ abstract class UserBase extends Construct implements IUser {
...this.databaseProps,
user: this,
});

this.privileges.node.addDependency(table);
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest adding the reason as a code comment.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jan 3, 2025
@5d 5d requested review from godwingrs22 and samson-keung January 3, 2025 20:42
Copy link
Member

Choose a reason for hiding this comment

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

QQ: Is there any reason of adding the feature flag context file into integ-runner for this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, it's auto generated. I will remove it

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Jan 3, 2025

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 db950b3 into main Jan 3, 2025
22 checks passed
@mergify mergify bot deleted the 5d/redshift-iam-role branch January 3, 2025 22:46
Copy link

github-actions bot commented Jan 3, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

redshift-alpha: Rationalization of IAM roles creation for Lambdas execution
4 participants