2017-12-24 2 views
0

람다 함수 및 SNS 주제를 만들기 위해 cloudformation 템플릿이 있습니다. 람다 함수는 일부 처리를 수행하고 결과를 SNS 주제에 게시합니다.AWS 람다에게 모든 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

. 그런 다음 템플릿에서 참조 된 역할을 만들 AWS::Serverless::Function

Role: !GetAtt LambdaExecutionRole.Arn

Role 특성이 있어야한다 : 필요에 따라

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 섹션의 권한을 조정할.

관련 문제