https://pulumi.com logo
#general
Title
# general
s

strong-vr-90029

11/28/2023, 4:32 PM
i want to create sqs in multi region
Copy code
import * as pulumi from '@pulumi/pulumi';
import * as aws from '@pulumi/aws';
import { sqsConfig } from './constract';

export class MySqs {
    static create(params: sqsConfig) {
        const regions: aws.Region[] = ["us-east-1", "us-east-2"];
        for (const region of regions) {
            // Create a new AWS provider for the specified region
            const provider = new aws.Provider(`${region}-provider`, { region: region });
            // Create a new SQS queue in the specified region using the corresponding provider
            const sqsQueue = new aws.sqs.Queue(`${region}-queue`, {}, { provider: provider });
        }
    }
}
when running this code i have this failure : error: Duplicate resource URN 'urnpulumistage:xxx deployment infrapulumiprovidersaws:us-east-2-provider'; try giving it a unique name , someone can assist ? how to solve it 🙂 ??
d

dry-keyboard-94795

11/28/2023, 7:02 PM
Please don't double post, at most link to the original message. It sounds like you have another provider instance elsewhere in your code. You should have a single provider instance per region in your case, then pass it around into your class instances for use. You could also reference them as globals from a separate module
l

little-cartoon-10569

11/28/2023, 9:40 PM
This code can be called multiple times (it's a static function) but the same names will be used each time, hence the clash. You need to either ensure that the code is called only once, or pass in a discriminator that you can use in the resource names.
The convention in Pulumi is to have a name parameter as the first parameter (cf. every resource constructor), and use this as the discriminator:
Copy code
static create(name: string, params: sqsConfig) {
        const regions: aws.Region[] = ["us-east-1", "us-east-2"];
        for (const region of regions) {
            // Create a new AWS provider for the specified region
            const provider = new aws.Provider(`${name}-${region}-provider`, { region: region });
            // Create a new SQS queue in the specified region using the corresponding provider
            const sqsQueue = new aws.sqs.Queue(`${name}-${region}-queue`, {}, { provider: provider });
        }
    }
s

strong-vr-90029

11/29/2023, 10:20 AM
perfect 🙂