amazon-web-services - 如何将 CloudFormation 参数作为类型 : Number? 传递

标签 amazon-web-services aws-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: "exam<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63130f0623040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>"
  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

数字:

An integer or float. AWS CloudFormation validates the parameter value as a number; however, when you use the parameter elsewhere in your template (for example, by using the Ref intrinsic function), the parameter value becomes a string.

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

关于amazon-web-services - 如何将 CloudFormation 参数作为类型 : Number? 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66050456/

相关文章:

amazon-web-services - 从 CloudFormation 更改自管理 Eks 集群节点的实例类型时,eks 上的服务和部署会发生什么情况?

amazon-web-services - 是否有办法确定 CloudFormation 模板实际需要哪些 IAM 权限?

amazon-web-services - 自定义授权者的 AWS API 网关 403 错误

python - 使用 Boto3 嵌套键获取 S3 中文件的完整路径

java - Apache camel 和 SQS 扩展客户端库

aws-cloudformation - 在 CloudFormation 中跨不同区域添加 VPC 对等互连

amazon-web-services - 目标的 AWS::Events::Rule 输入不是有效的 JSON 文本

amazon-web-services - Cognito 用户池在配置为发送电子邮件时尝试发送 SMS

amazon-web-services - 适用于 AWS 的 Docker 与 EC2 上的纯 Docker 部署

json - CloudFormation AWS::EC2::Instance 指定实例数量