localstack / serverless-localstack

⚡ Serverless plugin for running against LocalStack
519 stars 83 forks source link

fix internal Serverless custom resources S3 path addressing #209

Closed bentsku closed 1 year ago

bentsku commented 1 year ago

While debugging a support case about serverless-python-requirements, I stumbled upon an issue regarding Serverless own internal Custom Resources: they would spin up a lambda to update the notification configuration of an existing S3 bucket. However, they would try to access it using virtual host addressing, leading to the lambda failing and the bucket not getting the right configuration. You can reproduce it using this kind of function events:

functions:
  s3_to_dynamodb:
    handler: lambda_functions/s3_to_dynamodb.lambda_handler
    events:
      - s3:
          bucket: test-bucket
          event: s3:ObjectCreated:*
          rules:
            - suffix: .csv
          existing: true

The deployment would not fail, but the S3 bucket would not trigger the lambda.

This PR introduces a mechanism to hook into package:compileEvents which is triggered when building resources to manage events. We're checking for the presence of the custom-resources.zip file, which would be created if Serverless was going to package its internal custom-resources to create its lambdas. We then get the utils.js file which is imported by all lambdas, and set the SDK config there to use S3forcePathStyle=true. As there are nothing in the node standard library to unzip file, I had to introduce a new dependency, adm-zip.

Relevant documentation: