I’m trying to figure out a good pattern for the fo...
# aws
f
I’m trying to figure out a good pattern for the following scenario: I want to create a pulumi.aws.rds.Instance Before creation I want to check if there in existing database snapshot, and if that is the case I want to create the database using the snapshot, of there isn’t the database should be created normally. The whole operation should only check the first time
pulumi up
is run, subsequent runs should not check again, and thus not try to alter the database, even if a snapshot has been create before the subsequent
pulumi up
is run. Basically:
Copy code
snapshot = find_latest_snapshot(filter=some_useful_filter)
if snapshot: # or maybe snapshot.id or similar
   create_db_from_snapshot(snapshot)
else:
   create_db_without_snapshot()
Would be nice if e.g. LatestSnapsshot was a resource, with the check for existing snapshot was done in the
create
method of a dynamic provider, but any way that works is fine. The problem with a resource for this is of course that I would get a
pulumi.Output
back, so I suspect I would have wrap it all in an
Output.apply
to get a useful value to check against. I also need to do the same for an ebs volume snapshot, but if I can find a good pattern for the above, I*m sure I can use the same for the volume snapshot. If this makes any kind of sense, and if anyone has any tips, I would be very happy.
m
If I understand you correctly, you're managing an RDS instance with Pulumi, and you want to create this RDS instance from an existing snapshot if such a snapshot exists. I think in the simplest case, you don't need to do anything special, because the
SnapshotIdentifier
input can either be
None
or the string ID. If I understand correctly, since https://github.com/hashicorp/terraform-provider-aws/pull/18013 (this is the upstream provider that
pulumi-aws
uses) you can remove the
SnapshotIdentifier
without triggering recreation of the RDS instance. (You just can't set it to a different snapshot ID.) Hence, it might be easiest to do the snapshot ID lookup only if the RDS instance does not yet exist (e.g., check via
aws.rds.getInstance
or even outside of the Pulumi program), and otherwise set the snapshot ID to
None
.
f
In the end, using pulumi.export and Stackreference.get_output to "store" the value of the snapshot, in a slightly convoluted chain of Output.apply, worked. I will be add a check if the DB instance exists (@modern-zebra-45309 thanks for that idea, btw), to make it more straightforward. I believe the same effect could have been achieved with a Custom Resource, and Output.apply chain, but since the export is also informative, it was the easy solution.