-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
(aws-sns): addSubscription() doesn't lead to working subscription #12120
Comments
I analyzed your example to figure out what is really missing. const key = new kms.Key(this, 'Key');
const mySqs = new sqs.Queue(this, 'MyQueue', {
encryption: sqs.QueueEncryption.KMS,
encryptionMasterKey: key,
});
const myTopic = new sns.Topic(this, 'MyTopic', {
masterKey: key,
});
myTopic.addSubscription(new subscriptions.SqsSubscription(mySqs));
key.addToResourcePolicy(
new PolicyStatement({
sid: 'Allow SNS to use this key',
effect: iam.Effect.ALLOW,
principals: [new iam.ServicePrincipal('sns.amazonaws.com')],
actions: ['kms:Decrypt', 'kms:GenerateDataKey'],
resources: ['*'],
}),
);
mySqs.addToResourcePolicy(
new PolicyStatement({
sid: 'allow sqs actions',
effect: Effect.ALLOW,
principals: [new AccountRootPrincipal()],
actions: ['sqs:*'],
resources: ['*'],
}),
);
const allowSns = new PolicyStatement({
sid: 'allow publishing from the sns',
effect: Effect.ALLOW,
principals: [new ServicePrincipal('sns.amazonaws.com')],
actions: ['sqs:sendMessage'],
resources: ['*'],
});
allowSns.addCondition('ArnLike', {
// Annoyingly, you can't just ref the ARN. Chicken & egg.
// Or at least I don't know how to get around the issue without resorting to L1 stuff.
'aws:SourceArn': `arn:aws:sns:${props.env.region}:${props.env.account}:${mySnsName}`,
});
mySqs.addToResourcePolicy(allowSns); In my example, CDK already generates a queue policy for SNS service principal. Can you please double check this part again? Do you really need to add this policy? "MyQueuePolicy6BBEDDAC": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "sqs:SendMessage",
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Ref": "MyTopic86869434"
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Resource": {
"Fn::GetAtt": [
"MyQueueE6CA6235",
"Arn"
]
}
}
],
"Version": "2012-10-17"
},
"Queues": [
{
"Ref": "MyQueueE6CA6235"
}
]
}
} |
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled. |
@jumi-dev thank you for looking! I think part 2 was necessary for older versions of CDK and could have been implemented using the Regarding the 3rd stanza, it was required when I initially wrote this. I don't know if it is still required since it's been almost a year. I'd be happy to see that it isn't required anymore. |
I dont know if its relateed to this, but last week I did this type of sqs subscribed to sns topic and messages didnt arrive the queue... I have been following this to know if may be related... I was able to see subscription in aws console, but because messages where not arriving sqs, I thought if it may be related to policy mentioned here... This was my code
|
@ayozemr you must use a CMK and not |
@ahammond thanks for the info, will modify code with that in mind And yes, throwing and error would help a lot to know whats the problem. Because you end in a situation where events don't reach SQS with no info why. Thanks again |
What's the status here? Stumbled on this few days ago? |
This was fixed by #2504 released in https://github.com/aws/aws-cdk/releases/tag/v1.111.0 (July 2nd 2021). As others have noted, you have to specify encryptionMasterKey for your SQS because you must use a custom CMK, not an AWS key. @ahammond, there is an open ticket to verify that: #19796 @ayozemr, you can (now?) get information about why your SNS messages don't reach SQS. You can set up delivery status logging to CloudWatch for SNS failures (and successes). Unfortunately it is not supported by CloudFormation, so that will have to be done outside CDK for quite some time. https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html#topics-attrib |
|
To subscribe an SNS to an SQS, you can write something like
and then your messages will disappear.
Reproduction Steps
What did you expect to happen?
Everything necessary to get the SNS publishing messages into the SQS should be handled. Or at least tell me what I need to do in addition to get it working.
What actually happened?
Messages disappeared into the void.
Environment
Other
For it to actually work, you need
I'm not sure the Right Way for cdk to do this. Ideally,
new SqsSubscription(mySqs)
would validate that the SQS has a policy that looks reasonable and has a CMKmySns.addSubscription()
would validate that the SNS involved has a CMK and that it has a reasonable policyWorking code snippet for The Next Person:
This is 🐛 Bug Report
The text was updated successfully, but these errors were encountered: