Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

amazon web services - Pass Azure pipeline variable to AWS Cloudformation template as parameter with type: Number

When my azure-pipelines.yml file defines and passes down a variable with an integer-type value to a AWS Cloudformation template as a parameter, the template fails to use that parameter as a Number type. This causes my Cloudformation stack to fail. How can I instruct the Azure pipeline to keep the integer type ? Here is the variable definitions in azure-pipelines.yml (The problematic ones are budget_amount and amount_threshold):

variables:
  email_recipients: "[email protected]"
  budget_amount: 100
  amount_threshold: 80

Then sending them down to a azure-pipelines template deploy_env.yml:

- template: azure-pipelines/stages/deploy_env.yml
  parameters:
    stage_name: deploy
    aws_credentials: $(aws_dev_credentials)
    aws_region: $(aws_region)
    user_initials: $(user_initials)
    email_recipients: $(email_recipients)
    budget_amount: $(budget_amount)
    amount_threshold: $(amount_threshold)

And the passing of them to the Cloudformation template:

- task: CloudFormationCreateOrUpdateStack@1
  displayName: budgets
  inputs:
    awsCredentials: ${{parameters.aws_credentials}}
    regionName: ${{parameters.aws_region}}
    stackName: ${{parameters.stack_name}}
    templateSource: 'file'
    templateFile: $(Pipeline.Workspace)/${{parameters.project_name}}-templates/budgets.yml
    templateParametersSource: "inline"
    templateParameters: |
      - ParameterKey: EmailRecipients
        ParameterValue: ${{parameters.email_recipients}}
      - ParameterKey: BudgetAmount
        ParameterValue: ${{parameters.budget_amount}}
      - ParameterKey: AmountThreshold
        ParameterValue: ${{parameters.amount_threshold}}
    useChangeSet: true
    changeSetName: 'role-changeset'
    captureStackOutputs: asVariables
    captureAsSecuredVars: false

The Cloudformation template budgets.yml:

Parameters:
  EmailRecipients:
    Type: String
    Description: Name of the Email Recipient
  BudgetAmount:
    Type: Number
    Default: 500
    Description: Budget Amount
  AmountThreshold:
    Type: Number
    Default: 80
    Description: Budget Threshold

Resources:
  BudgetExample:
    Type: "AWS::Budgets::Budget"
    Properties:
      Budget:
        BudgetLimit:
          Amount: !Sub ${BudgetAmount}
          Unit: USD
        TimeUnit: MONTHLY
        BudgetType: COST
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Sub ${AmountThreshold}
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Sub ${EmailRecipients}

The error thrown is:

##[error]MultipleValidationErrors: There were 2 validation errors:
* InvalidParameterType: Expected params.Parameters[1].ParameterValue to be a string
* InvalidParameterType: Expected params.Parameters[2].ParameterValue to be a string
question from:https://stackoverflow.com/questions/66061022/pass-azure-pipeline-variable-to-aws-cloudformation-template-as-parameter-with-ty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Azure Devops, the variable is String type by default. So when you use variable to pass the value to parameter, the value is still the string type. This could be the root cause of this issue.

To solve this issue, you need to use the number type Parameters.

Format:

parameters:
- name: myNumber
  type: number
  default: 2

In you case, you could define the Number type Parameters in your Yaml template(deploy_env.yml), and set the Number type Parameters to pass the value in Main Yaml(azure-pipelines.yml).

Here is an example:

parameters:

    - name: budget_amount
      type: number
      default: 100
    
    - name: amount_threshold
      type: number
      default: 80
    
    - name: email_recipients
      type: string
      default: "[email protected]"
    
    
    
    steps: 
    
    - task: CloudFormationCreateOrUpdateStack@1
    ....

Main Yaml(azure-pipelines.yml):

parameters:
  - name: budget_amount
    type: number
    default: 90

  - name: amount_threshold
    type: number
    default: 80

  - name: email_recipients
    type: string
    default: "[email protected]"


pool:
  vmImage: 'ubuntu-latest'

steps:
- template: deploy_env.yml
  parameters:
    budget_amount: ${{parameters.budget_amount}}
    amount_threshold: ${{parameters.amount_threshold}}
    email_recipients: ${{parameters.email_recipients}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...