Upload file to s3 bucket via lambda function
🏠 Homepage
.
├── README.MD <-- This instructions file
├── event.json <-- API Gateway Proxy Integration event payload
├── upload <-- Source code for a lambda function
│ └── app.js <-- Lambda function code
│ └── package.json <-- NodeJS dependencies and scripts
│ └── tests <-- Unit tests
│ └── upload.test.js
├── template.yaml <-- SAM template
Invoking function locally using a local sample payload
sam local invoke LambdaS3Upload --event event.json
Invoking function locally through local API Gateway
sam local start-api
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:3000/upload
AWS Lambda NodeJS runtime requires a flat folder with all dependencies including the application. SAM will use CodeUri
property to know where to look up for both application and dependencies:
...
LambdaS3Upload:
Type: AWS::Serverless::Function
Properties:
CodeUri: upload/
...
Firstly, we need a S3 bucket
where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one:
aws s3 mb s3://BUCKET_NAME
Next, run the following command to package our Lambda function to S3:
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
Next, the following command will create a Cloudformation Stack and deploy your SAM resources.
sam deploy \
--template-file packaged.yaml \
--stack-name lambda-s3-upload \
--capabilities CAPABILITY_IAM
npm install
npm install
npm run test
👤 Sebastian Serrano
- Github: @sebastianserrano
Copyright © 2019 Sebastian Serrano.
This project is MIT licensed.