aws-cdk: [lambda] Circular dependency when trying to add policy to invoke itself
Reproduction Steps
const myLambda = new lambda.Function(this,'myLambda, {
...
});
const myLambdaInvokePolicyStatement = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [ 'lambda:InvokeFunction' ],
resources: [ myLambda.functionArn ]
});
myLambda.addToRolePolicy(myLambdaInvokePolicyStatement);
What did you expect to happen?
CDK to be able to add the policy so my lambda can invoke itself.
What actually happened?
CDK is throwing ValidationError.
Stack failed: Error [ValidationError]: Circular dependency between resources: [...]
Environment
- CLI Version : 1.69.0
- Framework Version: 1.69.0
- Node.js Version: v12.17.0
- OS : MacOS 10.15.7
- Language (Version): TypeScript (3.7.2)
Other
This is đ Bug Report
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (5 by maintainers)
Commits related to this issue
- Fixes circular dependency Nice reading here: https://github.com/aws/aws-cdk/issues/11020 — committed to umccr/cttso-ica-to-pieriandx by alexiswl a year ago
The problem seems to be that with this dependency CloudFormation needs to create the lambda before the ServiceRole and like always the ServiceRole before the lambda like someone stated before.
We worked around that by introducing an additional policy into this circle:
This works for us. I would guess that CloudFormation can then create it like this: ServiceRole -> Lambda -> Policy -> (Attach Policy to Role)
This is because the lambda permission node adds a
GetAtton the lambda function that is yet to be created. So the âPermissionâ cannot be created until the âFunctionâ is created and the permission cannot be created without the âFunction ARNâ being available.You can use the following workaround -
I think thereâs a missing bracket at the end of the initialisation of the const variable policy if you want to make an update there as well. đ
Thanks that helped!