I have some code I found for assigning tag values to resources in AWS via typescript. The problem is, one of the resources (autoScaling Groups) handles tags differently than all other resources. Instead of defining all my tag values twice, I'm trying to figure out a programatic way to do it.
Here is my current tag variable:
const tags = [{
Name: "ServerName"
tContact: "john@doe.com"
tEnv: "DEV"
tOwner: "John doe",
tProject: "Typescript",
}];
If I were to create a 2nd manually for the different tag type, it would be:
const tags2 =
[
{
key: "Name",
propagateAtLaunch: true,
value: "ServerName",
},
{
key: "tContact",
propagateAtLaunch: true,
value: "john@doe.com",
},
{
key: "tEnv",
propagateAtLaunch: true,
value: "DEV",
},
{
key: "tOwner",
propagateAtLaunch: true,
value: "John Doe",
},
{
key: "tProject",
propagateAtLaunch: true,
value: "Typescript",
}
];
I'm new to typescript, but I'm sure there's even nearly a one liner to do this. I was thinking of a for loop and strings, but I'm sure there is some sort of apply
and redirect I'm missing.
You can .map
the object entries Object.entries
like this
Object.entries(tags[0]).map(([key, value]) => (
{
key: key,
value: tags[0][key],
propagateAtLaunch: true
}));
const tags = [{
Name: "ServerName",
tContact: "john@doe.com",
tEnv: "DEV",
tOwner: "John doe",
tProject: "Typescript",
},
{
Name: "ServerName1",
tContact: "john@doe.com",
tEnv: "DEV1",
tOwner: "John doe",
tProject: "Typescript",
}];
//let obj = tags[0];
//console.log(obj)
let results = [];
let result = tags.forEach(c=> results.push(Object.entries(c).map(([key, value]) => ({key: key, value: tags[0][key], propagateAtLaunch: true}))));
console.log(results);