无法通过CFT在CodeBuild项目中成功设置SNS,但手动操作即可。

问题描述 投票:0回答:1

我正在尝试建立一个SNS系统,以便在构建失败时发送电子邮件。目前我只是为了测试的目的,将其设置为在达到几个不同阶段时发送通知。

我已经按照AWS的教程做了很多次,我已经记住了整个过程,但是当我把它翻译成CFT时,有些东西并没有发挥作用。

当所有的东西都被创建时,我收到了确认邮件,但之后当我在CodeBuild项目中点击 "开始构建 "按钮时,我没有收到任何关于构建状态的进一步邮件。如果我手动操作,我将收到所有预期的邮件。

sns-build-notifications.yml

###
### Stack Name: sns-build-notifications
###
### Template Name: sns-build-notifications.yml
###
### Description: Creates a notification system that emails users when a
###              specified build fails
###

AWSTemplateFormatVersion : '2010-09-09'

Globals:
  Api:
    OpenApiVersion: 3.0.1

Transform: AWS::Serverless-2016-10-31

Resources:
  rSnsTopicDemo:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: rSnsTopicDemo
      Subscription:
        - Protocol: email
          Endpoint: [email protected]

  rCloudWatchEventRuleDemo:
    Type: AWS::Events::Rule
    Properties:
      Description: Delete when finished
      EventPattern:
        source:
          - aws.codebuild
        detail-type:
          - CodeBuild Build State Change
        detail:
          build-status:
            - IN_PROGRESS
            - SUCCEEDED
            - FAILED
            - STOPPED
          project-name:
            - CodeBuildStateChangeDemo
      Targets:
        - Arn: !Ref rSnsTopicDemo
          Id: CodeBuildStateChangeDemo
          InputTransformer:
              InputPathsMap:
                build-status: "$.detail.build-status"
              InputTemplate: |
                "Build has entered status: '<build-status>'"

我附上一些我通过CFT创建的资源的图片。为了安全起见,有些名称可能与我在这里描述的不同。

CodeBuildStateChangeDemoenter image description here

rSnsTopicDemoenter image description here

rSnsTopicDemo订阅enter image description here

CloudWatch事件规则enter image description here

amazon-web-services amazon-cloudformation amazon-sns
1个回答
1
投票

从你发布的信息中,我看到 没有 AWS::SNS::TopicPolicy 允许CW事件向其提交任何信息。

主题策略将需要 在CFN中明确创建. 控制台在后台进行操作。策略示例如下。

{
  "Sid": "AWSEvents_kkk_Id983908485049",
  "Effect": "Allow",
  "Principal": {
    "Service": "events.amazonaws.com"
  },
  "Action": "sns:Publish",
  "Resource": "<my-sns-topic-arn>"
}
© www.soinside.com 2019 - 2024. All rights reserved.