Mark
Mark

Reputation: 5068

Lambda funciton not triggering after s3 upload

I am trying to trigger a Lambda function after uploading a file.

Below I have defined the file Lambda function. Within it, a file is uploaded to s3. I then want the process Lambda function to triger. However, I cannot get it to trigger. Also, in AWS s3, the bucket Properties > Event notifications is empty.

My serverless.yml:

service: backend
frameworkVersion: "2"

package:
  exclude:
    - "frontend/**"

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: true
    dockerFile: ./dockerfile

provider:
  name: aws
  runtime: python2.7
  stage: dev
  lambdaHashingVersion: 20201221
  apiGateway:
    shouldStartNameWithService: true
  environment:
    STORAGE_BUCKET: dev-storage-bucket-01392334
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
        - s3:PutObjectAcl
        - s3:GetObject
        - s3:GetObjectAcl
        - s3:DeleteObject
      Resource: 
        - "arn:aws:dynamodb:us-east-1:*:*"
        - "arn:aws:s3:::*"

functions:
  process:
    handler: handler.process
    events:
      - s3:
        bucket: ${self:provider.environment.STORAGE_BUCKET}
        events: s3:ObjectCreated:*
        existing: true
  file:
    handler: handler.file
    events:
      - http:
          method: POST
          path: /file


resources:
  Resources:
    storage:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:provider.environment.STORAGE_BUCKET}

In my handler I have:

def process(event, context):

    print("in process")


def fcsfile(event, context):
    uploaded_file = event['body']

    s3 = boto3.resource('s3')
    object = s3.Object(STORAGE_BUCKET, 'the_file_name')
    s3_response = object.put(Body=uploaded_file)

    print(s3_response)
    response = {
        "statusCode": 200,
        "body": json.dumps(response)
    }
    return response

When I do a CURL to the endpoint, the file gets uploaded to s3, but the process function is never triggered.

I have tried removing 'existing: true'. Same result. I have tried changing bucket name to something completely different e.g. 'dev-storage-bucket-99999999968686' but I then see the error:

An error occurred: S3BucketDevstoragebucket99999999968686 - dev-storage-bucket-99999999968686 already exists in stack

And no, the bucket does not exist.

What else can I try?

Upvotes: 1

Views: 1118

Answers (1)

Aaron Stuyvenberg
Aaron Stuyvenberg

Reputation: 3777

It appears as though you may have a small typo.

Replacing events with event in the handler.process block seems to work for me:

functions:
  process:
    handler: handler.process
    events:
      - s3:
          bucket: ${self:provider.environment.STORAGE_BUCKET}
          event: s3:ObjectCreated:*
          existing: true

When running the command with events, I receiving a warning from the serverless framework: Serverless: Configuration warning at 'functions.process.events[0]': unsupported function event

Here is the documentation, which helped me find the correct syntax.

Upvotes: 3

Related Questions