SNS 主题未收到 CodeBuild 通知

如何解决SNS 主题未收到 CodeBuild 通知

尝试让 CodeBuild 通过 CloudWatch Events 规则将通知推送到 SNS 主题(绑定到 Lambda)。

Cloudformation 模板(见下文)部署良好。

CodeBuild 过程运行良好(已测试)。

SNS 主题和绑定的 Lambda 工作正常 - 我可以通过 AWS CLI 向主题推送消息,并查看 Lambda 将该消息转储到 Cloudwatch 日志。

Cloudwatch 事件规则似乎配置得很好——我在cosole中可以看到它,看起来格式很好,似乎绑定了SNS主题

此外,我一直小心地赋予事件规则一个具有 sns:Publish 权限的角色,并且还为 SNS 主题定义了一个 AWS::SNS::TopicPolicy -

Unable to successfully set up SNS on CodeBuild project through CFT but works manually

但仍然没有 - CodeBuild 成功完成,但我没有收到任何通知

任何关于可能出错的想法?​​

TIA :)

---
AWstemplateFormatVersion: '2010-09-09'
Parameters:
  AppName:
    Type: String
  StagingBucket:
    Type: String
  RepoOwner:
    Type: String
  RepoName:
    Type: String
  RepoBranch:
    Type: String
  RepoPAT:
    Type: String
  CodeBuildBuildSpec:
    Type: String
  CodeBuildType:
    Type: String
    Default: LINUX_CONTAINER
  CodeBuildComputeType:
    Type: String
    Default: BUILD_GENERAL1_SMALL
  CodeBuildImage:
    Type: String
    Default: aws/codebuild/standard:4.0
  LambdaHandler:
    Type: String
    Default: "index.handler"
  Lambdamemory:
    Type: Number
    Default: 128
  LambdaTimeout:
    Type: Number
    Default: 30
  LambdaRuntime:
    Type: String
    Default: python3.8
Resources:
  CodeBuildProject:
    DependsOn:
      - CodeBuildSourceCredential
    Properties:
      Environment:
        ComputeType:
          Ref: CodeBuildComputeType
        Image:
          Ref: CodeBuildImage
        Type:
          Ref: CodeBuildType
      Name:
        Ref: AppName
      ServiceRole:
        Fn::GetAtt:
          - Codebuildrole
          - Arn
      Source:
        Location:
          Fn::Sub:
            - "https://github.com/${repo_owner}/${repo_name}.git"
            - repo_owner:
                Ref: RepoOwner
              repo_name:
                Ref: RepoName
        Type: GITHUB
        BuildSpec:
          Fn::Sub:
            - "${build_spec}"
            - build_spec:
                Ref: CodeBuildBuildSpec
      Artifacts:
        Type: S3
        Location:
          Ref: StagingBucket
      SourceVersion:
        Ref: RepoBranch
      Triggers:
        Webhook: true
        FilterGroups:
          - - Type: EVENT
              Pattern: PUSH
              ExcludeMatchedPattern: false
            - Type: HEAD_REF
              Pattern: "refs/tags/.*"
              ExcludeMatchedPattern: false
    Type: AWS::CodeBuild::Project
  CodeBuildSourceCredential:
    Type: AWS::CodeBuild::SourceCredential
    Properties:
      Token:
        Ref: RepoPAT
      ServerType: GITHUB
      AuthType: PERSONAL_ACCESS_TOKEN
  Codebuildrole:
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: codebuild.amazonaws.com
        Version: '2012-10-17'
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AdministratorAccess
      Path: /
    Type: AWS::IAM::Role
  CodeBuildNotificationFunction:
    Properties:
      Code:
        ZipFile: "def handler(event,context):\n  print (event)"
      Handler:
        Ref: LambdaHandler
      MemorySize:
        Ref: Lambdamemory
      Role:
        Fn::GetAtt:
          - CodeBuildNotificationFunctionRole
          - Arn
      Runtime:
        Ref: LambdaRuntime
      Timeout:
        Ref: LambdaTimeout
    Type: AWS::Lambda::Function
  CodeBuildNotificationFunctionRole:
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
        Version: '2012-10-17'
      Policies:
        - PolicyDocument:
            Statement:
              - Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Effect: Allow
                Resource: '*'
            Version: '2012-10-17'
          PolicyName: code-build-notification-role-policy
    Type: AWS::IAM::Role
  CodeBuildNotificationTopic:
    Properties:
      Subscription:
        - Protocol: lambda
          Endpoint:
            Fn::GetAtt:
              - CodeBuildNotificationFunction
              - Arn
    Type: AWS::SNS::Topic
  CodeBuildNotificationTopicPolicy:
    Properties:
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: "events.amazonaws.com"
            Action:
              - "sns:Publish"
            Resource:
              Ref: CodeBuildNotificationTopic
      Topics:
        - Ref: CodeBuildNotificationTopic
    Type: AWS::SNS::TopicPolicy
  CodeBuildNotificationLambdaInvokePermission:
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName:
        Ref: CodeBuildNotificationFunction
      Principal: "sns.amazonaws.com"
      SourceArn:
        Ref: CodeBuildNotificationTopic
    Type: AWS::Lambda::Permission
  SampleNotificationRule:
    Type: AWS::Events::Rule
    Properties:
      EventPattern:
        Fn::Sub:
          - '{"source": ["aws.codebuild"],"detail-type": ["Codebuild Build Phase Change"],"detail": {"completed-phase": ["SUBMITTED","PROVISIONING","DOWNLOAD_SOURCE","INSTALL","PRE_BUILD","BUILD","POST_BUILD","UPLOAD_ARTIFACTS","FINALIZING"],"completed-phase-status": ["TIMED_OUT","STOPPED","Failed","SUCCEEDED","FAULT","CLIENT_ERROR"],"project-name": ["${project_name}"]}}'
          - project_name:
              Ref: CodeBuildProject
      State: ENABLED
      RoleArn:
        Fn::GetAtt:
          - SampleNotificationRuleRole
          - Arn
      Targets:
        - Arn:
            Ref: CodeBuildNotificationTopic
          Id: sample-notification
  SampleNotificationRuleRole:
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: events.amazonaws.com
        Version: '2012-10-17'
      Policies:
        - PolicyDocument:
            Statement:
              - Action:
                  - "sns:Publish"
                Effect: Allow
                Resource: '*'
            Version: '2012-10-17'
          PolicyName: sample-notification-rule-role-policy
    Type: AWS::IAM::Role

解决方法

已修复 - AWS::Events::Rule 目标缺少 InputTransformer 块,就像这样

      Targets:
        - Arn:
            Ref: CodeBuildNotificationTopic
          Id: sample-notification
          InputTransformer:
            InputPathsMap:
              build-id: "$.detail.build-id"
              project-name: "$.detail.project-name"
              completed-phase: "$.detail.completed-phase"
              completed-phase-status: "$.detail.completed-phase-status"
            InputTemplate: |
              "Build '<build-id>' for build project '<project-name>' has completed the build phase of '<completed-phase>' with a status of '<completed-phase-status>'."

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?