API to retrieve tag name of Cloud formation stack

0

Here is an example of creating a tag for a stack.

const tags = [ { Key: 'Environment', Value: 'Development' }, ];

// Create the stack with tags try { const response = await cloudFormation .createStack({ StackName: stackName, TemplateURL: templateUrl, Tags: tags, }) .promise();

When you create a tag for stack level, how do you retrieve the tag from the stack, What is the API? I did not find anything here https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudformation/classes/cloudformation.html

feita há um ano275 visualizações
3 Respostas
4

Hi,

You could use describeStacks api: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_DescribeStacks.html.

Its Stack response object (https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_Stack.html) contains a list of tags.

Hope it is an acceptable answer ;)

profile picture
ESPECIALISTA
respondido há um ano
4

in addition to @alatech

yes ou can use the describeStacks API to retrieve information about your CloudFormation stack, including the tags associated with it

and as an example code

const { CloudFormationClient, DescribeStacksCommand } = require('@aws-sdk/client-cloudformation');

(async () => {
  const stackName = 'your-stack-name';

  // Create a CloudFormation client
  const cloudFormation = new CloudFormationClient({ region: 'us-west-2' });

  try {
    // Call describeStacks to get stack information
    const response = await cloudFormation.send(
      new DescribeStacksCommand({
        StackName: stackName,
      })
    );

    // Extract stack information
    const stack = response.Stacks[0];
    const tags = stack.Tags;

    console.log(`Tags for stack ${stackName}:`, tags);
  } catch (error) {
    console.error('Error retrieving stack tags:', error);
  }
})();

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudformation/classes/describestackscommand.html https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudformation/index.html

profile picture
ESPECIALISTA
respondido há um ano
0

Thanks for your response !!

I assume Tags can be extracted the same way for stacks obtained from ListStacksOutput as well.

respondido há um ano
  • I think so. That api returns a stack summary, which will return stack name among other info. You can then call describeStacks using the name as parameter, same as described above

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas