Skip to content

Commit

Permalink
fix(redshift-alpha): use same role for database-query singleton function
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Dec 12, 2024
1 parent 8dc18f4 commit cc05f22
Show file tree
Hide file tree
Showing 20 changed files with 1,184 additions and 1,018 deletions.
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-redshift-alpha/lib/private/database-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

const queryHandlerProps: DatabaseQueryHandlerProps & HandlerProps = {
Expand Down Expand Up @@ -116,4 +117,20 @@ export class DatabaseQuery<HandlerProps> extends Construct implements iam.IGrant
}
return adminUser;
}

/**
* 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 {
const id = handler.constructName + 'ProviderRole';
const existing = cdk.Stack.of(this).node.tryFindChild(id);

return existing != null
? existing as iam.Role
: new iam.Role(this, id, {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole')],
});
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"TableProviderframeworkonEventServiceRoleC3128F67": {
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
Expand Down Expand Up @@ -637,7 +637,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D": {
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
Expand Down Expand Up @@ -671,10 +671,10 @@
],
"Version": "2012-10-17"
},
"PolicyName": "TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D",
"PolicyName": "TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC",
"Roles": [
{
"Ref": "TableProviderframeworkonEventServiceRoleC3128F67"
"Ref": "TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A"
}
]
},
Expand Down Expand Up @@ -704,7 +704,7 @@
"Handler": "framework.onEvent",
"Role": {
"Fn::GetAtt": [
"TableProviderframeworkonEventServiceRoleC3128F67",
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A",
"Arn"
]
},
Expand All @@ -720,8 +720,8 @@
"Timeout": 900
},
"DependsOn": [
"TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D",
"TableProviderframeworkonEventServiceRoleC3128F67"
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC",
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A"
],
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"TableProviderframeworkonEventServiceRoleC3128F67": {
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
Expand Down Expand Up @@ -637,7 +637,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D": {
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
Expand Down Expand Up @@ -671,10 +671,10 @@
],
"Version": "2012-10-17"
},
"PolicyName": "TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D",
"PolicyName": "TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC",
"Roles": [
{
"Ref": "TableProviderframeworkonEventServiceRoleC3128F67"
"Ref": "TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A"
}
]
},
Expand Down Expand Up @@ -704,7 +704,7 @@
"Handler": "framework.onEvent",
"Role": {
"Fn::GetAtt": [
"TableProviderframeworkonEventServiceRoleC3128F67",
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A",
"Arn"
]
},
Expand All @@ -720,8 +720,8 @@
"Timeout": 900
},
"DependsOn": [
"TableProviderframeworkonEventServiceRoleDefaultPolicyAD08715D",
"TableProviderframeworkonEventServiceRoleC3128F67"
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRoleDefaultPolicyBBFA0AEC",
"TableQueryRedshiftDatabase3de5bea727da479686625efb56431b5fProviderRole71E75E1A"
],
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down

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

Loading

0 comments on commit cc05f22

Please sign in to comment.