https://pulumi.com logo
Title
b

better-translator-47169

01/16/2023, 2:57 AM
Hey all, does anyone have some simple code that checks for the existence of a S3 bucket before attempting to create a new one? Or some Pulumi config that skips over a call to
new aws.s3.Bucket
if it exists when running
pulumi up
?
w

worried-rain-74420

01/16/2023, 4:11 AM
I don’t have sample code for you, but I’m tempted to say you should be able to use “get” to lookup a bucket, and skip creating if the get is successful. https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketobject/#look-up
b

better-translator-47169

01/16/2023, 6:28 AM
Thanks. I ended up using aws.s3.getBucket to check for bucket existence in the end, similar to BucketObject. Cheers.
l

little-cartoon-10569

01/16/2023, 8:55 PM
This isn't the normal pattern for achieving this in Pulumi. If you want to have a bucket managed by Pulumi, then the if-already-exists logic should be handled manually, once. The resource would be imported (via
pulumi import
or by adding the import ID to the resource opts) if it exists, and not imported if it doesn't. This avoids conditional code that should run only once.