(lambda,iam): using a shared role for more than 1 function while enabling lambda insights generates problematic CloudFormation #17552
Labels
@aws-cdk/aws-iam
Related to AWS Identity and Access Management
bug
This issue is a bug.
effort/small
Small work item – less than a day of effort
p1
What is the problem?
Having noticed the introduction of the lambda function prop
insightsVersion
, I wanted to refactor some code (See the code in Other Information) to make use of that property instead of explicitly attaching a layer and adding the required managed policy similar to what's defined here.I have multiple Lambda functions using the same explicitly created IAM role passed to those functions via the
role
property.The issue that happens is that the generated CloudFormation ends up having 2 repeated entries for the role's
ManagedPolicyArns
propertyIgnoring the wasted duplication, when deploying a CloudFormation template containing such code, CloudFormation deployment fails.
I've checked various pieces of code in lambda and iam modules
function.ts
's configureLambdaInsights method which attaches the layer and adds theAwsManagedPolicy
managed-policy.ts
's fromAwsManagedPolicyName which has the arn as a token - more specifically a Lazy.uncachedStringrole.ts
's addManagedPolicy which seems to try to do the right thing by attempting to only add a policy to the role only if it has not been added before. I believe this to be the source of the issue, because since the managed policy in this cases uses token, the equality comparison failsReproduction Steps
Here's a skeleton code for creating 1 role, 2 lambda functions, use the role in the 2 lambdas while enabling insights for both. When synthesized, it generates the CloudFormation that will fail deployment due to the duplicate entries in the role's
ManagedPolicyArns
A simple check to verify what I think to be the root cause
What did you expect to happen?
CloudWatchLambdaInsightsExecutionRolePolicy
managed policyWhat actually happened?
CloudWatchLambdaInsightsExecutionRolePolicy
managed policyCDK CLI Version
1.132.0
Framework Version
No response
Node.js Version
14
OS
macOS 11.6.1
Language
Typescript
Language Version
No response
Other information
A couple of thoughts/ideas
iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchLambdaInsightsExecutionRolePolicy')
into a constant that's reused across all function instantiations. This way the comparison will always succeed as the token object is always the same.My current implementation for setting up Lambda Insights is to call the following utility method
The text was updated successfully, but these errors were encountered: