lively-needle-84406
11/28/2022, 4:34 PMpulumi.interpolate
inside an iam PolicyDocument.
Here is my code:
const stringEqualsKey = pulumi.interpolate`aws:ResourceTag/k8s.io/cluster-autoscaler/${cluster.core.cluster.name}`;
export const clusterAutoScalingPolicy = new aws.iam.Policy("clusterAutoScalingPolicy", {
policy: {
Version: "2012-10-17",
Statement: [
{
Sid: "VisualEditor0",
Effect: "Allow",
Action: [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
Resource: "*",
Condition: {
StringEquals: {
[`${stringEqualsKey}`]: "owned"
}
}
},
{
Sid: "VisualEditor1",
Effect: "Allow",
Action: [
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeAutoScalingGroups",
"ec2:DescribeLaunchTemplateVersions",
"autoscaling:DescribeTags",
"autoscaling:DescribeLaunchConfigurations"
],
Resource: "*"
}
]
}
}, {
dependsOn: [cluster]
});
Here is the error I am receiving:
Calling [toString] on an [Output\u003cT\u003e] is not supported.\n\nTo get the value of an Output\u003cT\u003e as an Output\u003cstring\u003e consider either:\n1: o.apply(v =\u003e `prefix${v}suffix`)\n2: pulumi.interpolate `prefix${v}suffix`\n\nSee <https://pulumi.io/help/outputs> for more details.\nThis function may throw in a future version of @pulumi/pulumi.
I have tried apply
and pulumi.interpolate
to resolve this issue, but neither seem to work.
Am I missing something about outputs?
Thanksbillowy-army-68599
11/28/2022, 4:42 PMapply
- you need to invoke apply and interpolate in the correct place, there are examples here https://github.com/jaxxstorm/pulumi-examples/blob/main/typescript/aws/s3-cloudfront/index.ts#L50-L66lively-needle-84406
11/28/2022, 4:57 PMapply
example.
export const clusterAutoScalingPolicy = new aws.iam.Policy("clusterAutoScalingPolicy", {
policy: pulumi
.all([cluster.core.cluster.name])
.apply(([clusterName]) => {
JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "VisualEditor0",
Effect: "Allow",
Action: [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
Resource: "*",
Condition: {
StringEquals: {
[clusterName]: "owned"
}
}
},
{
Sid: "VisualEditor1",
Effect: "Allow",
Action: [
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeAutoScalingGroups",
"ec2:DescribeLaunchTemplateVersions",
"autoscaling:DescribeTags",
"autoscaling:DescribeLaunchConfigurations"
],
Resource: "*"
}
]
})
})
}, {
dependsOn: [cluster]
});
I like this method, but the issue is that the policy argument for aws.iam.Policy
needs to be of type string | PolicyDocument
, But, the response of pulumi.all.apply
is of type OutputInstance<void>
. Any ideas of how to resolve the type conflict?billowy-army-68599
11/28/2022, 5:00 PMlively-needle-84406
11/28/2022, 6:00 PMerror: aws:iam/policy:Policy resource 'clusterAutoScalingPolicy' has a problem: Missing required argument: The argument "policy" is required, but no definition was found.. Examine values at 'Policy.Policy'.
Code:
export const clusterAutoScalingPolicy = new aws.iam.Policy("clusterAutoScalingPolicy", {
policy: pulumi
.all([cluster.core.cluster.name])
.apply(([clusterName]) => {
JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "VisualEditor0",
Effect: "Allow",
Action: [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
Resource: "*",
Condition: {
StringEquals: {
[`aws:ResourceTag/k8s.io/cluster-autoscaler/${clusterName}`]: "owned"
}
}
},
{
Sid: "VisualEditor1",
Effect: "Allow",
Action: [
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeAutoScalingGroups",
"ec2:DescribeLaunchTemplateVersions",
"autoscaling:DescribeTags",
"autoscaling:DescribeLaunchConfigurations"
],
Resource: "*"
}
]
})
}) as unknown as aws.iam.PolicyDocument
}, {
dependsOn: [cluster]
});
policy
value to unknown first because of this error:
Conversion of type 'OutputInstance<void>' to type 'PolicyDocument' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
billowy-army-68599
11/29/2022, 7:59 PMlively-needle-84406
12/05/2022, 4:12 PMbillowy-army-68599
12/06/2022, 5:27 PMconst policyDoc = cluster.core.cluster.name.apply((name) =>
JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "VisualEditor0",
Effect: "Allow",
Action: [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
],
Resource: "*",
Condition: {
StringEquals: {
[`aws:ResourceTag/k8s.io/cluster-autoscaler/${name}`]: "owned",
},
},
},
{
Sid: "VisualEditor1",
Effect: "Allow",
Action: [
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeAutoScalingGroups",
"ec2:DescribeLaunchTemplateVersions",
"autoscaling:DescribeTags",
"autoscaling:DescribeLaunchConfigurations",
],
Resource: "*",
},
],
})
);
export const clusterAutoScalingPolicy = new aws.iam.Policy(
"clusterAutoScalingPolicy",
{
policy: policyDoc,
}, {
dependsOn: [cluster],
}
);