https://pulumi.com logo
Title
l

lively-controller-39972

02/12/2023, 8:30 AM
import * as aws from "@pulumi/aws";

const cachingOptimizedPolicy = aws.cloudfront.getCachePolicyOutput({
  name: "Managed-CachingOptimized",
});

const distribution = new aws.cloudfront.Distribution("distribution", {
  orderedCacheBehaviors: [
    {
      cachePolicyId: cachingOptimizedPolicy.id,
      ...
    }
  ],
  ...
});
This code has a type error.
cachingOptimizedPolicy.id
is of type
Output<string | undefined>
but
cachePolicyId
expects something of type
string
. I had a similar code with Terraform but didn't have any issue.
e

echoing-dinner-19531

02/13/2023, 11:51 AM
This might be a typing bug in the SDK. I'm not sure if getCachePolicyOutput actually always sets the
id
value in the result, and if it doesn't then it coming back as
string | undefined
is accurate and you should handle that (maybe by just casing to
string
if you know that in your case it does always get set). If it always sets id then we should fix up the type for that. If you just want to cast it you can do
cachingOptimizedPolicy.id.apply(id => id!)
to tell TS to ignore the undefined part. Might be worth raising an issue at https://github.com/pulumi/pulumi-aws about this, the team that work on this will know better than me if
id
is actually optional or not.
l

lively-controller-39972

02/13/2023, 12:14 PM
Thanks @echoing-dinner-19531 for your answer! And in the meantime, I will adopt your solution with
.apply(id => id!)
that seems a good workaround.