错误代码:AmazonSNS中的AuthorizationError

时间:2017-10-04 11:03:43

标签: amazon-web-services serverless-architecture

我试图创建一个SNS主题并从lambda发布消息。但是在尝试这样做时我获得了授权错误。

Service: AmazonSNS; Status Code: 403; Error Code: AuthorizationError

完全例外

com.amazonaws.services.sns.model.AuthorizationErrorException: User: arn:aws:sts::166916908689:assumed-role/AWSLambdaVPCAccessExecutionRole/lambda-event-common-test is not authorized to perform: SNS:Publish on resource: arn:aws:sns:eu-west-1:166916908689:events (Service: AmazonSNS; Status Code: 403; Error Code: AuthorizationError; Request ID: 9266e536-baa4-55d1-b277-b766f5536b70)

我的山姆模板看起来像这样

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  EventListenFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: event.lambda.EventHandler::handleRequest
      Role: !Sub arn:aws:iam::${AWS::AccountId}:role/AWSLambdaVPCAccessExecutionRole
      FunctionName: lambda-event-$ENVNAME
      Runtime: java8
      VpcConfig:
        SecurityGroupIds:
          - !ImportValue LambdaVPCSecurityGroup
        SubnetIds:
          - !ImportValue VsolPublicSubnetAz1
          - !ImportValue VsolPublicSubnetAz2
      Environment:
        Variables:
          SNS_TOPIC_ARN: !Ref Topic
      Events:
        GetResource:
          Type: Api
          Properties:
            Path: /event/{Id}
            Method: post
      Policies:
        Statement:
          - Effect: Allow
            Action: sns:Publish
            Resource: !Ref Topic
  Topic:
      Type: "AWS::SNS::Topic"
      Properties:
        DisplayName: "events"
        TopicName: "events"  

发送sns通知

private AmazonSNSClient snsClient =(AmazonSNSClient)AmazonSNSClient.builder().build();
 snsClient.publish(new PublishRequest(System.getenv(“SNS_TOPIC_ARN
”),”Test”));

可以允许任何用户使用控制台发布sns主题。我正在寻找使用山姆模板的方法。

由于

1 个答案:

答案 0 :(得分:0)

从这个清单中可以看出

http://docs.aws.amazon.com/IAM/latest/UserGuide/list_sns.html

SNS IAM许可可用的选项多于&s 34:发布"

你没有显示你的lambda代码,但我猜你需要" sns:CreateTopic"

如果这不起作用,那么允许" sns:*"然后查看它在Cloudtrail中调用的内容,然后将权限降低到所需的最低要求

更新:我没有使用SAM模板格式,因此我检查了文档。没有一个例子可以用来声明新的策略内联,但是有使用现有的IAM策略。

所以你说的是

  Policies:
    Statement:
      - Effect: Allow
        Action: sns:Publish
        Resource: !Ref Topic

 Policies: AmazonSNSFullAccess