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

silly-fireman-6851

03/19/2024, 9:22 PM
Hi folks. I need to update bucket if it exists and create bucket if it do not exists. I have been banging my head for a while but was not able to make any progress
Copy code
import pulumi
import pulumi_aws as aws


existing_bucket = aws.s3.get_bucket(bucket="my-bucket")


modified_or_new_bucket = aws.s3.Bucket(
    "997911f256644644a1cc1c6d93d75e4fer",
    bucket="my-bucket",
    acl="private",
    opts=pulumi.ResourceOptions(import_=existing_bucket.id),
)

pulumi.export("Bucket", modified_or_new_bucket.arn)
This throws an error if bucket is not present. which is valid. but how to deal with this scenario
l

little-cartoon-10569

03/19/2024, 9:54 PM
The best pattern for this is to import the bucket and manage it. In most cases, your code doesn't need to distinguish between exists and not-exists; you do that when starting to manage your environment. Pulumi can then take over and ensure everything continues to exist thereafter.
If you have a managed environment (that is, you have previously run
pulumi up
and you're re-running it to deploy changes), then your get-or-create code won't help: you know that the bucket is already managed.
If you don't have a managed environment (you've never run
pulumi up
) then you are at the right point to decide whether or not to import the bucket. Pulumi doesn't do that bit: you do.