微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 CloudFormation 参数作为类型传递:数字?

如何解决如何将 CloudFormation 参数作为类型传递:数字?

我正在尝试通过部署以下 AWS CloudFormation 模板(使用 Azure DevOps)在 AWS 账户中创建预算:

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}

但得到以下错误

##[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

我尝试将 TypeNumber 更改为 String,但出现相同的错误。 还尝试通过其他内在函数 !Ref 解析参数,但没有运气。 这是我从 azure-pipelines.yml 文件为 CloudFormation 模板设置参数的方式:

variables:
  email_recipients: "example@gmail.com"
  budget_amount: 100
  amount_threshold: 80

这就是他们如何将它们传递给模板:

            - 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

解决方法

Parameters

Number

整数或浮点数。 AWS CloudFormation 将参数值验证为数字;但是,当您在模板中的其他地方使用该参数时(例如,通过使用 Ref 内部函数),该参数值将变成字符串。

---
AWSTemplateFormatVersion: 2010-09-09
Description: >-
    Deploy an instance of wallaby api to your AWS Organization.
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: !Ref BudgetAmount
          Unit: USD
        TimeUnit: MONTHLY
        BudgetType: COST
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref AmountThreshold
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Ref EmailRecipients

cfn-lint 检查

$ cfn-lint cfn.yml
$ echo $?
0

更新:

随着原始帖子的更新。用户创建了另一个帖子 Pass Azure pipeline variable to AWS Cloudformation template as parameter with type: Number

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。