aws-cdk: Cognito circular reference when setting lambda trigger permissions
Create a lambda Create a user pool Assign the lambda to one of the user pool triggers Set the permissions on the lambda to call Cognito APIs against the user pool Get circular reference error in cdk deploy
Reproduction Steps
const postAuthentication = new lambda.Function(this, "postAuthentication", {
description: "Cognito Post Authentication Function",
runtime: lambda.Runtime.NODEJS_12_X,
handler: "postAuthentication.handler",
code: lambda.Code.asset("dist/postAuthentication"),
timeout: cdk.Duration.seconds(30),
memorySize: 256,
environment: {},
});
const userPool = new cognito.UserPool(this, userPoolName, {
....
lambdaTriggers: {
postAuthentication,
},
});
const postAuthPermissionPolicy = new iam.PolicyStatement({
actions: ["cognito-idp:AdminDeleteUserAttributes", "cognito-idp:AdminAddUserToGroup"],
resources: [userPool.userPoolArn],
});
// now give the postAuthentication lambda permission to change things
postAuthentication.addToRolePolicy(postAuthPermissionPolicy);
Error Log
Cognito failed: Error [ValidationError]: Circular dependency between resources
Environment
- CLI Version : 1.31.0
- Framework Version:
- OS :
- Language : Typescript
Other
This is 🐛 Bug Report
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 32
- Comments: 34 (10 by maintainers)
Commits related to this issue
- fix(lambda): circular reference when Cognito permissions are added to function role Lambda functions can be used as [triggers] on Cognito user pools. It is common for these triggers to query (callbac... — committed to aws/aws-cdk by deleted user 7 years ago
- fix(lambda): updating addToRolePolicy to avoid circular dependency potential (under feature flag) (#33291) ## Issue Closes https://github.com/aws/aws-cdk/issues/7016 I did NOT mean to drop the branc... — committed to aws/aws-cdk by IkeNefcy 2 years ago
- fix(lambda): updating addToRolePolicy to avoid circular dependency potential (under feature flag) (#33291) ## Issue Closes https://github.com/aws/aws-cdk/issues/7016 I did NOT mean to drop the branc... — committed to yashkh-amzn/aws-cdk by IkeNefcy 2 years ago
@markcarroll -
The workaround for this issue is to not use the
addToRolePolicy()but instead toattachInlinePolicy(). See code snippet below -Can you check if this fixes this issue for you?
I have the same issue with providing the lambda function with the list of app clientIds as environment variables upon creation.
Has this been looked into ?
Today I noticed that userPoolId is inside event object for lambda.
I am facing a similar problem because I’m trying to pass the userPoolId in the Lambda trigger. Is there a way to know the userPoolId without passing it as an env var?
This is still an issue in 2024. The
attachInlinePolicymethod works but are you guys planning to fix this or put it on the back burner for another 4 years? Or is this working as intended?Leave this open. I’d like to get a better solution for this in place. It’s more complicated than I originally thought it would be.
The
grant*functions do not solve this problem. However, I’ve classified this as a bug.I ran into this issue but regarding using a User Pool Id or User Pool Client Id as an environment variable for a Lambda function I was assigning as a trigger to the User Pool. My fix for this was to use
addTrigger()on the User Pool after setting everything up and usinglambda.Function.fromFunctionArn()to reference the function.I’m facing a similar issue, I’m creating the lambdas after the UserPool and AppClients are created, I then use the app clients to populate a few env variables for the lambdas when creating them (I need a mapping so the event client id is not useful here), and then I just add them as triggers, so the issue is around the env variables, if I dont use the clients to populate it it works, otherwise just throws a circular reference
For me even attachInlinePolicy creates an error Do we know when the bug will be fixed?
I think trigger needs to be a new element like: new CognitoTrigger so the dependson can be added
this save me to avoid the error when passing variable environment to lambda
You can use event.userPoolId inside of the lamba
this approach is the only way we have been able to solve for this currently
Lambda receives userPoolId in the event argument.