Upon file uploaded, S3 bucket invokes the lambda function we're about to create.
Our lambda function reads csv file content, then log it out into the console. In a real world scenario, this service might do something like validating or formatting the csv data.
Local environment
Serverless framework version 1.15.2 (installed globally)
service: csv-service
provider:
name: aws
runtime: nodejs6.10
region: ap-northeast-1 # specify region
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
Resource: "arn:aws:s3:::csv-bucket/*"
functions:
hello:
handler: handler.hello
timeout: 20
events:
- s3: # in this case, instead of http call, s3 will trigger the event to invoke our lambda function
bucket: csv-bucket
event: s3:ObjectCreated:* # the hello function will be invoked automatically upon file uploaded
rules:
- prefix: uploaded/ # the folder in the bucket will be watched
- suffix: .csv
Write our lambda function
// handler.js
'use strict'
const aws = require('aws-sdk');
const s3 = new aws.S3();
module.exports.hello = (event, context, callback) => {
console.log(JSON.stringify(event)) // we will need this later for testing locally
callback(null, {})
const Bucket = event.Records[0].s3.bucket.name
const Key = event.Records[0].s3.object.key
const handler = (err, data) => {
if (err) {
context.done('error', `error getting file ${err}`)
} else {
console.log(String(data.Body))
}
}
s3.getObject({ Bucket, Key }, handler)
}
Deploy
As easy as pie
$ sls deploy
Now, our csv service should be working, after the csv uploaded (to the uploaded folder of bucket csv-bucket), if you go to CloudWatch, you should be seeing the csv file content in the logs.
Testing
In order to test our function locally without deploy every time there's a code change, we'll first create a event.json file, in which, we'll simply paste in the event object when lambda function invoked, then:
Application logic
Local environment
Getting started
Create a new project using Serverless framework
Update serverless.yml
Write our lambda function
Deploy
As easy as pie
Now, our csv service should be working, after the csv uploaded (to the
uploaded
folder of bucketcsv-bucket
), if you go to CloudWatch, you should be seeing the csv file content in the logs.Testing
In order to test our function locally without deploy every time there's a code change, we'll first create a
event.json
file, in which, we'll simply paste in the event object when lambda function invoked, then:Debug
Set a
debugger
before the data being console out.Then run node
Open the
chrome-devtools://
url in a new Chrome tab, that's it, happy hacking :)