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

demandé il y a un an276 vues
3 réponses
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
EXPERT
répondu il y a un an
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
EXPERT
répondu il y a un an
0

Thanks for your response !!

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

répondu il y a un an
  • 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

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions