sst / ion

❍ — a new engine for SST

Home Page:https://ion.sst.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mario-shang opened this issue · comments

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;
}

export function build(links: any[]) {
return links
.filter((l) => isLinkable(l))
.map((l) => {
const link = l.getSSTLink();
return all([l.urn, link.properties]).apply(([urn, properties]) => ({
name: urn.split("::").at(-1)!,
properties: {
...properties,
type: urn.split("::").at(-2)!,
},
}));
});
}

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;
}