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

질문됨 일 년 전276회 조회
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
전문가
답변함 일 년 전
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
전문가
답변함 일 년 전
0

Thanks for your response !!

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

답변함 일 년 전
  • 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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠