sst / ion

SST v3
https://sst.dev
MIT License
1.88k stars 221 forks source link

Should we override the resource name when making it linkable because the original name is not suitable or contains illegal characters? #491

Closed mario-shang closed 4 months ago

mario-shang commented 4 months ago

This is my use case:

  1. I want to use the existing topic here, but the original name is like "xxx-xxx," so building here will cause an error.
  2. Although I want to create a new topic in the dev stage and use the existing topic in the prod stage, but the generated name is different, which makes it unusable.
export async function getExistsTopic(topicName: string): Promise<aws.sns.Topic | undefined> {
  const res = await aws.sns.getTopic({ name: topicName })
  if (!res.id || !res.name) {
    console.warn(`The topic ${topicName} doesn't exists.`);
    return
  }
  const existsTopic = aws.sns.Topic.get(res.name, res.id);
  // build the resource param will cause error
  // sst.Link.makeLinkable(aws.sns.Topic, function () {
  //   return {
  //     properties: {
  //       arn: this.arn,
  //     },
  //   };
  // })
  sst.Link.AWS.makeLinkable(aws.sns.Topic, function () {
    return [
      {
        actions: ["sns:*"],
        resources: [this.arn],
      },
    ];
  });
  return existsTopic;
}

https://github.com/sst/ion/blob/05e099d8b75d23528202667198e72832b803c941/pkg/platform/src/components/link.ts#L46-L59

mario-shang commented 4 months ago

I changed to using the Pulumi topic construct with the id in the options to avoid using the original name, like this:

export async function getExistsTopic(originTopicName: string): Promise<aws.sns.Topic | undefined> {
  const res = await aws.sns.getTopic({ name: originTopicName });
  if (!res.id) {
    console.warn(`The topic ${originTopicName} doesn't exists.`);
    return;
  }
  const topic = new aws.sns.Topic(TopicName, undefined, {
    id: res.id,
  });
  sst.Link.makeLinkable(aws.sns.Topic, function () {
    return {
      properties: {
        arn: this.arn,
      },
    };
  });
  sst.Link.AWS.makeLinkable(aws.sns.Topic, function () {
    return [
      {
        actions: ["sns:*"],
        resources: [this.arn],
      },
    ];
  });
  return topic;
}