2017-12-24 28 views
0

ラムダ関数とSNSトピックを作成するためのcloudformationテンプレートがあります。ラムダ関数は何らかの処理を行い、結果をSNSトピックにパブリッシュします。AWS LambdaにすべてのSNSトピックの一覧を表示する

SNSトピックのARNを取得するには、boto3.client('sns').list_topics()関数を使用して、テンプレートに設定したSNSトピック名を検索しています。

しかしlist_topics() APIは私に次のエラー与えて呼び出す:私はcloudformationテンプレートYAMLファイルで私のラムダリソースにListTopics権限を追加することができますどのように

An error occurred (AuthorizationError) when calling the ListTopics operation: User: arn:aws:sts::136732452473:assumed-role/test/severless-btc-update-PriceUpdateFunction-B38KNZMCBGB is not authorized to perform: SNS:ListTopics on resource: arn:aws:sns:eu-west-1:136732452473:*

を?

これは私のcloudformation.yamlファイルです:あなたはラムダの実行の役割を定義し、関数に適切な権限を割り当てる必要が

AWSTemplateFormatVersion: "2010-09-09" 
Transform: AWS::Serverless-2016-10-31 
Description: Bitcoin daily update 


Parameters: 
    PhoneNumber: 
    Type: String 
    Description: The phone number recipient of the update, in E.164 (e.g. +919876123456) format. 
    UTCHour: 
    Type: String 
    Default: 3 
    Description: The hour at which to send the update, in the UTC time zone. 

Resources: 
    PriceUpdateFunction: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: main.lambda_handler 
     Runtime: python3.6 
     Timeout: 5 
     CodeUri: main.py 
     Environment: 
     Variables: 
      PHONE_NUMBER: !Ref PhoneNumber 
     Events: 
     ScheduledEvent: 
      Type: Schedule 
      Properties: 
      Schedule: !Join [' ', ['cron(0', !Ref UTCHour, '* * ? *)']] 
     Policies: 
     - SNSPublishMessagePolicy: 
      TopicName: !GetAtt SNSTopic.TopicName 
    SNSTopic: 
    Type: "AWS::SNS::Topic" 
    Properties: 
     TopicName: "sendSMS" 
     DisplayName: "BitcoinPriceTopic" 
     Subscription: 
     - 
      Endpoint: !Ref PhoneNumber 
      Protocol: "sms" 

答えて

3

。必要に応じて

LambdaExecutionRole: 
    Type: AWS::IAM::Role 
    Properties: 
     AssumeRolePolicyDocument: 
     Version: '2012-10-17' 
     Statement: 
     - Effect: Allow 
      Principal: {Service: [lambda.amazonaws.com]} 
      Action: ['sts:AssumeRole'] 
     Path:/
     ManagedPolicyArns: 
     - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 
     - arn:aws:iam::aws:policy/service-role/AWSLambdaRole 
     Policies: 
     - PolicyName: SNSPolicy 
     PolicyDocument: 
      Version: '2012-10-17' 
      Statement: 
      - Effect: Allow 
       Action: 
       - "SNS:ListTopic" 
       Resource: ['*'] 

Actionセクションに許可を微調整:次に、あなたのテンプレートで参照役割を作成AWS::Serverless::Function

Role: !GetAtt LambdaExecutionRole.Arn

Role性質があるはずです。

関連する問題