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

salmon-account-74572

08/13/2019, 3:37 PM
Question: how would I go about incorporating a variable's value into the name of a tag to be applied to an AWS resource? For example, the AWS cloud provider for Kubernetes requires a tag named
<http://kubernetes.io/cluster/<clustername>|kubernetes.io/cluster/<clustername>>
(the value is immaterial). I'd like to be able to define the
<clustername>
value once in a variable, and then include it in the tags to be applied to resources. Any suggestions?
g

gentle-diamond-70147

08/13/2019, 3:48 PM
You can specify a variable as a tag name with brackets.
Copy code
const tagName = "my-tag";
const webServer = new aws.ec2.Instance("web-server", {
    tags: {
        [tagName]: "my-value",
    },
    ...
});
s

salmon-account-74572

08/13/2019, 4:14 PM
I'll try that; thanks!
q

quaint-artist-58613

11/28/2019, 11:44 PM
@salmon-account-74572 does this work? thanks.
s

salmon-account-74572

12/02/2019, 3:23 PM
Yes, this definitely works!
c

cuddly-flower-91328

02/17/2023, 7:20 PM
@salmon-account-74572 what was your resolution? I'm dealing with this same issue and apparently I can't construct the string correctly.
s

salmon-account-74572

02/17/2023, 7:22 PM
Hmmm…that was quite a while ago, and I switched from TypeScript to Go in the meantime. 🙂 Let me see if I can find any of my legacy code. One sec… runs off to search his files
c

cuddly-flower-91328

02/17/2023, 7:22 PM
haha thanks @salmon-account-74572
s

salmon-account-74572

02/17/2023, 7:24 PM
OK, found some code that I think may help illustrate. Hopefully this still works!
Copy code
// Set some default values for later
let k8sClusterTag: string = "<http://kubernetes.io/cluster/agtest|kubernetes.io/cluster/agtest>";
let k8sRoleTag: string = "<http://kubernetes.io/role|kubernetes.io/role>";

// Create new VPC
const vpc = new aws.ec2.Vpc("agtest-vpc", {
    cidrBlock: "10.1.0.0/16",
    enableDnsHostnames: true,
    enableDnsSupport: true,
    tags: {
        Name: "agtest-vpc",
        [k8sClusterTag]: "unmanaged",
        [k8sRoleTag]: "common",
    },
});
c

cuddly-flower-91328

02/17/2023, 7:33 PM
Ah I was hoping you were doing some string interpolation with that cluster name
Thanks for replying @salmon-account-74572
Copy code
let clusterName = cluster.core.cluster.name;
let clusterTag = pulumi.interpolate `<http://k8s.io/cluster-autoscaler/${clusterName}`;|k8s.io/cluster-autoscaler/${clusterName}`;>
This is what I was trying to accomplish in some fashion
s

salmon-account-74572

02/17/2023, 7:50 PM
Oh! I don’t know if I tried that in TypeScript. In Go, it looks something like this:
Copy code
// Create a map of regions and friendly names
regionNames := make(map[string]string)
regionNames["us-west-2"] = "oregon"
regionNames["us-east-2"] = "ohio"
regionNames["eu-west-1"] = "ireland"
regionNames["ap-northeast-1"] = "tokyo"
regionNames["ca-central-1"] = "central"

// Define some values to be used later
k8sTag := fmt.Sprintf("<http://kubernetes.io/cluster/%s-mgmt|kubernetes.io/cluster/%s-mgmt>", regionNames[awsRegion])
I’m not sure of the equivalent TypeScript syntax, but it should definitely be possible.
c

cuddly-flower-91328

02/17/2023, 9:34 PM
I ended up going this route:
Copy code
let clusterName = cluster.core.cluster.name;
let clusterTag = pulumi.interpolate `<http://k8s.io/cluster-autoscaler/${clusterName}`;|k8s.io/cluster-autoscaler/${clusterName}`;>
let tags = {
      "<http://k8s.io/cluster-autoscaler/enabled|k8s.io/cluster-autoscaler/enabled>": "true",
}
clusterTag.apply(s => tags[s] = "owned");
s

salmon-account-74572

02/17/2023, 9:38 PM
For incorporating an Output (which I didn’t do), yeah, I think that’s the route you probably need to go.
c

cuddly-flower-91328

02/17/2023, 9:41 PM
Yeah I spent a bit of time banging my head on that one. I'm a TS newb. Thankfully one of my team mates came through with that recommendation.
s

salmon-account-74572

02/17/2023, 9:43 PM
Using the Output adds a certain level of complexity, TBH.
c

cuddly-flower-91328

02/17/2023, 9:45 PM
Is there a better option than what I chose by chance?
s

salmon-account-74572

02/17/2023, 11:14 PM
I’m not aware of one, but I’m also not a TypeScript expert.
2 Views