AWS CloudFormation:AWS :: Events :: Rule-InputTemplate

时间:2019-07-04 17:33:19

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

我正在构建一个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仪表板中也会发生相同的问题。

我确实与AWS Tach支持人员进行了交谈,他们也不知道如何解决此问题,他们确实提供了一些尝试方法,但没有一种有效。

他们告诉我,他们将要问负责AWS这部分工作的团队,但我寄予的希望不高。

问题

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

3 个答案:

答案 0 :(得分:0)

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

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

对于YAML,应该是这样的:

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

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

确认 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."'