Upload a file to S3 to trigger a lambda function
This example shows how to upload a file to S3 using a HTML form, and have S3 trigger a lambda function.
Use-cases
- Postprocess files uploaded to an S3 bucket.
Setup
- The upload bucket defaults to
upload-postprocess-<stage>-<account-id>, which is unique to your AWS account and stage. To use a different name, set theBUCKETenvironment variable before deploying. - Edit
generate-form.jsand fill in youraws_access_key_id,aws_secret_access_keyandbucket_name(matching the bucket name above). - Run
yarn installto install crypto-js dependency forgenerate-form.js. - Generate the HTML form:
yarn install
node generate-form.js
Deploy
In order to deploy the example, simply run:
serverless deploy
The output should look similar to:
Deploying "upload-to-s3-and-postprocess" to stage "dev" (us-east-1)
✔ Service deployed to stack upload-to-s3-and-postprocess-dev (38s)
functions:
postprocess: upload-to-s3-and-postprocess-dev-postprocess (1.1 kB)
Usage
Open the generated frontend/index.html in your browser, or run:
xdg-open frontend/index.html
Select a PNG image smaller than 1Mb, and click "Upload File to S3".
You should get an XML response similar to:
<PostResponse>
<Location>https://serverless-fetch-file-and-store-in-s3.s3.amazonaws.com/uploads%2Fimage.png</Location>
<Bucket>serverless-fetch-file-and-store-in-s3</Bucket>
<Key>uploads/image.png</Key>
<ETag>"08c03c6a24e5058b9f3556981a23b1d7"</ETag>
</PostResponse>
After a while, the postprocess function gets triggered by an S3 event:
serverless logs --function postprocess
START RequestId: e2deccf2a0-11e6-b6e3fbcfad7d8c Version: $LATEST
2014 12:32:30.350 (+02:00) e2deccf2a0-11e6-b6e3fbcfad7d8c New .png object has been created: uploads/image.png (23975 bytes)
END RequestId: e2deccf2a0-11e6-b6e3fbcfad7d8c
REPORT RequestId: e2deccf2a0-11e6-b6e3fbcfad7d8c Duration: 2.84 ms Billed Duration: 100 msMemory Size: 1024 MB Max Memory Used: 29 MB