cloudformation-diff crashes if forced to diff against a non-CDK template
See original GitHub issue@aws-cdk/cloudformation-diff/lib/iam does not handle Fn::If
in IAM policy statements in existing CloudFormation stacks.
Reproduction Steps
- Create a CloudFormation stack with raw CloudFormation and utilize
Fn::If:
in IAM policies.
for example:
Parameters:
AttachLambdaFunctionToVPC: { Type: String, Default: 'false', AllowedValues: ['true', 'false']}
Conditions:
RunLambdaInVPC:
Fn::Equals: [ {Ref: AttachLambdaFunctionToVPC}, "true"]
Resources:
LambdaRole:
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: '2012-10-17'
Policies:
- Fn::If:
- RunLambdaInVPC
- PolicyDocument:
Statement:
- Action: ['ec2:CreateNetworkInterface', 'ec2:DescribeNetworkInterfaces', 'ec2:DeleteNetworkInterface']
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: lambdaRoleAPIG
- {Ref: 'AWS::NoValue'}
Type: AWS::IAM::Role
alternatively, this following valid policy definition will also result in an error:
Policies:
- PolicyDocument:
Statement:
- Action: ['cloudwatch:*', 'logs:*', 'dynamodb:GetItem', 'dynamodb:PutItem']
Effect: Allow
Resource: '*'
- Fn::If:
- RunLambdaInVPC
- Action: ['ec2:CreateNetworkInterface', 'ec2:DescribeNetworkInterfaces', 'ec2:DeleteNetworkInterface']
Effect: Allow
Resource: '*'
- {Ref: 'AWS::NoValue'}
Version: '2012-10-17'
PolicyName: lambdaRoleAPIG
- Rewrite the IAM role in CDK
- Synthesize templates
- Run
cdk diff
orcdk deploy
against the CloudFormation stack created in step 1
Error Log
Errors observed when running cdk diff
or cdk deploy
for the templates listed in the repro step respectively:
TypeError: Cannot read property 'Statement' of undefined
at /Users/yangtina/.../node_modules/@aws-cdk/cloudformation-diff/lib/iam/iam-changes.ts:187:106
TypeError: Cannot use 'in' operator to search for 'NotResource' in {"Fn::If":["RunLambdaInVPC",{"Action":["ec2:CreateNetworkInterface","ec2:DescribeNetworkInterfaces","ec2:DeleteNetworkInterface"],"Effect":"Allow","Resource":"*"}]}
at new Targets (/Users/yangtina/.../node_modules/@aws-cdk/cloudformation-diff/lib/iam/statement.ts:160:20)
Environment
- CLI Version : 1.27.0 (build a98c0b3)
- Framework Version:
- OS : macOS Mojave 10.14.6
- Language : typescript
Other
The use case this serves is for migrating a production services originally created with CloudFormation to CDK. To avoid doing endpoint migrations for production services due to essential resources being recreated by CloudFormation, we are writing CDK to target an existing stack previously defined by raw CloudFormation instead of creating a whole new CDK stack.
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Troubleshooting common AWS CDK issues
When synthesizing an AWS CDK stack, I receive an error because the AWS CloudFormation template contains too many resources. I specified three (or...
Read more >Lesson learned while I'm using CDK | by Jaewoo Ahn - Medium
CDK offers cdk diff command which compares the specified stack with the deployed stack or a local CloudFormation template. It will show IAM...
Read more >Open CDK Guide - The Open Construct Foundation
Deployments · when checking in CR for CDK code, include output of cdk diff in the code review · its a good idea...
Read more >Top 5 @aws-cdk/cloudformation-diff Code Examples - Snyk
Learn more about how to use @aws-cdk/cloudformation-diff, based on @aws-cdk/cloudformation-diff code examples created from the most popular ways it is used ...
Read more >CDK tips, part 3 – how to unblock cross-stack references
During cdk deploy '*' , CloudFormation noticed that the template of ... Keep running cdk diff until it shows no edits or deletions...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
Our team is also impacted by this issue. The context is the same as @yangtina described above. Our project is in the process of migrating from CloudFormation to CDK and contains a mix of CloudFormation templates and CDK.
Due to this crash we have either to
Fn::If
is used inside IAM policiesor
--require-approval
tonever
(in this casecdk diff
is not called during deployment). Again, this workaround in turn can mask sensitive changes to the infrastructure 😞Error stack trace:
Best regards, Filipp
This is similar to https://github.com/aws/aws-cdk/issues/12887. We should definitely fix it.