Skip to content
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

feat(sns): add fromTopicName api to import a sns topic via the topic name #26635

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/aws-sns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ const topic = new sns.Topic(this, 'Topic', {

Note that FIFO topics require a topic name to be provided. The required `.fifo` suffix will be automatically generated and added to the topic name if it is not explicitly provided.

## Import
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use the term "import". This is confusing because it doesn't actually import resources into the stack (which people might otherwise think it does).

We prefer the term "referencing existing resources".


Any SNS topics that has been created outside the stack can be imported into your CDK stack.

Topics can be imported by their ARN via the `Topic.fromTopicArn()` API or using the topic name via the `StateMachine.fromTopicName()` API.

```ts
const topic = sns.Topic.fromTopicArn(
this,
'ViaArnImportedStateMachine',
'arn:aws:sns:us-east-1:123456789012:my-topic'
);

const topic = sns.Topic.fromTopicName(
this,
'ViaResourceNameImportedStateMachine',
'my-topic'
);
```

## Subscriptions

Various subscriptions can be added to the topic by calling the
Expand Down
15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-sns/lib/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ export class Topic extends TopicBase {
return new Import(scope, id);
}

/**
* Import an existing SNS topic provided the topic name
* @param scope The parent creating construct
* @param id The construct's name
* @param topicName The topic name (i.e. MyTopic)
*/
public static fromTopicName(scope: Construct, id: string, topicName: string): ITopic {
const topicArn = Stack.of(scope).formatArn({
service: 'sns',
resource: topicName,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
return this.fromTopicArn(scope, id, topicArn);
}

public readonly topicArn: string;
public readonly topicName: string;
public readonly fifo: boolean;
Expand Down
27 changes: 27 additions & 0 deletions packages/aws-cdk-lib/aws-sns/test/sns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,33 @@ describe('Topic', () => {
expect(imported.fifo).toEqual(true);
});

test('fromTopicName', () => {
// GIVEN
const stack2 = new cdk.Stack();

// WHEN
const imported = sns.Topic.fromTopicName(stack2, 'Imported', 'my_corporate_topic');

// THEN
expect(imported.topicName).toEqual('my_corporate_topic');
expect(imported.topicArn).toEqual('arn:aws:sns:*:123456789012:my_corporate_topic');
expect(imported.fifo).toEqual(false);

});

test('fromTopicName fifo', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const imported = sns.Topic.fromTopicName(stack, 'Imported', 'mytopic.fifo');

// THEN
expect(imported.topicName).toEqual('mytopic.fifo');
expect(imported.topicArn).toEqual('arn:aws:sns:*:123456789012:mytopic.fifo');
expect(imported.fifo).toEqual(true);
});

test('test metrics', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Loading