Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

Commit 9511db0

Browse files
committed
First version
0 parents  commit 9511db0

File tree

8 files changed

+4233
-0
lines changed

8 files changed

+4233
-0
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"plugins": ["source-map-support", "transform-runtime"],
3+
"presets": [
4+
["env", {
5+
"targets": {
6+
"node": "8.10"
7+
},
8+
"useBuiltIns": true
9+
}],
10+
"stage-3"
11+
]
12+
}

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
.webpack
8+
9+
npm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
13+
# Intellij IDEA
14+
.idea
15+
*.iml
16+
17+
# VisualStudioCode
18+
.vscode/*
19+
!.vscode/settings.json
20+
!.vscode/tasks.json
21+
!.vscode/launch.json
22+
!.vscode/extensions.json
23+
.history

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# aws-nodejs8-koa
2+
3+
This is a template you can use to quickly create a Koa-based Serverless service with configured
4+
`serverless-webpack`, `serverless-offline`, and optionally `serverless-dynamodb-local`. It uses
5+
`serverless-http` to wrap a Koa application. We added AWS SDK for your convenience as well.
6+
7+
If you don't need Koa and just need configured Serverless plugins, check out our other template:
8+
[aws-nodejs8-webpack-offline](https://github.com/aproint/aws-nodejs8-webpack-offline)
9+
10+
## How to use
11+
12+
```
13+
sls create --template-url https://github.com/aproint/aws-nodejs8-koa --path myservice
14+
```

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "aws-nodejs8-koa",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"devDependencies": {
7+
"babel-core": "^6.26.3",
8+
"babel-loader": "^7.1.4",
9+
"babel-plugin-source-map-support": "^2.0.1",
10+
"babel-plugin-transform-runtime": "^6.23.0",
11+
"babel-preset-env": "^1.7.0",
12+
"babel-preset-stage-3": "^6.24.1",
13+
"serverless-offline": "^3.25.4",
14+
"serverless-webpack": "^5.1.5",
15+
"webpack": "^4.12.0",
16+
"webpack-node-externals": "^1.7.2"
17+
},
18+
"dependencies": {
19+
"aws-sdk": "^2.255.1",
20+
"babel-runtime": "^6.26.0",
21+
"koa": "^2.5.1",
22+
"koa-bodyparser": "^4.2.1",
23+
"koa-router": "^7.4.0",
24+
"serverless-dynamodb-local": "^0.2.30",
25+
"serverless-http": "^1.5.5",
26+
"source-map-support": "^0.5.6"
27+
},
28+
"scripts": {
29+
"fixdb": "sls dynamodb remove && sls dynamodb install",
30+
"start": "sls offline start"
31+
}
32+
}

serverless.yml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Welcome to Serverless!
2+
#
3+
# This file is the main config file for your service.
4+
# It's very minimal at this point and uses default values.
5+
# You can always add more config options for more control.
6+
# We've included some commented out config examples here.
7+
# Just uncomment any of them to get that config option.
8+
#
9+
# For full config options, check the docs:
10+
# docs.serverless.com
11+
#
12+
# Happy Coding!
13+
14+
service: aws-nodejs8-koa
15+
16+
# You can pin your service to only deploy with a specific Serverless version
17+
# Check out our docs for more details
18+
# frameworkVersion: "=X.X.X"
19+
20+
# The plugins need to be ordered as follows.
21+
plugins:
22+
- serverless-webpack
23+
# Uncomment to add local dynamodb support
24+
# - serverless-dynamodb-local
25+
- serverless-offline
26+
27+
custom:
28+
webpack:
29+
includeModules:
30+
forceExclude:
31+
- aws-sdk
32+
packager: 'yarn' # Packager that will be used to package your external modules
33+
# dynamodb:
34+
# start:
35+
# migrate: true
36+
# seed: false
37+
serverless-offline:
38+
#httpsProtocol: "dev-certs"
39+
#port: 4000
40+
41+
provider:
42+
name: aws
43+
runtime: nodejs8.10
44+
45+
# you can overwrite defaults here
46+
# stage: dev
47+
# region: us-east-1
48+
49+
# you can add statements to the Lambda function's IAM Role here
50+
# iamRoleStatements:
51+
# - Effect: "Allow"
52+
# Action:
53+
# - "s3:ListBucket"
54+
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
55+
# - Effect: "Allow"
56+
# Action:
57+
# - "s3:PutObject"
58+
# Resource:
59+
# Fn::Join:
60+
# - ""
61+
# - - "arn:aws:s3:::"
62+
# - "Ref" : "ServerlessDeploymentBucket"
63+
# - "/*"
64+
65+
# you can define service wide environment variables here
66+
# environment:
67+
# variable1: value1
68+
69+
# you can add packaging information here
70+
#package:
71+
# include:
72+
# - include-me.js
73+
# - include-me-dir/**
74+
# exclude:
75+
# - exclude-me.js
76+
# - exclude-me-dir/**
77+
78+
functions:
79+
hello:
80+
handler: src/handler.server
81+
events:
82+
- http:
83+
path: / # this matches the base path
84+
method: ANY
85+
cors: true
86+
- http:
87+
path: /{any+} # this matches any path, the token 'any' doesn't mean anything special
88+
method: ANY
89+
cors: true
90+
91+
# The following are a few example events you can configure
92+
# NOTE: Please make sure to change your handler code to work with those events
93+
# Check the event documentation for details
94+
# events:
95+
# - http:
96+
# path: users/create
97+
# method: get
98+
# - s3: ${env:BUCKET}
99+
# - schedule: rate(10 minutes)
100+
# - sns: greeter-topic
101+
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
102+
# - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx
103+
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
104+
# - iot:
105+
# sql: "SELECT * FROM 'some_topic'"
106+
# - cloudwatchEvent:
107+
# event:
108+
# source:
109+
# - "aws.ec2"
110+
# detail-type:
111+
# - "EC2 Instance State-change Notification"
112+
# detail:
113+
# state:
114+
# - pending
115+
# - cloudwatchLog: '/aws/lambda/hello'
116+
# - cognitoUserPool:
117+
# pool: MyUserPool
118+
# trigger: PreSignUp
119+
120+
# Define function environment variables here
121+
# environment:
122+
# variable2: value2
123+
124+
# You can add CloudFormation resource templates here
125+
#resources:
126+
# Resources:
127+
# yourDynamoDbTable:
128+
# Type: AWS::DynamoDB::Table
129+
# Properties:
130+
# TableName: YourDynamoDbTable
131+
# AttributeDefinitions:
132+
# - AttributeName: id
133+
# AttributeType: S
134+
# KeySchema:
135+
# - AttributeName: id
136+
# KeyType: HASH
137+
# ProvisionedThroughput:
138+
# ReadCapacityUnits: 5
139+
# WriteCapacityUnits: 5
140+
# NewResource:
141+
# Type: AWS::S3::Bucket
142+
# Properties:
143+
# BucketName: my-new-bucket
144+
# Outputs:
145+
# NewOutput:
146+
# Description: "Description for the output"
147+
# Value: "Some output value"

src/handler.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import serverless from 'serverless-http'
2+
import Koa from 'koa'
3+
import Router from 'koa-router'
4+
import bodyParser from 'koa-bodyparser'
5+
6+
const app = new Koa()
7+
const router = new Router()
8+
9+
const getResult = async (id) => new Promise((resolve, reject) => {
10+
resolve((id % 2 === 0) ? 'odd' : 'even')
11+
})
12+
13+
router.get('/:id', async (ctx) => {
14+
ctx.body = await getResult(ctx.params.id)
15+
})
16+
17+
app.use(bodyParser())
18+
app.use(router.routes())
19+
app.use(router.allowedMethods())
20+
21+
export const server = serverless(app)

webpack.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const slsw = require('serverless-webpack');
2+
const nodeExternals = require("webpack-node-externals");
3+
4+
module.exports = {
5+
entry: slsw.lib.entries,
6+
target: "node",
7+
// Generate sourcemaps for proper error messages
8+
devtool: 'source-map',
9+
// Since 'aws-sdk' is not compatible with webpack,
10+
// we exclude all node dependencies
11+
externals: [nodeExternals()],
12+
mode: slsw.lib.webpack.isLocal ? "development" : "production",
13+
optimization: {
14+
// We no not want to minimize our code.
15+
minimize: false
16+
},
17+
performance: {
18+
// Turn off size warnings for entry points
19+
hints: false
20+
},
21+
// Run babel on all .js files and skip those in node_modules
22+
module: {
23+
rules: [
24+
{
25+
test: /\.js$/,
26+
loader: "babel-loader",
27+
include: __dirname,
28+
exclude: /node_modules/
29+
}
30+
]
31+
}
32+
};

0 commit comments

Comments
 (0)