Search code examples
amazon-web-servicesazure-devopstfsyamlaws-cloudformation

Pass parameter from azure devops pipeline yaml to cloudformation yaml


I do not manage to pass parameter from my azure yaml to my cloudformation yaml. I got the following error:

##[error]ValidationError: Parameters: [EnvironmentType] must have values

Nothing called "EnvironmentType" is passed through azure yml to cloudformation yml.

azure-pipeline.yaml

pool:
  name: AWS

variables:
  ArtifactName: update-email

stages:
- stage: Build
  displayName: Build Stage
  ...

- stage: DeployToDev
  displayName: 'Deploy To Dev'
  variables:
    EnvironmentType: 'dev'
    AwsCredentials: '${{ variables.EnvironmentType }}-xx'
    AwsRegion: 'eu-central-1'
    CodeBucket: 's3-$(EnvironmentType)-xx'
    BucketPrefix: 'xx-$(ArtifactName)'
    StackName: 'clf-$(EnvironmentType)-xx'
  jobs:
  - job: DeployToDev
    steps:
    ...
    - task: AmazonWebServices.aws-vsts-tools.CloudFormationCreateOrUpdateStack.CloudFormationCreateOrUpdateStack@1
      displayName: 'Create/Update Stack: $(StackName)'
      inputs:
        awsCredentials: ${{ variables.AwsCredentials }}
        regionName: $(AwsRegion)
        stackName: $(StackName)
        templateFile: '$(ArtifactName)/clf-xx-$(EnvironmentType).yml'
        parameters: |
          EnvironmentType=$(EnvironmentType)

clf.yaml

Parameters:
  EnvironmentType:
    Description: The deployment environment type.
    Type: String
    AllowedValues:
      - dev
      - prod

Resources:
  xx:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: !Sub "lbd-${EnvironmentType}-xx"
      ...

What I also tried:

- task: AmazonWebServices.aws-vsts-tools.CloudFormationCreateOrUpdateStack.CloudFormationCreateOrUpdateStack@1
      displayName: 'Create/Update Stack: $(StackName)'
      inputs:
        awsCredentials: ${{ variables.AwsCredentials }}
        regionName: $(AwsRegion)
        stackName: $(StackName)
        templateFile: '$(ArtifactName)/clf-xx-$(EnvironmentType).yml'
        templateParameters: |
          - ParameterKey: EnvironmentType
            ParameterValue: ${{variables.EnvironmentType}}
  • use "parameters"

     - task: AmazonWebServices.aws-vsts-tools.CloudFormationCreateOrUpdateStack.CloudFormationCreateOrUpdateStack@1
       displayName: 'Create/Update Stack: $(StackName)'
       inputs:
         awsCredentials: ${{ variables.AwsCredentials }}
         regionName: $(AwsRegion)
         stackName: $(StackName)
         templateFile: '$(ArtifactName)/clf-xx-$(EnvironmentType).yml'
         parameters: |
           - ParameterKey: EnvironmentType
             ParameterValue: ${{variables.EnvironmentType}}
    
  • Add templateParametersSource: inline and remove '/' in templateParameters got error A sequence was not expected, which point to the "ParameterKey" line

     - task: AmazonWebServices.aws-vsts-tools.CloudFormationCreateOrUpdateStack.CloudFormationCreateOrUpdateStack@1
       displayName: 'Create/Update Stack: $(StackName)'
       inputs:
         awsCredentials: ${{ variables.AwsCredentials }}
         regionName: $(AwsRegion)
         stackName: $(StackName)
         templateFile: '$(ArtifactName)/clf-xx-$(EnvironmentType).yml'
         templateParametersSource: inline
         templateParameters: 
           - ParameterKey: EnvironmentType
             ParameterValue: 'dev'
    

I know that I can use ssm but here I am wondering if I want to stay with the solution using CloudFormationCreateOrUpdateStack task.

Am I missing anything here ? Is it possible to pass parameter from azure yml to cloudformation yml through CloudFormationCreateOrUpdateStack task ?

Thanks in advance


Solution

  • There is no property "parameters" in "AWS CloudFormation Create/Update Stack task".

    If want to pass parameters to your AWS template, you should set "Template Parameters Source" to inline as suggested by erik258. In addition you can also use template parameters file. For example:

    parameters:
      deploymentEnvironment: ''         # development, staging, production, etc
      awsCredentials:        ''         # service connection name
      region:                ''         # the AWS region
    
    steps:
    
    - task: CloudFormationCreateOrUpdateStack@1
      displayName: 'Create/Update Stack: Staging-Deployment'
      inputs:
        awsCredentials: '${{ parameters.awsCredentials }}'
        regionName:     '${{ parameters.region }}'
        stackName:      'my-stack-name'
        useChangeSet:   true
        changeSetName:  'my-stack-name-changeset'
        templateFile:   'my-aws-cloudformation-template.yml'
        templateParametersFile: '${{ parameters.deploymentEnvironment }}/parameters.json'
        captureStackOutputs: asVariables
        captureAsSecuredVars: false
    

    You can see the detailed info from Use Azure DevOps Parameters to Switch Between Your AWS Accounts.


    Update

        templateParametersSource: inline
        templateParameters: |
          - ParameterKey: EnvironmentType
            ParameterValue: '$(EnvironmentType)'
    

    Or

        templateParametersSource: 'inline'
        templateParameters: |
          [
            {
              "ParameterKey":"EnvironmentType",
              "ParameterValue":"$(EnvironmentType)"
            }
          ]