icy-controller-6092
08/24/2022, 10:24 PMimport * as aws from '@pulumi/aws'
import * as pulumi from '@pulumi/pulumi'
export class Builder {
constructor(private provider: aws.Provider, private nameSuffix: string) {}
createResource<T extends typeof pulumi.Resource> (
Resource: T,
name: string,
args: ConstructorParameters<T>[1]
) {
const regionalName = `${name}_${this.nameSuffix}`
return new Resource(name, args, {
provider: this.provider,
})
}
}
little-cartoon-10569
08/24/2022, 10:33 PMicy-controller-6092
08/24/2022, 10:42 PMopts
parameter)
If I didn’t do it this way, it’s too easy to forget to add a suffix to the resource name, or to forget to pass the provider into a resource constructor (and I want to keep a default provider, because only 20% of my resources are deployed into multiple regions)little-cartoon-10569
08/24/2022, 10:43 PMicy-controller-6092
08/24/2022, 10:43 PMlittle-cartoon-10569
08/24/2022, 10:43 PMicy-controller-6092
08/24/2022, 10:44 PMlittle-cartoon-10569
08/24/2022, 10:44 PMicy-controller-6092
08/24/2022, 10:44 PMlittle-cartoon-10569
08/24/2022, 10:45 PMicy-controller-6092
08/24/2022, 10:45 PMconst createRegionalBucket = (provider) => {
return new Bucket('mybucket', {}, { provider })
}
createRegionalBucket(x)
createRegionalBucket(y)
awsx.apigateway.API
little-cartoon-10569
08/24/2022, 10:46 PMicy-controller-6092
08/24/2022, 10:46 PMlittle-cartoon-10569
08/24/2022, 10:47 PMicy-controller-6092
08/24/2022, 10:47 PMconst createRegionalResources (region: aws.Region) => {
const provider = new aws.Provider(region, { region })
new Resource1('a', { ... }, { provider })
new Resource2('b', { ... }, { provider })
// etc
}
this is how I was doing it to start with, and I was seeing errors with name clashes, not just with the S3 bucketlittle-cartoon-10569
08/24/2022, 10:51 PMonst createRegionalBucket = (name, provider) => {
return new Bucket(name, {}, { provider })
}
createRegionalBucket("one", x)
createRegionalBucket("two", y)
name is not a suitable field for autogenerating. It should be chosen by the app writer with forethought and good reason. Having library / general code generate a name for you like this does not work with with either Pulumi or cloud providers. I'm not trying to be abrasive: this is advice given from years of experience in this area.icy-controller-6092
08/24/2022, 10:52 PMlittle-cartoon-10569
08/24/2022, 10:53 PMicy-controller-6092
08/24/2022, 10:53 PMcreateRegionalBucket
for all resources in the regional stack would require a line of code for every resource inside the stack for every regionlittle-cartoon-10569
08/24/2022, 10:53 PMicy-controller-6092
08/24/2022, 10:57 PMa
and the b
folder is how I propose to solve itlittle-cartoon-10569
08/24/2022, 11:15 PMicy-controller-6092
08/24/2022, 11:19 PMconst x = createLocalResources()
createRegionalStack('ap-southeast-2, x)
createRegionalStack('us-west-1', x)
little-cartoon-10569
08/24/2022, 11:20 PMicy-controller-6092
08/24/2022, 11:21 PMlittle-cartoon-10569
08/24/2022, 11:22 PMicy-controller-6092
08/24/2022, 11:23 PMlittle-cartoon-10569
08/24/2022, 11:23 PM