Skip to content

Commit 9ed5e22

Browse files
webpack2, split code across multiple files, use postgres
1 parent 648d941 commit 9ed5e22

File tree

8 files changed

+431
-680
lines changed

8 files changed

+431
-680
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ To install it (assuming you have serverless configured with AWS):
77
`yarn install`
88

99
### For local development:
10+
> Note: add your database connection details to hello.ts before runing the example
1011
11-
`yarn run dev`
12+
`yarn build` - compile the project into the `dist` folder.
13+
`yarn dev` - compile the project then use [serverless-offline](https://github.com/dherault/serverless-offline) to emulate the nodejs lambda execution environment so we can make HTTP requests locally.
1214

1315
Your lambda should be avaliable on `http://localhost:3000` - and example function: `http://localhost:3000/hello`
1416

1517
### For deployment:
1618

17-
`yarn run deploy`
19+
`yarn deploy`
1820

1921

2022
TODO (PR's are welcome!):

package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
"description": "Serverless webpack example using Typescript",
55
"main": "handler.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
8-
"dev": "serverless offline --location dist",
9-
"deploy": "serverless deploy"
7+
"build": "webpack --config webpack.config.js",
8+
"deploy": "serverless deploy",
9+
"dev": "yarn build && serverless offline --location dist",
10+
"test": "echo \"Error: no test specified\" && exit 1"
1011
},
11-
"author": "Nicola Peduzzi <[email protected]> (http://nikso.net)",
12+
"author": "Craig Snyders <[email protected]>",
1213
"license": "MIT",
1314
"devDependencies": {
15+
"@types/node": "^7.0.29",
16+
"awesome-typescript-loader": "^3.1.3",
17+
"aws-lambda": "^0.1.2",
18+
"pg": "^6.2.3",
19+
"pg-native": "^1.10.1",
1420
"serverless-offline": "^3.13.5",
15-
"serverless-webpack": "^1.0.0-rc.4",
16-
"ts-loader": "^2.0.3",
17-
"typescript": "^2.3.2",
18-
"webpack": "^2.5.1"
21+
"typescript": "^2.3.4",
22+
"webpack": "^2.6.1"
1923
},
2024
"dependencies": {
21-
"aws-lambda": "^0.1.2"
25+
"@types/pg": "^6.1.41"
2226
}
2327
}

serverless.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
service: serverless-webpack-typescript-example
22

3-
# Add the serverless-webpack plugin
43
plugins:
5-
- serverless-webpack
64
- serverless-offline
75

86
provider:

src/handler.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
import { Context, Callback } from 'aws-lambda';
2-
3-
4-
export const hello = (event: any, context: Context, cb: Callback) => {
5-
return cb(null,
6-
{ message: 'Typescript handler works!', event }
7-
);
8-
}
1+
export * from './hello';

src/hello.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Callback, Context } from 'aws-lambda';
2+
import { Client } from 'pg';
3+
4+
export const hello = (event: any, context: Context, cb: Callback) => {
5+
6+
// instantiate our pg client with connection details
7+
const client = new Client({
8+
user: 'your-username-here',
9+
password: 'your-password-here',
10+
database: 'your-database-here',
11+
host: 'abc.def.your-host.com',
12+
ssl: true
13+
});
14+
15+
// connect to db
16+
client.connect(function (err) {
17+
if (err) throw err;
18+
19+
const query = `
20+
select
21+
*
22+
from
23+
yout_table_name
24+
;
25+
`;
26+
27+
// execute a query on our database
28+
client.query(query, null, function (err, result) {
29+
if (err) throw err;
30+
31+
// disconnect the client
32+
client.end(function (err) {
33+
if (err) throw err;
34+
});
35+
36+
// call the lambda callback with our db data and event object
37+
cb(null,
38+
{
39+
dbResponse: result.rows,
40+
event
41+
}
42+
);
43+
44+
});
45+
});
46+
};

tsconfig.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"module": "commonjs"
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"lib": [
7+
"es6"
8+
],
9+
"types": [
10+
"node"
11+
],
12+
"typeRoots": [
13+
"node_modules/@types"
14+
]
515
},
616
"include": [
7-
"src/**/*.ts"
17+
"src/**/*.ts"
818
],
919
"exclude": [
10-
"node_modules",
11-
"**/*.spec.ts"
20+
"dist",
21+
"**/*.spec.ts"
1222
]
1323
}

webpack.config.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
var path = require('path');
1+
const path = require('path');
22

3-
module.exports = {
4-
entry: './src/handler.ts',
5-
output: {
6-
libraryTarget: 'commonjs',
7-
path: path.join(__dirname, 'dist'),
8-
filename: 'handler.js',
9-
},
10-
target: 'node',
11-
module: {
12-
loaders: [{ test: /\.ts(x?)$/, loader: 'ts-loader' }],
13-
},
14-
resolve: {
15-
extensions: ['.ts', '.js', '.tsx', '.jsx', ''],
16-
},
3+
const root = (dir) => {
4+
return path.resolve(__dirname, dir);
175
};
186

19-
// When webpack 2 will be supported with serverless-webpack, it'll look like:
20-
21-
// module.exports = {
22-
// entry: './src/handler.ts',
23-
// output: {
24-
// filename: 'handler.js',
25-
// path: path.join(__dirname, 'dist'),
26-
// },
27-
// module: {
28-
// rules: [
29-
// {
30-
// test: /\.tsx?$/,
31-
// loader: 'ts-loader',
32-
// exclude: /node_modules/,
33-
// },
34-
// ],
35-
// },
36-
// resolve: {
37-
// extensions: ['.tsx', '.ts', '.js'],
38-
// },
39-
// };
7+
module.exports = (options) => {
8+
return {
9+
entry: {
10+
'main': './src/handler.ts'
11+
},
12+
resolve: {
13+
extensions: ['.ts', '.js'],
14+
modules: [root('node_modules'), root('src')]
15+
},
16+
target: 'node',
17+
module: {
18+
rules: [
19+
{
20+
test: /\.ts$/,
21+
use: [
22+
{
23+
loader: 'awesome-typescript-loader',
24+
options: {
25+
useCache: false
26+
}
27+
}
28+
],
29+
exclude: [/\.(spec|e2e)\.ts$/]
30+
}
31+
]
32+
},
33+
output: {
34+
path: root('dist'),
35+
filename: 'handler.js',
36+
libraryTarget: 'commonjs'
37+
}
38+
};
39+
};

0 commit comments

Comments
 (0)