Thilak
Thilak

Reputation: 75

AWS SAM Template: Issue mapping S3 events to Lambda

I've followed the AWS SAM documentation for hands-on learning experience and have reached this particular section: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-s3.html

Above page in the documentation explains how to map the S3 events to any resource and I've done something similar on my local machine. My local template below.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  CreateThumbnail:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.6
      Timeout: 60
      Policies: AWSLambdaExecute
      Events:
        CreateThumbnailEvent:
          Type: S3
          Properties:
            Bucket: !Ref testBucket
            Events: s3:ObjectCreated:*

  testBucket:
    Type: AWS::S3::Bucket

Now, when I build and deploy above SAM template, while a new S3 bucket is getting created, the 'object creation' event is not mapped to the Lambda function created. PSB screenshot.

Screenshot showing that S3 events are not mapped

I haven't found any articles or blogs on this error (probably, I'm doing a silly mistake here)

Please help.

Thanks.

Upvotes: 2

Views: 6083

Answers (3)

Erick Benites
Erick Benites

Reputation: 77

You can add a AWS::Lambda::Permission to your Resources as described in this AWS SAM Github issue

  LambdaInvokePermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      FunctionName: !GetAtt MyFunction.Arn
      Action: 'lambda:InvokeFunction'
      Principal: 's3.amazonaws.com'
      SourceAccount: !Sub ${AWS::AccountId}
      SourceArn: !GetAtt MyBucket.Arn

Upvotes: 2

Thilak
Thilak

Reputation: 75

I realized that for some reason, the AWS Lambda console is not showing the trigger event but the mapping is successful. I've also validated by uploading sample image to the bucket which triggered the lambda.

The event mapping can be seen in the properties section of the S3 bucket created.

S3 test bucket created -> Properties -> Events

S3 bucket properties showing the created events

Upvotes: 4

Richard Rublev
Richard Rublev

Reputation: 8164

First create bucket where you plan to save packaged code

aws s3 mb s3://youtbucketname

After sam build go for

sam package --template-file template.yaml --s3-bucket <yourbucketname>

Upvotes: 0

Related Questions