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

gefragt vor einem Jahr276 Aufrufe
3 Antworten
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
EXPERTE
beantwortet vor einem Jahr
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
EXPERTE
beantwortet vor einem Jahr
0

Thanks for your response !!

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

beantwortet vor einem Jahr
  • 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

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen