retinal icon indicating copy to clipboard operation
retinal copied to clipboard

Handler fails with error "Cannot read property 'forEach' of undefined"

Open wim-jan opened this issue 7 years ago • 2 comments

The following error occurs when an image is uploaded to S3:

{
    "errorMessage": "Cannot read property 'forEach' of undefined",
    "errorType": "TypeError",
    "stackTrace": [
        "/var/task/handler.js:959:20",
        "next (native)",
        "step (/var/task/handler.js:279:31)",
        "/var/task/handler.js:290:14",
        "process._tickDomainCallback (internal/process/next_tick.js:135:7)"
    ]
}

I use an existing bucket for both the sourceBucket and the destinationBucket, with the following serverless.yml:

service: ${file(config.json):name}

provider:
  name: aws
  runtime: nodejs6.10
  profile: ${file(config.json):provider.profile}
  stage: ${file(config.json):provider.stage}
  region: ${file(config.json):provider.region}
  iamRoleStatements:
    -  Effect: "Allow"
       Action:
         - "s3:ListBucket"
         - "s3:GetObject"
         - "s3:GetObjectAcl"
       Resource:
         Fn::Join:
           - ""
           -  - "arn:aws:s3:::"
              - ${file(config.json):sourceBucket}
              - "/"
              - ${file(config.json):sourcePrefix}
              - "*"
    -  Effect: "Allow"
       Action:
         - "s3:ListBucket"
         - "s3:PutObject"
         - "s3:DeleteObject"
         - "s3:GetObjectAcl"
         - "s3:ListBucketMultipartUploads"
         - "s3:ListMultipartUploadParts"
         - "s3:PutObject"
         - "s3:PutObjectAcl"
         - "s3:PutObjectTagging"
         - "s3:PutObjectVersionAcl"
         - "s3:PutObjectVersionTagging"
       Resource:
         Fn::Join:
           - ""
           -  - "arn:aws:s3:::"
              - ${file(config.json):destinationBucket}
              - "/"
              - ${file(config.json):destinationPrefix}
              - "*"

custom:
  webpackIncludeModules: false # disable auto including modules

plugins:
  - serverless-webpack

package:
  exclude:
    - .serverless
    - .webpack
    - coverage
    - .babelrc
    - .eslintignore
    - .eslintrc
    - .gitignore
    - LICENSE
    - package.json
    - README.md
    - serverless.yml
    - webpack.config.js

functions:
  sharpImage:
    description: Resizes images
    memorySize: 1024
    timeout: 30
    handler: handler.processImage

My guessing it has something to do with the whole 'existing bucket for both the source as the destination'.

wim-jan avatar Aug 01 '17 13:08 wim-jan

Hi @wim-jan. What does your config.json file look like? Have you defined outputs? Is there any additional logging output before or after the error in CloudWatch? If possible, could you share that, too?

adieuadieu avatar Aug 01 '17 15:08 adieuadieu

@adieuadieu this is my config.json:

{
  "name": "serverless-sharp-image",
  "provider": {
    "profile": "serverless-admin",
    "stage": "dev",
    "region": "eu-central-1"
  },
  "sourceBucket": "foo-bucket",
  "sourcePrefix": "uploads",
  "destinationBucket": "foo-bucket",
  "destinationPrefix": "images/resized/",
  "s3": {
    "params": {
      "Metadata": {
        "generator": "created by a serverless-sharp-image lambda function"
      }
    }
  },
  "all": [
    ["rotate"],
    ["toFormat", "jpeg", { "quality": 80 }]
  ],
  "outputs": [
    {
      "key": "%(filename)s-200x200.jpg",
      "params": {
        "ACL": "public-read"
      },
      "operations": [
        ["resize", 200, 200],
        ["max"],
        ["withoutEnlargement"]
      ]
    },{
      "key": "%(filename)s.jpg",
      "params": {
        "ACL": "public-read"
      }
    },
    {
      "key": "%(filename)s-100x100.jpg",
      "operations": [
        ["resize", 100, 100],
        ["max"],
        ["withoutEnlargement"]
      ]
    }
  ]
}

There aren't really any other logs, other than START, END and Processing: uploads/foo-img.jpg

wim-jan avatar Aug 01 '17 15:08 wim-jan