How can I pass a variable to the tags parameter in CloudFormationCreateOrUpdateStackTask@1?

Zareen 0 Reputation points
2024-03-14T22:47:01.1466667+00:00

I am using the CloudFormationCreateOrUpdateStackTask@1 and I can't seem to pass a variable or parameter to the tags parameter. I added a top-level variable in the calling YAML template to hold the tags like so:

variables:  
  tags: |
    {
      "AppName": "$(Build.Repository.Name)",
      "BuildId": "$(Build.BuildNumber)"
    }

And in the called/nested YAML template, I'm trying to use it like so:

...
- task: CloudFormationCreateOrUpdateStack@1
  displayName: 'Cloudformation'
  inputs:
  regionName: ${{ parameters.aws_region }}
  stackName: '$(Build.Repository.Name)-task-definition'
  templateSource: 'file'
  templateFile: './template.yaml'
  templateParametersSource: 'inline'
  templateParameters: '{
        "ParameterKey": "Name",
        "ParameterValue": "$(Build.Repository.Name)"
    }'
  tags: $(tags)

However, the tags value is blank and doesn't seem to work even when I pass it as a parameter. The only way that seems to work is to hardcode the tags like so:

tags: |
  AppName=$(Build.Repository.Name)      
  BuildId=$(Build.BuildNumber)

How can I pass either an input parameter or a global variable to the tags parameter?

Community Center Not monitored
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-03-15T08:51:24.79+00:00

    Hello @Zareen

    Happy to help.

    To pass the tags variable from your YAML pipeline to the CloudFormationCreateOrUpdateStack@1 task, you need to make a few adjustments. Please make the following changes:

    Template Parameters Source:

    First, ensure that you set the templateParametersSource to 'inline'. This allows you to directly specify the template parameters inline within your YAML.

    Remove the trailing slash (/) in the templateParameters section. Passing the Tags:

    Instead of using $(tags), you should reference the tags variable directly within the templateParameters section.

    Here’s the modified snippet for your task:

     task: CloudFormationCreateOrUpdateStack@1
      displayName: 'Cloudformation'
      inputs:
        regionName: ${{ parameters.aws_region }}
        stackName: '$(Build.Repository.Name)-task-definition'
        templateSource: 'file'
        templateFile: './template.yaml'
        templateParametersSource: 'inline'
        templateParameters: |
          {
            "ParameterKey": "Name",
            "ParameterValue": "$(Build.Repository.Name)",
            "Tags": "$(tags)"
          }
    

    Make sure to replace Tags with the appropriate key for tags in your CloudFormation template. Now the tags variable should be correctly passed to your CloudFormation stack.


    if this solves your query, please tag this as answered, so it also helps other community readers who may have similar questions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.