Skip to content

Commit 54e7cc1

Browse files
committed
Initial commit
0 parents  commit 54e7cc1

15 files changed

+573
-0
lines changed

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root: true
2+
3+
env:
4+
node: true
5+
es6: true
6+
7+
extends:
8+
"eslint:recommended"
9+
10+
ecmaFeatures:
11+
generators: true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage
2+
.nyc_output
3+
node_modules

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.nyc_output
2+
coverage
3+
__tests__
4+
.eslintrc
5+
.gitignore

LICENSE

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Doug Moscrop
4+
5+
The following license applies to all parts of this software except as
6+
documented below:
7+
8+
====
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
28+
====
29+
30+
All files located in the node_modules and external directories are
31+
externally maintained libraries used by this software which have their
32+
own licenses; we recommend you read them, as their terms may differ from
33+
the terms above.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# serverless-plugin-custom-domain
2+
3+
This is a plugin for Serverless that injects a [CloudFormation Custom Resource](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html) in your deployed stack that sets up a base path mapping between the `ApiGateway::Deployment` that Serverless creates, and an [API Gateway Custom Domain](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html).
4+
5+
## Usage
6+
7+
```yaml
8+
9+
service: my-service
10+
11+
plugins:
12+
- serverless-plugin-custom-domain
13+
14+
custom:
15+
domain: "${opt:region}.myservice.foo.com"
16+
```
17+
18+
## Notes
19+
20+
### Why a Custom Resource?
21+
22+
CloudFormation supports `ApiGateway::BasePathMapping` resources but I found they frequently fail to update correctly. Implementing the (relatively simple) logic to get-and-update-or-create combined with a `remove` hook for cleanup has proven to be more reliable.
23+
24+
### Setting up the Custom Domain
25+
26+
These take a long time to provision and are long-lived persistent resources that have Route53 entires pointing at them as well as ACM certificates that have to be requested and approved. You should manage these outside of Serverless, either via CloudFormation or something like Terraform.

__tests__/add-custom-resource.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const test = require('ava');
4+
const sinon = require('sinon');
5+
6+
const Plugin = require('..');
7+
8+
test.beforeEach(t => {
9+
t.context.template = {
10+
Resources: {}
11+
};
12+
t.context.serverless = {
13+
getProvider: () => null,
14+
service: {
15+
provider: {
16+
compiledCloudFormationTemplate: t.context.template
17+
}
18+
}
19+
};
20+
t.context.plugin = new Plugin(t.context.serverless, {
21+
stage: 'test'
22+
});
23+
});
24+
25+
test('addCustomResource adds resources', t => {
26+
const getSourceCodeLines = sinon.stub(t.context.plugin, 'getSourceCodeLines').returns(['foo']);
27+
28+
t.context.plugin.addCustomResource();
29+
30+
t.true(getSourceCodeLines.called);
31+
t.deepEqual(Object.keys(t.context.template.Resources), [
32+
'CustomApiGatewayBasePathMappingFunction',
33+
'CustomApiGatewayBasePathMappingLogGroup',
34+
'CustomApiGatewayBasePathMapping',
35+
'CustomApiGatewayBasePathMappingRole'
36+
]);
37+
});

__tests__/custom-domain.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const test = require('ava');
4+
5+
const Plugin = require('..');
6+
7+
test.beforeEach(t => {
8+
t.context.template = {
9+
Resources: {}
10+
};
11+
t.context.serverless = {
12+
getProvider: () => null,
13+
service: {
14+
provider: {
15+
compiledCloudFormationTemplate: t.context.template
16+
},
17+
custom: {
18+
domain: 'foo.com'
19+
}
20+
}
21+
};
22+
t.context.plugin = new Plugin(t.context.serverless, {
23+
stage: 'test'
24+
});
25+
});
26+
27+
test('custom-domain adds hooks', t => {
28+
t.deepEqual(Object.keys(t.context.plugin.hooks), [
29+
'before:aws:package:finalize:mergeCustomProviderResources',
30+
'before:remove:remove',
31+
]);
32+
});
33+
34+
test('throws if unable to find domain name', t => {
35+
t.context.plugin.serverless.service.custom.domain = {};
36+
t.throws(() => {
37+
t.context.plugin.beforePackage();
38+
});
39+
})
40+
41+
test('throws if unable to find ApiGatewayDeployment', t => {
42+
t.context.plugin.serverless.service.custom.domain = 'foo.com';
43+
44+
t.throws(() => {
45+
t.context.plugin.beforePackage();
46+
});
47+
});

__tests__/remove-mapping.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const test = require('ava');
4+
const sinon = require('sinon');
5+
6+
const Plugin = require('..');
7+
8+
test.beforeEach(t => {
9+
t.context.provider = {
10+
request: Function.prototype
11+
};
12+
t.context.serverless = {
13+
getProvider: () => t.context.provider,
14+
service: {
15+
provider: {
16+
compiledCloudFormationTemplate: t.context.template
17+
}
18+
}
19+
};
20+
t.context.plugin = new Plugin(t.context.serverless, {
21+
stage: 'test'
22+
});
23+
t.context.plugin.log = sinon.spy();
24+
});
25+
26+
test('removeMapping calls remove', t => {
27+
const mock = sinon.mock(t.context.provider)
28+
29+
mock.expects('request').returns({
30+
promise: () => Promise.resolve()
31+
});
32+
33+
return t.context.plugin.removeMapping('(none)', 'foo.com')
34+
.then(() => {
35+
mock.verify();
36+
t.true(t.context.plugin.log.calledOnce);
37+
});
38+
});

__tests__/utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const test = require('ava');
4+
5+
const Plugin = require('..');
6+
7+
test.beforeEach(t => {
8+
t.context.template = {
9+
Resources: {}
10+
};
11+
t.context.serverless = {
12+
getProvider: () => null,
13+
service: {
14+
provider: {
15+
compiledCloudFormationTemplate: t.context.template
16+
}
17+
}
18+
};
19+
t.context.plugin = new Plugin(t.context.serverless, {
20+
stage: 'test'
21+
});
22+
});
23+
24+
test('finds deployment id', t => {
25+
const id = 'ApiGatewayDeployment12345';
26+
27+
t.context.template.Resources[id] = {
28+
Type: 'AWS::ApiGateway::Deployment',
29+
};
30+
31+
t.true(id === t.context.plugin.getApiGatewayDeploymentId());
32+
});
33+
34+
test('getDomainName string', t => {
35+
t.true('foo' === t.context.plugin.getDomainName('foo'));
36+
});
37+
38+
test('getDomainName object', t => {
39+
t.true('bar' === t.context.plugin.getDomainName({ name: 'bar' }));
40+
});

custom-domain.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const addCustomResource = require('./lib/add-custom-resource');
4+
const removeMapping = require('./lib/remove-mapping');
5+
const utils = require('./lib/utils');
6+
7+
module.exports = class CustomDomain {
8+
9+
constructor(serverless, options) {
10+
this.serverless = serverless;
11+
this.options = options;
12+
this.provider = this.serverless.getProvider('aws');
13+
14+
Object.assign(this,
15+
{ addCustomResource },
16+
{ removeMapping },
17+
utils
18+
);
19+
20+
this.hooks = {
21+
'before:aws:package:finalize:mergeCustomProviderResources': this.beforePackage.bind(this),
22+
'before:remove:remove': this.beforeRemove.bind(this),
23+
};
24+
}
25+
26+
beforePackage() {
27+
const custom = this.serverless.service.custom;
28+
29+
if (custom && custom.domain) {
30+
const domainName = this.getDomainName(custom.domain);
31+
32+
if (domainName) {
33+
const deploymentId = this.getApiGatewayDeploymentId();
34+
35+
if (deploymentId) {
36+
this.addCustomResource(domainName, deploymentId);
37+
} else {
38+
throw new Error('Could not find AWS::ApiGateway::Deployment resource in CloudFormation template!');
39+
}
40+
} else {
41+
throw new Error('custom.domain must either be a string or an object with a name property');
42+
}
43+
}
44+
}
45+
46+
beforeRemove() {
47+
const domain = this.serverless.service.custom.domain;
48+
49+
if (domain) {
50+
const domainName = this.getDomainName(domain);
51+
52+
if (domainName) {
53+
return this.removeMapping(domainName);
54+
}
55+
}
56+
}
57+
58+
};

0 commit comments

Comments
 (0)