【问题标题】:AWS CloudFormation: AWS::Events::Rule - InputTemplateAWS CloudFormation:AWS::Events::Rule - InputTemplate
【发布时间】:2021-07-22 20:58:37
【问题描述】:

我正在构建一个 AWS CloudFormation 文件,但我被困在 AWS::Events::Rule 服务中。我正在尝试制定一个规则,为 CodePipeline 的每个阶段向开发人员发送一封电子邮件。这是整个文件:

{
    "PipelineWebpageAccessStageNotification": {
        "Type": "AWS::Events::Rule",
        "Properties": {
            "Description": "Send out notifications to developers about the CodePipeline stages status for the websites.",
            "Name": "codepipeline-webpage_access",
            "RoleArn": { "Fn::GetAtt": ["PipelineWebpageAccessStageNotificationRole", "Arn" ] },
            "EventPattern": {
                "source": [
                    "aws.codepipeline"
                ],
                "detail-type": [
                    "CodePipeline Stage Execution State Change"
                ],
                "detail": {
                    "pipeline": [
                        { "Ref": "PipelineWebpageAccess" }
                    ],
                    "state": [
                        "FAILED",
                        "CANCELED",
                        "RESUMED",
                        "SUCCEEDED",
                        "STARTED"
                    ]
                }
            },
            "State": "ENABLED",
            "Targets": [
                {
                    "Arn": { "Ref": "SNSTopic" },
                    "Id": "SNS",
                    "InputTransformer": {
                        "InputPathsMap": {
                            "pipeline": "$.detail.pipeline",
                            "stage": "$.detail.stage",
                            "state": "$.detail.state"
                        },
                        "InputTemplate": "Hello Human, This message is related to the CodePipeline: <pipeline>. Bellow are some details: \r\n\r\n - Stage: <stage>\r\n - Status: <state>\r\n\r\n Thank you for reading, Enjoy your existence."
                    }
                }
            ]
        }
    }
}

问题在于InputTemplate。 CloudFormation 告诉我发送一个字符串。我在做什么......原来你实际上必须发送一个包含 JSON 的字符串。好的,所以我这样做了,就像在其他地方一样,例如,当您在 CloudWatch 中构建仪表板时,您使用 JSON 传递一个字符串。这没有问题。但在这里 - 它一直在失败。

有趣的是,同样的问题也发生在 AWS Dashboard 中。

我确实与 AWS Tach Support 进行了交谈,他们也不知道如何解决这个问题,他们确实提供了一些尝试的方法,但都没有奏效。

他们告诉我,他们将询问负责 AWS 这部分的团队,但我并不抱太大希望。

问题

如果您遇到同样的问题,并且知道如何解决,我们将不胜感激:)

【问题讨论】:

  • 文档对此很奇怪,阅读InputTemplate 下方的链接应该是这样的:"InputTemplate": "\"Hello Human, This message is related to the CodePipeline: &lt;pipeline&gt;. Bellow are some details: \r\n\r\n - Stage: &lt;stage&gt;\r\n - Status: &lt;state&gt;\r\n\r\n Thank you for reading, Enjoy your existence.\""forums.aws.amazon.com/thread.jspa?messageID=798687

标签: amazon-web-services amazon-cloudformation amazon-cloudwatch amazon-ses


【解决方案1】:

它必须是 AWS 内部的有效字符串,不仅在 JSON 模板中。所以InputTemplateJSON CloudFormation 中必须是这样的(注意转义引号):

"InputTemplate": "\"your text here <placeholders>\""

YAML 应该是这样的:

InputTemplate: |
    "Line 1 <placeholders>."
    "Line 2 <placeholders>."

当我从控制台创建它时,它显示如下:

Input Transformer: {"InputPathsMap":{"state":"$.detail.state"},"InputTemplate":"\"bla <state>\""}   

【讨论】:

  • 这是一个不起作用的解决方案。我的意思是它可以工作,但它不适用于换行符。如果您删除新行字符,则电子邮件实际上以 " 开头。
  • 对于 YAML,我必须在每行之间添加一个额外的新行。
【解决方案2】:

对于这个特殊问题,我们最终发送的不是 JSON 对象,而是单个双引号字符串,这也是有效的 JSON。然后它确实允许您在字符串中使用占位符,适合撰写电子邮件正文或主题。

所以我们的电子邮件 lambda 采用以下参数:

"recipient@email.org\nSubject line with <placeholder>\nMessage line 1 with <placeholder>\nMessage line N"

【讨论】:

    【解决方案3】:

    确认 azize 的评论是正确的,虽然 \r\n 对我不起作用,但它们只是出现在通知中。这对我有用。

    JSON:

    "InputTemplate": "\"Hello Human, This message is related to the CodePipeline: <pipeline>. Details:  - Stage: <stage> - Status: <state> Thank you for reading, Enjoy your existence.\""
    

    YAML:

    InputTemplate: '"Hello Human, This message is related to the CodePipeline: <pipeline>. Details:  - Stage: <stage> - Status: <state> Thank you for reading, Enjoy your existence."'
    

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2022-06-14
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多