Question: how would I go about incorporating a var...
# typescript
s
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
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
I'll try that; thanks!
q
@salmon-account-74572 does this work? thanks.
s
Yes, this definitely works!
c
@salmon-account-74572 what was your resolution? I'm dealing with this same issue and apparently I can't construct the string correctly.
s
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
haha thanks @salmon-account-74572
s
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
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
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
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
For incorporating an Output (which I didn’t do), yeah, I think that’s the route you probably need to go.
c
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
Using the Output adds a certain level of complexity, TBH.
c
Is there a better option than what I chose by chance?
s
I’m not aware of one, but I’m also not a TypeScript expert.