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

已提問 1 年前檢視次數 275 次
3 個答案
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
專家
已回答 1 年前
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
專家
已回答 1 年前
0

Thanks for your response !!

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

已回答 1 年前
  • 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

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南