Lambda with SNS event not triggering | Serverless-offline-sns

0

Hello there,

I am working with serverless-offline, so I run my project with sls offline, all good with that. Here is my serverless.yml:

service: ${env:APP_SERVICE_NAME}
useDotenv: true
configValidationMode: warn

plugins:
  - serverless-plugin-typescript
  - serverless-plugin-optimize
  - serverless-offline-sns
  - serverless-offline-sqs
  - serverless-offline

provider:
  name: aws
  runtime: nodejs18.x
  memorySize: 128
  region: ${env:AWS_REGION}
  stage: ${opt:stage,'dev'}
  environment:
    # APP
    APP_STAGE: ${env:APP_STAGE}
    APP_SERVICE_NAME: ${env:APP_SERVICE_NAME}
    AWS_ACCOUNT_ID: ${aws:accountId}
    DEVELOPMENT_PATH: ${env:DEVELOPMENT_PATH}
    EXCHANGE_RATE_API: ${env:EXCHANGE_RATE_API}
    SNS_TOPIC_ARN: arn:aws:sns:${self:provider.region}:${aws:accountId}:TextractTriggerTopic-${self:provider.stage}
    ROLE_ARN: arn:aws:iam::${aws:accountId}:role/${env:APP_SERVICE_NAME}-${self:provider.stage}-${self:provider.region}-lambdaRole

  iam:
    role:
      statements:
        - Effect: Allow
          Action:
            - sqs:SendMessage
            - sqs:ReceiveMessage
            - sqs:DeleteMessage
            - sqs:GetQueueAttributes
          Resource: !GetAtt MySQSQueue.Arn
        - Effect: Allow
          Action:
            - textract:StartExpenseAnalysis
            - textract:GetExpenseAnalysis
          Resource: "*"
        - Effect: Allow
          Action:
            - s3:GetObject
            - s3:PutObject
          Resource: "*"
        - Effect: Allow
          Action:
            - sns:Publish
            - sns:Subscribe
            - sns:Receive
          Resource: arn:aws:sns:${self:provider.region}:${aws:accountId}:TextractTriggerTopic-${self:provider.stage}
        - Effect: Allow
          Action:
            - lambda:InvokeFunction
          Resource: "*"

functions:
  startExpenseAnalysisJob:
    handler: src/functions/startExpenseAnalysisJob.handler
    timeout: 180
    maximumRetryAttempts: 1
    events:
      - sqs:
          arn: !GetAtt MySQSQueue.Arn
  processExpenseAnalysisJob:
    handler: src/functions/processExpenseAnalysisJob.handler
    timeout: 360
    events:
      - sns:
          arn: arn:aws:sns:${self:provider.region}:${aws:accountId}:TextractTriggerTopic-${self:provider.stage}

resources:
  Resources:
    MySQSQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: TextractTriggerQueue-${self:provider.stage}
        VisibilityTimeout: 360

    TextractTriggerTopic:
      Type: AWS::SNS::Topic
      Properties:
        TopicName: TextractTriggerTopic-${self:provider.stage}

    IamRoleLambdaExecution:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal:
                Service: lambda.amazonaws.com
              Action: sts:AssumeRole
            - Effect: Allow
              Principal:
                Service: textract.amazonaws.com
              Action: sts:AssumeRole
              Condition:
                ArnLike:
                  aws:SourceArn: arn:aws:textract:*:${aws:accountId}:*
                StringEquals:
                  aws:SourceAccount: ${aws:accountId}

custom:
  serverless-offline-sqs:
    autoCreate: true
    apiVersion: "2012-11-05"
    endpoint: http://0.0.0.0:9324
    region: ${self:provider.region}
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false

  serverless-offline-sns:
    port: 4002
    debug: true
    accountId: ${aws:accountId}

The problem seems that for my second lambda function processExpenseAnalysisJob with a sns event, is not triggering, looks like the ARN is not working with serverless-offline-sns and I do not know how to resolve this. I am using the StartExpenseAnalysisCommand from amazon textract sending the NotificationChannel parameter.

This works when deployed, but locally not. This is working with this method https://stackoverflow.com/questions/50188932/invoke-lambda-function-by-sns-message-on-local-serverless-offline-environment, but I can't use that when using amazon textract.

No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions