简体   繁体   中英

AWS Lambda NodeJS writing to S3 behaves differently on local and sever

I created a NodeJS Lambda function with Serverless. It reads from a DynamoDB table and writes the data to a S3 bucket.

Here's my handler.js : (showing the affected function):

module.exports.exportContactsByClientID = (event, context, callback) => {
  // const id = event.pathParameters.id.toLowerCase();
  const id= 'mkh';
  
  const params = {
    TableName: contactsTable,
    IndexName: "client_code-created_at_local-index",
    KeyConditionExpression: "client_code = :v_ID",
    ExpressionAttributeValues: {
      ":v_ID": id,
    },
    ProjectionExpression: "client_code, created_at, contact_name, age, phone, email, dsr_ratio, max_eligibility, selected_banks"
  };

return db.query(params)
.promise()
.then((res) => {
  // console.log(res);
  const items = res.Items;
  // Headers + Columns
  worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
  worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
  worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
  worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
  worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
  worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
  worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
  // Rows
  // items.sort((a, b) => (a.date > b.date) ? -1 : 1);
  items.forEach((item, i) => {
    console.log(item.contact_name);
    worksheet.cell(i + 2, 1).string(item.client_code);
    worksheet.cell(i + 2, 2).string(item.contact_name);
    worksheet.cell(i + 2, 3).string(item.age);
    worksheet.cell(i + 2, 4).string(item.email);
    worksheet.cell(i + 2, 5).string(item.phone);
    worksheet.cell(i + 2, 6).string(item.dsr_ratio);
    worksheet.cell(i + 2, 7).string(item.max_eligibility);
  });
  console.log(contactsBucket);
  console.log(id);
  workbook.writeToBuffer().then(buffer => {
    console.log('inside writetoubgger')
    var params = {
      Bucket: contactsBucket,
      Key: `contacts/mkh.xlsx`,
      Body: buffer,
      ACL: 'public-read'
    }
    S3.upload(params, function(err, data) {
      if (err) {
        console.log(err, err.stack);
      } else {
        // callback(null, response(200, res));
        callback(null, response(200, res));
      }
    });
  })
})
.catch((err) => callback(null, response(err.statusCode, err)));
};

My serverless.yml :

service: mhub-dsr-calculator-api

plugins:
  - serverless-plugin-include-dependencies

package:
  exclude:
    - node_modules/**

custom:
  allowed-headers:
      - Content-Type
      - Authorization
      - X-Api-Token
      - X-Origin
      - X-Amz-Date
      - X-Amz-Security-Token
  settings:
    CONTACTS_TABLE: dsrcalculator-contacts-dev
    CONTACTS_BUCKET: mhub-dsrcalculator-contacts

provider:
  name: aws
  runtime: nodejs12.x
  environment: ${self:custom.settings}
  region: ap-southeast-1 
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:Query
      Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}"
    - Effect: "Allow"
      Action:
        - dynamodb:Query
      Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}/index/client_code-created_at_local-index"
    - Effect: "Allow"
      Action:
       - "s3:*"
      Resource: 
       - "arn:aws:s3:::${self:custom.settings.CONTACTS_BUCKET}/*"

functions:
  createPost:
    handler: handler.createContact
    events:
    - http:
        path: /contact
        method: post
        cors: true
  updatePost:
    handler: handler.updateContact
    events:
    - http:
        path: /contact/{id}
        method: put
        cors: true
  getAllPosts:
    handler: handler.getAllContacts
    events:
    - http:
        path: /contacts
        method: get
        cors: true
  getContactsByClientID:
    handler: handler.getContactsByClientID
    events:
    - http:
        path: /contacts/{id}
        method: get
        cors: true
  exportContactsByClientID:
    handler: handler.exportContactsByClientID
    events:
    - http:
        path: /export/{id}
        method: get
        cors: true

resources:
  Resources:
    PostsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
        - AttributeName: "id"
          AttributeType: "S"
        - AttributeName: "client_code"
          AttributeType: "S"
        - AttributeName: "created_at_local"
          AttributeType: "S"
        KeySchema:
        - AttributeName: "id"
          KeyType: "HASH"
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.settings.CONTACTS_TABLE}
        

I get the following output when I invoke the function locally (with serverless invoke local -f exportContactsByClientID ) where 'mhub-dsrcalculator-contacts' is the bucket name, 'mkh' is the id and 'inside writetoubgger' indicates the code inside workbook.writeToBuffer ran.

mhub-dsrcalculator-contacts
mkh
inside writetoubgger

A file on S3 bucket is created successfully.

However when I deploy this and test, though it runs to succession the code is not entering into 'workbook.writeToBuffer' as we can see below.

2020-06-22T11:23:22.945Z    1ce59a7b-98e4-4c9e-8736-ec7c162c4759    INFO    mhub-dsrcalculator-contacts
2020-06-22T11:23:22.945Z    1ce59a7b-98e4-4c9e-8736-ec7c162c4759    INFO    mkh
END RequestId: 1ce59a7b-98e4-4c9e-8736-ec7c162c4759

Why is this happening? Any guidance will be greatly appreciated. Thank you.

Your outer promise is finishing without waiting the writeToBuffer promise.

Try changing:

workbook.writeToBuffer().then(...

To:

return workbook.writeToBuffer().then(...

You should do the same to the S3 upload call:

return S3.upload(params).promise().then(data => {
        callback(null, response(200, res));
});

I would also recommend you rewrite your code using the async / await syntax, so it is easier to read and understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM