curious if anyone has a good example TS code for c...
# typescript
e
curious if anyone has a good example TS code for creating resources if param was passed in or not and returning either the string returned, or the pulumi equivalent. For example, if in a config file a certificate arn is passed in, then use that cert arn when creating a cloudfront resource, but if the cert isn’t passed in, then create the cert and pass in the cert arn
p
Something like this?
d
I tend to wrap all the infra in classes, init all the infra in the constructor and then have readonly fields to pass things between different stacks/classes/pieces of infra
e
p
If that works 👍 I’m attaching what is (perhaps) a cleaner implementation.
config.get()
will return
undefined
if that key isn’t there, so the
|| "";
is probably unnecessary. Undefined also lets you use the ternary operator in line 14 of this snippet. This lets the
createCertificate
function to remain pretty simple.
Just note, the
createCertificate
function uses the same resource name every time. So if you need to call it more than once, you’ll need to come up with a way to make
my-cert
unique each call.