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

(sns): default .fifo suffix for FIFO topics #18740

Closed
1 of 2 tasks
skyrpex opened this issue Jan 30, 2022 · 4 comments
Closed
1 of 2 tasks

(sns): default .fifo suffix for FIFO topics #18740

skyrpex opened this issue Jan 30, 2022 · 4 comments
Assignees
Labels
@aws-cdk/aws-sns Related to Amazon Simple Notification Service effort/small Small work item – less than a day of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@skyrpex
Copy link
Contributor

skyrpex commented Jan 30, 2022

Description

#12386 introduced a requirement to add a topic name whenever a FIFO topic is created.

Do you think it's a good idea to generate a default name if it isn't provided by the user? All of the other CDK resources are named consistently using something similar to what cdk.Names.nodeUniqueId(node) does, ie MyStack2EC66409Topic.fifo, which prevents name collisions.

Use Case

Remove the need to manually define a topic name.

Proposed Solution

This is my current implementation:

import * as cdk from "aws-cdk-lib"
import { Construct, Node } from "constructs"

export class Topic extends cdk.aws_sns.Topic {
  public constructor(
    scope: Construct,
    id: string,
    properties?: cdk.aws_sns.TopicProps,
  ) {
    super(scope, id, {
      ...properties,
      topicName:
        properties?.topicName ?? properties?.fifo
          ? buildFifoName(scope.node, id)
          : undefined,
    })
  }
}

export function buildFifoName(node: Node, id: string) {
  const uniqueId = cdk.Names.nodeUniqueId(node)
  const suffix = `${id}.fifo`
  // Make sure that the name fits within the CFN's 80 character limit
  return `${uniqueId.slice(0, Math.max(0, 80 - suffix.length))}${suffix}`
}

Other information

No response

Acknowledge

  • I may be able to implement this feature request
  • This feature might incur a breaking change
@skyrpex skyrpex added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jan 30, 2022
@github-actions github-actions bot added the @aws-cdk/aws-sns Related to Amazon Simple Notification Service label Jan 30, 2022
@njlynch
Copy link
Contributor

njlynch commented Feb 3, 2022

It looks like CloudFormation has it on their roadmap to fix this so a topic name doesn't need to be input at creation; however, there's not been on update on the issue in over a year:
aws-cloudformation/cloudformation-coverage-roadmap#681

The above proposal -- to autogenerate a name and suffix the '.fifo' if a name is not provided -- sounds reasonable to me. Contributions welcome!

@njlynch njlynch added effort/small Small work item – less than a day of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. good first issue Related to contributions. See CONTRIBUTING.md p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 3, 2022
@njlynch njlynch removed their assignment Feb 3, 2022
@mjsztainbok
Copy link

mjsztainbok commented Apr 12, 2022

There is an issue with the code above. If you look at SQS (for example), the names for queues are generated using the stack name as the prefix. In this case though it looks like Names.uniqueId() just uses the ID of the stack for the prefix of the unique ID which causes issues if you have multiple instances of the same stack but with different stack names (which is what we are doing so that we can have individual stacks for development and CI/CD test purposes).

I modified the code as follows to correct this:

 private static replaceStackId(scope: Construct, id: string): string {
    const stack = cdk.Stack.of(scope);

    // Replace the stack ID in the identifier with the stack name
    return id.replace(stack.node.id, stack.stackName);
  }

  private static buildFifoName(scope: Construct, id: string) {
    const uniqueId = this.replaceStackId(
      scope,
      cdk.Names.uniqueId(scope)
    );
    const suffix = `${id}.fifo`;
    // Make sure that the name fits within the CFN's 80 character limit
    return `${uniqueId.slice(0, Math.max(0, 80 - suffix.length))}${suffix}`;
  }

@TheRealAmazonKendra TheRealAmazonKendra self-assigned this Oct 5, 2022
mergify bot pushed a commit that referenced this issue Oct 11, 2022
…2375)

This PR adds logic for automatic generation of FIFO topics.

When customers don't pass a `topicName` during creation, the construct now generates an unique name that respects the maximum length allowed by the CloudFormation resource (256) and that ends with the `.fifo` suffix.

The previous behavior was to throw an error when the topic was of type FIFO and no name was passed.

Fixes ##18740

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@dreamorosi
Copy link
Contributor

I think this issue could be closed. The PR with the fix has been merged and the new behavior has been released yesterday

@mrgrain mrgrain closed this as completed Nov 15, 2022
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-sns Related to Amazon Simple Notification Service effort/small Small work item – less than a day of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
Development

No branches or pull requests

6 participants