Skip to content

Commit f466416

Browse files
committed
split architecture into sub-services per function group
1 parent 9e72840 commit f466416

21 files changed

+286
-231
lines changed

.bin/deploy.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# load .env variables
4+
export $(cat .env | xargs)
5+
export FUNCTION=$1
6+
7+
if [[ -z "$FUNCTION" ]]; then
8+
echo "Function name is not provided"
9+
exit 1
10+
fi
11+
12+
npx sls deploy

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
AWS_PROFILE=hsz
2+
AWS_ACCOUNT_ID=000
23
AWS_REGION=eu-west-1
4+
AWS_KMS_KEY_ARN=arn:aws:kms:AWS_REGION:AWS_ACCOUNT_ID:key/HASH
5+
SERVICE=onionful-api
36
ENVIRONMENT=development
47
OFFLINE_CACHE_CONTROL=0
58
OFFLINE_PORT=3002

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+12-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"start": "bnr offline",
99
"create-domain": "bnr create-domain",
10-
"deploy": "bnr deploy",
11-
"deploy-function": "bnr deploy-function",
12-
"sls": "bnr sls",
13-
"x": "bnr sls alias remove --alias=staging"
10+
"deploy": "./.bin/deploy.sh",
11+
"sls": "bnr sls"
1412
},
1513
"betterScripts": {
16-
"offline": "sls offline start",
14+
"offline": {
15+
"command": "sls offline start",
16+
"env": {
17+
"ENVIRONMENT": "development",
18+
"IS_OFFLINE": true
19+
}
20+
},
1721
"create-domain": "sls create_domain",
18-
"deploy": "sls deploy",
19-
"deploy-function": "sls deploy function --function ${FUNCTION}",
2022
"sls": "sls"
2123
},
2224
"author": "@hszanowski",
@@ -54,9 +56,11 @@
5456
"serverless-aws-alias": "^1.7.1",
5557
"serverless-domain-manager": "^2.6.6",
5658
"serverless-offline": "^3.31.1",
59+
"serverless-plugin-parent": "0.0.3",
5760
"serverless-webpack": "^5.2.0",
5861
"webpack": "^4.25.1",
59-
"webpack-node-externals": "^1.7.2"
62+
"webpack-node-externals": "^1.7.2",
63+
"js-yaml": "latest"
6064
},
6165
"peerDependencies": {
6266
"aws-sdk": "2.x"

roleStatements.yml

-5
This file was deleted.

serverless.functions.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { has, mapKeys, mapValues, merge, update } = require('lodash');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const yaml = require('js-yaml');
5+
6+
const { AWS_ACCOUNT_ID, AWS_REGION, FUNCTION, IS_OFFLINE, SERVICE } = process.env;
7+
8+
const paths = {
9+
functions: path.join(__dirname, 'src', 'functions'),
10+
};
11+
12+
const fixAuthorizer = event =>
13+
has(event, 'http.authorizer')
14+
? update(
15+
event,
16+
'http.authorizer',
17+
value =>
18+
(IS_OFFLINE ? '' : `arn:aws:lambda:${AWS_REGION}:${AWS_ACCOUNT_ID}:function:`) +
19+
[SERVICE, value].join('-'),
20+
)
21+
: event;
22+
23+
const loadFunctions = name => {
24+
const file = path.join(paths.functions, name, '.functions.yml');
25+
26+
return fs.existsSync(file)
27+
? mapKeys(
28+
mapValues(
29+
yaml.safeLoad(fs.readFileSync(file, 'utf8')),
30+
({ handler, events = [], ...fn }, key) => ({
31+
...fn,
32+
name: [SERVICE, name, key].join('-'),
33+
handler: path.join('src', 'functions', name, handler),
34+
events: events.map(fixAuthorizer),
35+
}),
36+
),
37+
fn => fn.name,
38+
)
39+
: {};
40+
};
41+
42+
const xxx = FUNCTION ? [FUNCTION] : fs.readdirSync(paths.functions);
43+
module.exports = merge({}, ...xxx.map(loadFunctions));

serverless.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const functions = require('./serverless.functions');
2+
3+
const {
4+
AWS_ACCOUNT_ID,
5+
AWS_KMS_KEY_ARN,
6+
AWS_REGION,
7+
FUNCTION,
8+
DOMAIN,
9+
ENVIRONMENT = 'development',
10+
OFFLINE_HOST,
11+
OFFLINE_PORT,
12+
SERVICE,
13+
} = process.env;
14+
15+
const service = [SERVICE, FUNCTION].join('-');
16+
17+
module.exports = {
18+
service,
19+
awsKmsKeyArn: AWS_KMS_KEY_ARN,
20+
provider: {
21+
name: 'aws',
22+
runtime: 'nodejs8.10',
23+
region: 'eu-west-1',
24+
stage: ENVIRONMENT,
25+
environment: { STAGE: ENVIRONMENT },
26+
iamRoleStatements: [
27+
{
28+
Effect: 'Allow',
29+
Action: ['secretsmanager:GetSecretValue'],
30+
Resource: `arn:aws:secretsmanager:${AWS_REGION}:${AWS_ACCOUNT_ID}:secret:${ENVIRONMENT}/onionful*`,
31+
},
32+
],
33+
apiName: service,
34+
stackName: service,
35+
},
36+
plugins: [
37+
'serverless-webpack',
38+
'serverless-offline',
39+
'serverless-domain-manager',
40+
'serverless-aws-alias',
41+
],
42+
custom: {
43+
'serverless-offline': {
44+
host: OFFLINE_HOST,
45+
port: OFFLINE_PORT,
46+
},
47+
customDomain: {
48+
basePath: FUNCTION,
49+
domainName: DOMAIN,
50+
stage: ENVIRONMENT,
51+
},
52+
},
53+
functions,
54+
};

serverless.yml

-212
This file was deleted.

0 commit comments

Comments
 (0)