-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use of KMS keys across nested stacks create circular dependencies #5765
Comments
This is probably a general problem but I gave my example using lambda and SQS. #4356 describes a situation with SQS and API gateway. I believe this will apply across the board with builtin implementations of |
A reproduction code here would help a lot in the diagnosis. I've tried reproducing this myself but was not able to. Here's the code I used - #!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { NestedStack } from '@aws-cdk/aws-cloudformation';
import { Construct, Stack } from '@aws-cdk/core';
import { Queue } from '@aws-cdk/aws-sqs';
import { Function, Code, Runtime } from '@aws-cdk/aws-lambda';
import { Key } from '@aws-cdk/aws-kms';
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';
class FirstNestedStack extends NestedStack {
public readonly key: Key;
constructor(scope: Construct, id: string) {
super(scope, id);
this.key = new Key(this, 'key');
}
}
class SecondNestedStack extends NestedStack {
constructor(scope: Construct, id: string, firstStack: FirstNestedStack) {
super(scope, id);
const queue = new Queue(this, 'queue', {
encryptionMasterKey: firstStack.key,
});
const fn = new Function(this, 'function', {
code: Code.fromInline('foo'),
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler'
});
fn.addEventSource(new SqsEventSource(queue));
}
}
class MainStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
const firstStack = new FirstNestedStack(this, 'FirstNestedStack');
new SecondNestedStack(this, 'SecondNestedStack', firstStack);
}
}
const app = new cdk.App();
new MainStack(app, 'MainStack'); I was able to synthesize using |
@cmckni3 - I copied that over and added the two lines below - const app = new App();
new MainStack(app, 'MainStack');
I'm using cdk version |
Error happens on |
Ah I see. I can confirm this occurs even for the repro code I had added in my original comment here - #5765 (comment) - and occurs only during @cmckni3 - would have loved for all of this to be clearer in the issue description under the section 'reproduction steps'. That section is dedicated to be filled in with such information. |
Here's the smallest code needed to reproduce - import { App, Construct, Stack } from '@aws-cdk/core';
import { NestedStack } from '@aws-cdk/aws-cloudformation';
import { Key } from '@aws-cdk/aws-kms';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
export class MainStack extends Stack {
constructor(scope: App, id: string) {
super(scope, id);
const firstStack = new FirstNestedStack(this, 'FirstNestedStack');
new SecondNestedStack(this, 'SecondNestedStack', firstStack);
}
}
class FirstNestedStack extends NestedStack {
public readonly key: Key;
constructor(scope: Construct, id: string) {
super(scope, id);
this.key = new Key(this, 'Key');
}
}
class SecondNestedStack extends NestedStack {
constructor(scope: Construct, id: string, firstStack: FirstNestedStack) {
super(scope, id);
const fn = new Function(this, 'function', {
code: Code.fromInline('foo'),
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler'
});
firstStack.key.grantDecrypt(fn);
}
}
const app = new App();
new MainStack(app, 'MainStack'); On further debugging, the issue isn't with Since the key is defined in I suspect that the code somewhere in the cc @skinny85 who is the last major change to this area of code. |
Ok, I felt like the description and error log covered it. Guess I didn’t call out
|
We're also hitting this problem - but with a slightly different use case - we're trying to define our KMS keys in a Stack for our SNS/SQS resources; we then have an ECS Stack which contains other resources. We're trying to use the aws-cdk/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts Line 312 in e8801a0
This happens when trying to use an existing queue as a prop passed in to the ECS task. This causes the same circular dependency as provided in the simple example. |
Any updates on this issue? This seems to be an ongoing problem. |
I have a separate nested stack that creates and associates security related resources including IAM resources and KMS CMKs. Let's refer to this nested stack as
SecurityNestedStack
.I have a nested stack which contains lambdas and SQS queues. Let's refer to this nested stack as
LambdaNestedStack
.These queues are using KMS CMKs and using an IAM role from
SecurityNestedStack
. Everything for the role and cmk has been preconfigured inSecurityNestedStack
including policies for SQS IAM access, KMS IAM access, and CMK key policy.Setting up the SQSEventSource in
LambdaNestedStack
callsgrantConsumeMessages
which in turn adds two new IAM policies to the queue's IAM role. This happens in the wrong stack and causes a circular dependency.This is helpful for ease of setting up events, roles, and key policies for the queue. It's problematic in more advanced use cases and larger stacks that must be split up (200 resource limit in stacks).
Reproduction Steps
See above. I can post code if necessary.
Error Log
CloudFormation reports that all nested stacks have circular dependencies. Not completely true but there is a circular dependency across nested stacks.
Environment
Other
My workaround right now is to call
addEventSourceMapping
and pass inbatchSize
andeventSourceArn
myself OR import the role.This is 🐛 Bug Report
The text was updated successfully, but these errors were encountered: