What is the equivalent of this python in typescrip...
# typescript
b
What is the equivalent of this python in typescript? I mean, in typescript, you use JSON. stringify, but is there any other method to do this?
g
Isn't
assume_role_policy=pulumi.Output.json_dumps()
good enough?
Output.json_dumps()
should be able to unwrap outputs for you. in Typescript
assume_role_policy
can now accept object directly (most of the time) and when it cannot, you can use `pulumi.interpolate`JSON.stringiy(obj)`
pulumi.interpolate
is somewhat python equivalent of
pulumi.Output.format
so in TS it would look like this:
Copy code
assumeRolePolicy={...props..}

// or

assumeRolePolicy=pulumi.interpolate`JSON.stringify({...props...}`
b
what about oidc_provider_url? that is a key
in python I'm using f to concatenate it and create the key
does output.json_dumps() exist in typescript?
g
this is exactly it
Copy code
pulumi.interpolate`JSON.stringify({...props...}`
b
so, how I know when i need to use interpolate or I can writ the props directly?
e
That f'' string needs the apply. But I'd write it as the following for python:
Copy code
assume_role_policy=pulumi.Output.json_dumps(
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "sts:AssumeRoleWithWebIdentity",
                "Principal": {"Federated": eks_cluster.core.oidc_provider.arn},
                "Condition": {
                    "StringEquals": {
                        pulumi.Output.format("{0}:aud", eks_cluster.core.oidc_provider.url): ["<http://sts.amazonaws.com|sts.amazonaws.com>"]
                    }
                },
            }
        ],
    }
)
I'm not sure typescript supports Output keys, so you probably need the apply for TS
Copy code
assume_role_policy=eks_cluster.core.oidc_provider.url.apply(url => 
    pulumi.jsonStringify(
        {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Action": "sts:AssumeRoleWithWebIdentity",
                    "Principal": {"Federated": eks_cluster.core.oidc_provider.arn},
                    "Condition": {
                        "StringEquals": {
                            [url + ":aud"]: ["<http://sts.amazonaws.com|sts.amazonaws.com>"]
                        }
                    },
                }
            ],
        }
    )
)
b
no no, I mean, the code in Python is working, I'm trying to translate it to typescript
g
just try it and run
pulumi preview
you'll see the policy result it depends on the what type is accepted by the input for example
aws.iamRole
accepts
Copy code
assumeRolePolicy: pulumi.Input<string | aws.iam.PolicyDocument
I'd do this in TS
Copy code
{
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Action": "sts:AssumeRoleWithWebIdentity",
                        "Principal": {"Federated": eks_cluster.core.oidc_provider.arn},
                        "Condition": {
                            "StringEquals": {pulumi.interpolate`${eks_cluster.core.oidc_provider.url}:aud`: ["<http://sts.amazonaws.com|sts.amazonaws.com>"]}
                        },
                    }
                ],
            }
b
I understand that this means that I need to use interpolate, right?
g
this means that you are referencing something which does not exist
b
and this? 😂 does this mean that I need interpolate?
e
no again that's because it might be undefined and that property doesn't expect undefined
b
but that property exists
e
That
.?
can cause the whole expression to be
undefined
b
why that property is marked with that
?
?
e
Because it's optional and might be undefined
If your sure it's not you can tell tsc to ignore that by putting a
!
at the end of the expression
b
yes, It will not be empty because I set to true the
createOidcProvider
in the cluster
perfect it works that part with
!
, now lets go with interpolate... 🤔
the issue with pulumi.interpolate is that I need to use it in a dict key and it is not accepted I think, dict keys can only be strings 😕
e
yeh I want to play with that at some point, but I think TS needs to do the apply here so you can have a string key
b
then I should put the entire policy inside of an apply like in python right?
e
Yup, although python does support Output keys in dicts so you could not use an apply there! Probably best to just try to keep the two as similar as possible though
b
Sorry for not following you at all... what I have now is this and I'm trying to set the oidcProviderUrl fat line 13 key, it should be a variable or a concatenated string, what do you mean by keeping the two as similar as possible?
e
Use an apply, just like you did in Python
b
ok, let me try, but do you mean that this is also the common way to do it in typescript? I don't need to have similar codes between python and typescript, I want to know the ts way
e
I think in typescript you have to use an apply In python you can use an apply OR an output as a key Given you're working in both langauges I'd just do the apply in both
b
Nice, this is working, any suggestion to simplify it or it is already ok?
e
looks fine to me
b
I mean, I'm directly using output in the line 10 and using the apply method in line 13, is there any way to homogenize this? Myabe using .all?
e
you could but it's not needed
If it's bothering you I think this would also work:
Copy code
const eksAutoscalerRole = new aws.iam.Role("AmazonEKSClusterAutoscalerRole", {
    name: "AmazonEKSClusterAutoscalerRole",
    assumeRolePolicy: pulumi.jsonStringify({
        Version: "2012-10-17",
        Statement: [
          {
            Effect: "Allow",
            Action: "sts:AssumeRoleWithWebIdentity",
            Principal: { Federated: eksCluster.core.oidcProvider?.arn! },
            Condition: {
              StringEquals: eksCluster.core.oidcProvider!.url.apply(oidcProviderUrl => {
                return { [oidcProviderUrl + ":aud"]: ["<http://sts.amazonaws.com|sts.amazonaws.com>"] }
              }),
            },
          },
        ],
      }),
  });
b
just trying to have the code as clean as possible 😅 , let me try
Working like a charm! Also removed
pulumi.jsonStringify
thank you very much, now I understand better typescript and pulumi!! 😊😊😊😊😊