Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node + webpack + babel example :D #566

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
| [Dropbox](https://github.com/serverless/examples/tree/master/aws-node-oauth-dropbox-api) <br/> dropbox integration | nodeJS |
| [Aws Node Puppeteer](https://github.com/serverless/examples/tree/master/aws-node-puppeteer) <br/> When it comes to AWS Lambda function , they have their own limits as follows ![AWS Limits](./images/aws_limits.png) So , When you try to use Puppeteer your deployment package size(unzipped) easily go's above 250 mb because When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. | nodeJS |
| [Aws Node Rekognition Analysis S3 Image](https://github.com/serverless/examples/tree/master/aws-node-rekognition-analysis-s3-image) <br/> Analyse an Image from an S3 Bucket with Amazon Rekognition | nodeJS |
| [Serverless Babel Webpack Sample](https://github.com/serverless/examples/tree/master/aws-node-rest-api-babel-webpack) <br/> A simple way to create an AWS API Gateway lambda | nodeJS |
| [Aws Node Restapi Mongodb](https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb) <br/> Serverless REST API with MongoDB using Mongoose and Bluebird | nodeJS |
| [Aws Node Rest Api Typescript](https://github.com/serverless/examples/tree/master/aws-node-rest-api-typescript-simple) | nodeJS |
| [Aws Node Typescript Rest Api](https://github.com/serverless/examples/tree/master/aws-node-rest-api-typescript) <br/> This is simple REST API example for AWS Lambda By Serverless framwork with TypeScript and MongoDB Atlas. | nodeJS |
Expand Down
13 changes: 13 additions & 0 deletions aws-node-rest-api-babel-webpack/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"plugins": ["source-map-support"],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "8.10"
}
}
]
]
}
10 changes: 10 additions & 0 deletions aws-node-rest-api-babel-webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Dependency directories
node_modules/
jspm_packages/

# Build folders/files
.serverless
.webpack
.docker
jsconfig.json

72 changes: 72 additions & 0 deletions aws-node-rest-api-babel-webpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Serverless + Webpack + Babel Sample
---

![badgen](https://badgen.net/badge/built%20with/love/red)
![badgen](https://badgen.net/badge/built%20with/javascript/yellow)
[![serverless](https://camo.githubusercontent.com/547c6da94c16fedb1aa60c9efda858282e22834f/687474703a2f2f7075626c69632e7365727665726c6573732e636f6d2f6261646765732f76332e737667)](http://www.serverless.com)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/0c62da09be11447b9427e85176e70a89)](https://www.codacy.com/gh/pcrodrigues0/serverless-babel-webpack/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=pcrodrigues0/serverless-babel-webpack&amp;utm_campaign=Badge_Grade)
![GitHub last commit](https://img.shields.io/github/last-commit/pcrodrigues0/serverless-babel-webpack)
[![Made By](https://img.shields.io/badge/made%20by-Paulo%20Rodrigues-blue)](https://www.linkedin.com/in/pcqrodrigues/)
---
#### Setup
You need to:
* [Configure the aws credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
* [Serverless framework](https://serverless.com) installed.
* [Node.js](https://nodejs.org/)
* [Yarn](https://yarnpkg.com/) or [Npm](https://www.npmjs.com/)

### To Test It Locally 💻

* Run ```npm install``` or ```yarn``` to install all the necessary dependencies.
* Run ```npm start:local``` or ```yarn start:local``` use serverless offline to test locally.

The expected result should be similar to:

```
Serverless: Bundling with Webpack...
Time: 659ms
Built at: 10/27/2020 7:18:32 PM
Asset Size Chunks Chunk Names
src/index.js 5.99 KiB src/index [emitted] src/index
src/index.js.map 4.58 KiB src/index [emitted] [dev] src/index
Entrypoint src/index = src/index.js src/index.js.map
[./src/helpers/response.js] 238 bytes {src/index} [built]
[./src/index.js] 209 bytes {src/index} [built]
[source-map-support/register] external "source-map-support/register" 42 bytes {src/index} [built]
Serverless: Watching for changes...
offline: Starting Offline: local/us-east-1.
offline: Offline [http for lambda] listening on http://localhost:3002

┌────────────────────────────────────────────────────────────────────────────┐
│ │
│ GET | http://localhost:3000/local/test │
│ POST | http://localhost:3000/2015-03-31/functions/getTeste/invocations │
│ │
└────────────────────────────────────────────────────────────────────────────┘

offline: [HTTP] server ready: http://localhost:3000 🚀
offline:
offline: Enter "rp" to replay the last request
```

### Deploy on AWS 🚀

```
$ npm run deploy:{yourStage}
```
or
```
$ yarn deploy:{yourStage}
```

## Usage 🎉

send an HTTP request directly to the endpoint using a tool like curl

```
curl https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/{stage}/test
```

## Scaling 🔝

By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 100. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in [To request a limit increase for concurrent executions](http://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html#increase-concurrent-executions-limit).
2 changes: 2 additions & 0 deletions aws-node-rest-api-babel-webpack/environments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default:
SLS_STAGE: ${self:custom.stage}
11 changes: 11 additions & 0 deletions aws-node-rest-api-babel-webpack/functions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
getTeste:
name: teste-${self:custom.stage}-query
description: Test Query for sample api gateway lambda
handler: ${self:custom.basePath, ''}src/index.handler
memorySize: 128
timeout: 30
events:
- http:
path: test
method: get
cors: true
36 changes: 36 additions & 0 deletions aws-node-rest-api-babel-webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "serverless-babel-webpack-sample",
"version": "1.0.1",
"description": "A simple way to create an AWS API Gateway lambda",
"scripts": {
"lint": "eslint .",
"start:local": "sls offline",
"deploy:dev": "sls deploy --stage dev",
"deploy:qa": "sls deploy --stage qa",
"deploy:staging": "sls deploy --stage staging",
"deploy:prod": "AWS_PROFILE=your-production-profile SLS_STAGE=prod serverless deploy --stage prod"
},
"devDependencies": {
"eslint": "^5.15.3",
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.16.0",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"babel-loader": "^8.0.5",
"babel-plugin-source-map-support": "^2.0.1",
"serverless-offline": "^6.5.0",
"webpack": "^4.29.3"
},
"dependencies": {
"serverless-webpack": "^5.2.0",
"source-map-support": "^0.5.10",
"webpack-node-externals": "^1.7.2"
},
"engines": {
"node": ">= 12.18",
"yarn": ">= 1.22.5"
},
"author": "Paulo Rodrigues <pcqrodrigues@gmail.com>",
"license": "MIT",
"private": true
}
42 changes: 42 additions & 0 deletions aws-node-rest-api-babel-webpack/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
service: sample-api-gateway

plugins:
- serverless-webpack
- serverless-offline

custom:
stage: ${opt:stage, 'local'}
basePath: ''
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
packager: yarn

provider:
name: aws
runtime: nodejs12.x
tags:
projeto: sample-api-gateway
stage: ${opt:stage, 'dev'}
apiName: ${self:service}
stackName: ${self:service}-${self:custom.stage}
deploymentBucket:
name: dev-projectname-deploys
memorySize: 256
timeout: 30
versionFunctions: true
logRetentionInDays: 14

environment: ${file(./environments.yml):${self:custom.stage}, file(./environments.yml):default}

functions: ${file(./functions.yml)}

package:
individually: true
excludeDevDependencies: true
exclude:
- README.md
- .serverless/**
- .webpack/**
- test/**
- /*.yml
7 changes: 7 additions & 0 deletions aws-node-rest-api-babel-webpack/src/helpers/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (statusCode, message) => ({
statusCode,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(message),
});
6 changes: 6 additions & 0 deletions aws-node-rest-api-babel-webpack/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createResponse from './helpers/response';

export const handler = async () =>
createResponse(200, {
message: 'Encountered a test!',
});
26 changes: 26 additions & 0 deletions aws-node-rest-api-babel-webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');

module.exports = {
entry: slsw.lib.entries,
target: 'node',
devtool: 'source-map',
externals: [nodeExternals()],
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
optimization: {
minimize: false,
},
performance: {
hints: false,
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: __dirname,
exclude: /node_modules/,
},
],
},
};