How to reference a s3 bucket in the lambda trigger

I want to create an s3 bucket, and trigger a lambda function whenever some file is uploaded to ‘uploads’ folder in the bucket. I want to create those resources using serverless framework in aws.

I have defined my s3 bucket configuration in under ‘provider.s3’, and then I am trying to reference that bucket under functions.hello.events.bucket

However, I am getting following error when i run sls package

 Serverless Error ----------------------------------------

     MyS3Bucket - Bucket name must conform to pattern (?!^(\d{1,3}\.){3}\d{1,3}$)(^(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])$). Please check provider.s3.MyS3Bucket and/or s3 events of function "hello".

serverless.yml

service: some-service
frameworkVersion: '2'
useDotenv: true

provider:
  name: aws
  runtime: python3.8
  lambdaHashingVersion: 20201221
  s3:
    MyS3Bucket:
      bucketName: ${env:MY_BUCKET_NAME}
      accessControl: Private
      lifecycleConfiguration:
        Rules:
          - Id: ExpireRule
            Status: Enabled
            ExpirationInDays: '7'    
package:
  individually: true

functions:
  hello:
    name: my-lambda-function
    handler: function.handler
    memorySize: 128
    timeout: 900
    events:
      - s3:
          bucket: MyS3Bucket
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/

My next try was defining the s3 bucket under ‘resources’, and using the reference of the bucket in the lambda trigger. I am still getting the warning messages

Serverless: Configuration warning at 'functions.hello.events[0].s3.bucket': should be string

serverless.yml

service: some-service
frameworkVersion: '2'
useDotenv: true

provider:
  name: aws
  runtime: python3.8
  lambdaHashingVersion: 20201221
    
package:
  individually: true

functions:
  hello:
    name: my-lambda-function
    handler: handler.handler
    memorySize: 128
    timeout: 900
    events:
      - s3:
          bucket: 
            Ref: MyS3Bucket
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
          existing: true

resources:
  Resources:
    MyS3Bucket:
      Type: AWS::S3::Bucket
      Properties:
        AccessControl: Private
        BucketName: 'test.bucket'
        OwnershipControls:
          Rules:
            - ObjectOwnership: ObjectWriter
        LifecycleConfiguration:
          Rules:
            - Id: ExpireRule
              Status: Enabled
              ExpirationInDays: '7'

Have you fix this?? Use boot3

No I haven’t solved this yet.

The error isn’t about the string MyS3Bucket but about whatever name you’ve given the bucket in your MY_BUCKET_NAME environmment variable.

To debug exactly what’s wrong with the name without posting the name of the bucket here, take the name, and the regex from the error and put into something like https://regex101.com/

Best guess would be that you’ve started the bucket name either with a number, or some punctuation character (e.g. an escaped quote, or ‘.’ or something like that). EDIT: Or you’ve not defined that environment var at all by accident?

I have just faced and fixed this issue, took me a few hours. I gave up using provider and it works right away for some reason.

custom:
  current_stage: ${opt:stage, 'dev'}
  env_var:
    node_env:
      production: "production"
      dev: "development"
    s3_bucket:
      production: "prod-bucket"
      dev: "dev-bucket"

functions:
  snsXXX:
    handler: src/functions/xxx.handler
    events:
      - s3:
        name: ${self:custom.env_var.s3_bucket.${self:custom.current_stage}}
        event: s3:ObjectCreated:*
        existing: true
        rules:
          - suffix: -source.json