https://pulumi.com logo
#typescript
Title
# typescript
e

elegant-crayon-4967

07/19/2021, 7:37 PM
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

prehistoric-london-9917

07/19/2021, 8:16 PM
Something like this?
d

damp-school-17708

07/20/2021, 7:57 AM
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

elegant-crayon-4967

07/21/2021, 6:18 PM
p

prehistoric-london-9917

07/22/2021, 11:47 PM
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.