├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── terraform └── main.tf /.gitignore: -------------------------------------------------------------------------------- 1 | terraform/.terraform 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Torgeir Thoresen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodejs-aws-lambda-s3 2 | 3 | Example repo showing how to use terraform to deploy a node.js aws lambda that handles file uploads from s3 4 | 5 | ## deploy to aws using terraform 6 | 7 | - zip up the lambda 8 | - create the lambda 9 | - create an s3 bucket with read only permissions 10 | - create a lambda execution role, that the lambda takes when running 11 | - create a lambda policy for the role, that allows the lambda to create log streams and write logs to cloudwatch 12 | - create an s3 bucket trigger to call the lambda when new objects are created 13 | - create a permission to allow for s3 to trigger the lambda 14 | 15 | ```sh 16 | npm run aws:deploy 17 | ``` 18 | 19 | ## destroy what terraform created 20 | 21 | tear down all of the infrastructure that was created when deploying. 22 | 23 | ```sh 24 | npm run aws:destroy 25 | ``` 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-aws-lambda-s3", 3 | "version": "1.0.0", 4 | "description": "nodejs on aws lambda with terraform, to handle uploads to s3. inspired by https://seanmcgary.com/posts/how-to-deploy-an-aws-lambda-with-terraform", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": {}, 8 | "scripts": { 9 | "zip": "zip -jr terraform/function.zip src/index.js", 10 | "aws:destroy": "cd terraform && terraform destroy && rm function.zip", 11 | "aws:deploy": "npm run zip && cd terraform && terraform apply" 12 | }, 13 | "author": "@torgeir", 14 | "license": "MIT" 15 | } 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | exports.handler = function (event, context, callback) { 2 | console.log("Event: ", JSON.stringify(event, null, "\t")); 3 | console.log("Context: ", JSON.stringify(context, null, "\t")); 4 | 5 | const record = event.Records[0]; 6 | if (!record) throw new Error("No record"); 7 | 8 | const region = record.awsRegion; 9 | const bucket = record.s3.bucket.name; 10 | const file = record.s3.object.key; 11 | 12 | console.log("https://s3-" + region + ".amazonaws.com/" + bucket + "/" + file); 13 | 14 | callback(null); 15 | }; 16 | -------------------------------------------------------------------------------- /terraform/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "eu-west-1" 3 | profile = "privat" 4 | } 5 | 6 | # an s3 bucket 7 | resource "aws_s3_bucket" "bucket" { 8 | bucket = "js-plattform-faggruppe-bucket" 9 | acl = "private" 10 | policy = <