Skip to content

Commit

Permalink
Explicitly set event schedule binding to Topic
Browse files Browse the repository at this point in the history
  • Loading branch information
biffgaut committed Nov 6, 2023
1 parent 477d26b commit 3ff8238
Show file tree
Hide file tree
Showing 9 changed files with 999 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as sns from 'aws-cdk-lib/aws-sns';
import * as events from 'aws-cdk-lib/aws-events';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as defaults from '@aws-solutions-constructs/core';
import * as cdk from 'aws-cdk-lib';
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
import { Construct } from 'constructs';
import { overrideProps } from '@aws-solutions-constructs/core';
Expand Down Expand Up @@ -107,10 +108,22 @@ export class EventbridgeToSns extends Construct {

this.snsTopic = buildTopicResponse.topic;
this.encryptionKey = buildTopicResponse.key;

// Setup the event rule target as sns topic.

// The CDK generally avoids resource names that are too long, but in this case the maximum SNS topic name is 256 characters and the maximum
// binding id is 64 characters, so a long SNS topic name (driven by Stack id, Construct id, etc.) breaks upon launch. Because of this, we take
// control of the physical name ourselves.
const maxBindingIdLength = 64;
const nameParts: string[] = [
cdk.Stack.of(scope).stackName, // Name of the stack
id, // Construct ID
];
const generatedTopicName = defaults.generatePhysicalName("", nameParts, maxBindingIdLength);

const topicEventTarget: events.IRuleTarget = {
bind: () => ({
id: this.snsTopic.topicName,
id: generatedTopicName,
arn: this.snsTopic.topicArn
})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ test('check if the event rule has permission/policy in place in sns for it to be
);
});

test('check events rule properties', () => {
test.only('check events rule properties', () => {
const stack = new cdk.Stack();
deployNewStack(stack);

Expand All @@ -155,9 +155,24 @@ test('check events rule properties', () => {
Ref: "testSnsTopic42942701"
},
Id: {
"Fn::GetAtt": [
"testSnsTopic42942701",
"TopicName"
"Fn::Join": [
"",
[
"Defaulttest-",
{
"Fn::Select": [
2,
{
"Fn::Split": [
"/",
{
Ref: "AWS::StackId"
}
]
}
]
}
]
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"testconstructEncryptionKey6153B053": {
"Type": "AWS::KMS::Key",
"Properties": {
"EnableKeyRotation": true,
"KeyPolicy": {
"Statement": [
{
Expand Down Expand Up @@ -49,8 +50,7 @@
}
],
"Version": "2012-10-17"
},
"EnableKeyRotation": true
}
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
Expand Down Expand Up @@ -179,9 +179,24 @@
"Ref": "testconstructSnsTopic44188529"
},
"Id": {
"Fn::GetAtt": [
"testconstructSnsTopic44188529",
"TopicName"
"Fn::Join": [
"",
[
"exist-bustest-construct-",
{
"Fn::Select": [
2,
{
"Fn::Split": [
"/",
{
"Ref": "AWS::StackId"
}
]
}
]
}
]
]
}
}
Expand Down
Loading

0 comments on commit 3ff8238

Please sign in to comment.