模板验证错误:模板资源属性无效

时间:2020-06-04 20:42:06

标签: amazon-web-services amazon-cloudformation troposphere

我正在验证通过对流层脚本生成的CloudFormation模板。可能引起错误的问题资源是度量转换,在对流层脚本中定义如下:

t.add_resource(logs.MetricTransformation(
    "planReconciliationFiduciaryStepMetricTransformation",
    MetricNamespace=Ref("metricNamespace"),
    MetricName=Join("", [Ref("springProfile"), "-", "plan-reconciliation-step-known-to-fiduciary"]),
    MetricValue="1"
))

它所依赖的参数预先在脚本中定义如下:

t.add_parameter(Parameter(
    "metricNamespace",
    Type="String",
    Default="BATCH-ERRORS",
    Description="Metric namespace for CloudWatch filters"
))

t.add_parameter(Parameter(
    "springProfile",
    Type="String",
    Default=" ",
    Description="SPRING PROFILE"
))

我正在运行的确切命令是

aws cloudformation validate-template --template-body 
   file://hor-ubshobackgroundtaskdefinition.template --profile saml

及其结果输出

An error occurred (ValidationError) when calling the ValidateTemplate operation: 
Invalid template resource property 'MetricName'

我对MetricTransformation的属性似乎是从AWS documentation定义的。为了提高可视性,这也是正在验证的模板中的指标转换资源也是如此:

"planReconciliationFiduciaryStepMetricTransformation": {
            "MetricName": {
                "Fn::Join": [
                    "",
                    [
                        {
                            "Ref": "springProfile"
                        },
                        "-",
                        "plan-reconciliation-step-known-to-fiduciary"
                    ]
                ]
            },
            "MetricNamespace": {
                "Ref": "metricNamespace"
            },
            "MetricValue": "1"
        }

有什么想法吗?

更新:

根据要求,添加指标过滤器资源:

"PlanReconciliationFiduciaryStepMetricFilter": {
            "Properties": {
                "FilterPattern": "INFO generatePlanReconciliationStepKnownToFiduciary",
                "LogGroupName": {
                    "Ref": "logGroupName"
                },
                "MetricTransformations": [
                    {
                        "Ref": "planReconciliationFiduciaryStepMetricTransformation"
                    }
                ]
            },
            "Type": "AWS::Logs::MetricFilter"
        }

1 个答案:

答案 0 :(得分:1)

事实证明,为了从对流层脚本生成正确的CloudFormation模板,需要在MetricFilter自身内部初始化MetricTransformation资源。如果您将MetricTransformation声明为单独的资源,并尝试在MetricFilter资源中引用它,则随后的模板将被错误地格式化。

对流层脚本中的正确格式如下所示:

t.add_resource(logs.MetricFilter(
    "PlanReconciliationFiduciaryStepMetricFilter",
    FilterPattern="INFO generatePlanReconciliationStepKnownToFiduciary",
    LogGroupName=Ref("logGroupName"),
    MetricTransformations=[logs.MetricTransformation(
        "planReconciliationFiduciaryStepMetricTransformation",
        MetricNamespace=Ref("metricNamespace"),
        MetricName=Join("", [Ref("springProfile"), "-", "plan-reconciliation-fiduciary-step"]),
        MetricValue="1")]
))
相关问题