Skip to content

Commit

Permalink
Update code to use an existing listener
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed May 30, 2019
1 parent 3b097b3 commit 21e0d93
Show file tree
Hide file tree
Showing 35 changed files with 357 additions and 304 deletions.
25 changes: 5 additions & 20 deletions docs/providers/aws/events/alb.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,15 @@ The Serverless Framework makes it possible to setup the connection between Appli

## Event definition

The following code will setup an alb using a HTTPS-based listener.

```yml
functions:
albEventConsumer:
handler: handler.hello
events:
- alb:
loadBalancerArn: arn:aws:elasticloadbalancing:us-east-1:123456:loadbalancer/app/my-load-balancer/50dc6c495c0c9188
certificateArn: arn:aws:iam::123456:server-certificate/ProdServerCert
name: alb-handler-https
listener: HTTPS:443
```
The Application Load Balancer `arn` can also be referenced via `Ref`. HTTP listeners don't require the `certificateArn` property to be set.

```yml
functions:
albEventConsumer:
handler: handler.hello
events:
- alb:
loadBalancerArn:
Ref: MyApplicationLoadBalancer
name: alb-handler-http
listener: HTTP:80
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/
priority: 1
conditions:
host: example.com
path: /hello
```
9 changes: 5 additions & 4 deletions docs/providers/aws/guide/serverless.yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,11 @@ functions:
pool: MyUserPool
trigger: PreSignUp
- alb:
loadBalancerArn: arn:aws:elasticloadbalancing:us-east-1:XXXXXX:loadbalancer/app/my-load-balancer/50dc6c495c0c9188
certificateArn: arn:aws:iam::XXXXXX:server-certificate/ProdServerCert # only required when HTTPS is used
name: alb-handler-https
listener: HTTPS:443
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/
priority: 1
conditions:
host: example.com
path: /hello

layers:
hello: # A Lambda layer
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/aws/lib/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ module.exports = {
getAlbTargetGroupLogicalId(functionName) {
return `${this.getNormalizedFunctionName(functionName)}AlbTargetGroup`;
},
getAlbListenerLogicalId(functionName, idx) {
return `${this.getNormalizedFunctionName(functionName)}AlbListener${idx}`;
getAlbListenerRuleLogicalId(functionName, idx) {
return `${this.getNormalizedFunctionName(functionName)}AlbListenerRule${idx}`;
},

// Permissions
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/aws/lib/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,10 @@ describe('#naming()', () => {
});
});

describe('#getAlbListenerLogicalId()', () => {
describe('#getAlbListenerRuleLogicalId()', () => {
it('should normalize the function name and add an index', () => {
expect(sdk.naming.getAlbListenerLogicalId('functionName', 0))
.to.equal('FunctionNameAlbListener0');
expect(sdk.naming.getAlbListenerRuleLogicalId('functionName', 0))
.to.equal('FunctionNameAlbListenerRule0');
});
});
});
6 changes: 3 additions & 3 deletions lib/plugins/aws/package/compile/events/alb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const BbPromise = require('bluebird');

const validate = require('./lib/validate');
const compileTargetGroups = require('./lib/targetGroups');
const compileListeners = require('./lib/listeners');
const compileListenerRules = require('./lib/listenerRules');
const compilePermissions = require('./lib/permissions');

class AwsCompileAlbEvents {
Expand All @@ -17,7 +17,7 @@ class AwsCompileAlbEvents {
this,
validate,
compileTargetGroups,
compileListeners,
compileListenerRules,
compilePermissions
);

Expand All @@ -31,7 +31,7 @@ class AwsCompileAlbEvents {

return BbPromise.bind(this)
.then(this.compileTargetGroups)
.then(this.compileListeners)
.then(this.compileListenerRules)
.then(this.compilePermissions);
},
};
Expand Down
29 changes: 15 additions & 14 deletions lib/plugins/aws/package/compile/events/alb/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ describe('AwsCompileAlbEvents', () => {

describe('#constructor()', () => {
let compileTargetGroupsStub;
let compileListenersStub;
let compileListenerRulesStub;
let compilePermissionsStub;

beforeEach(() => {
compileTargetGroupsStub = sinon
.stub(awsCompileAlbEvents, 'compileTargetGroups').resolves();
compileListenersStub = sinon
.stub(awsCompileAlbEvents, 'compileListeners').resolves();
compileListenerRulesStub = sinon
.stub(awsCompileAlbEvents, 'compileListenerRules').resolves();
compilePermissionsStub = sinon
.stub(awsCompileAlbEvents, 'compilePermissions').resolves();
});

afterEach(() => {
awsCompileAlbEvents.compileTargetGroups.restore();
awsCompileAlbEvents.compileListeners.restore();
awsCompileAlbEvents.compileListenerRules.restore();
awsCompileAlbEvents.compilePermissions.restore();
});

Expand All @@ -52,22 +52,23 @@ describe('AwsCompileAlbEvents', () => {
it('should run the promise chain in order', () => {
const validateStub = sinon
.stub(awsCompileAlbEvents, 'validate').returns({
events: [
{
functionName: 'first',
listener: 'HTTPS:443',
loadBalancerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456:loadbalancer/app/my-load-balancer/50dc6c495c0c9188', // eslint-disable-line
certificateArn: 'arn:aws:iam::123456:server-certificate/ProdServerCert',
name: 'some-alb-event-1',
events: [{
functionName: 'first',
// eslint-disable-next-line max-len
listenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2',
priority: 1,
conditions: {
host: 'example.com',
path: '/hello',
},
],
}],
});

return awsCompileAlbEvents.hooks['package:compileEvents']().then(() => {
expect(validateStub.calledOnce).to.be.equal(true);
expect(compileTargetGroupsStub.calledAfter(validateStub)).to.be.equal(true);
expect(compileListenersStub.calledAfter(compileTargetGroupsStub)).to.be.equal(true);
expect(compilePermissionsStub.calledAfter(compileListenersStub)).to.be.equal(true);
expect(compileListenerRulesStub.calledAfter(compileTargetGroupsStub)).to.be.equal(true);
expect(compilePermissionsStub.calledAfter(compileListenerRulesStub)).to.be.equal(true);
});
});
});
Expand Down
48 changes: 48 additions & 0 deletions lib/plugins/aws/package/compile/events/alb/lib/listenerRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const BbPromise = require('bluebird');

module.exports = {
compileListenerRules() {
this.validated.events.forEach((event) => {
const listenerRuleLogicalId = this.provider.naming
.getAlbListenerRuleLogicalId(event.functionName, event.priority);
const targetGroupLogicalId = this.provider.naming
.getAlbTargetGroupLogicalId(event.functionName);

const Conditions = [
{
Field: 'path-pattern',
Values: [event.conditions.path],
},
];
if (event.conditions.host) {
Conditions.push({
Field: 'host-header',
Values: [event.conditions.host],
});
}

Object.assign(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
[listenerRuleLogicalId]: {
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule',
Properties: {
Actions: [
{
Type: 'forward',
TargetGroupArn: {
Ref: targetGroupLogicalId,
},
},
],
Conditions,
ListenerArn: event.listenerArn,
Priority: event.priority,
},
},
});
});

return BbPromise.resolve();
},
};
105 changes: 105 additions & 0 deletions lib/plugins/aws/package/compile/events/alb/lib/listenerRules.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

const expect = require('chai').expect;
const AwsCompileAlbEvents = require('../index');
const Serverless = require('../../../../../../../Serverless');
const AwsProvider = require('../../../../../provider/awsProvider');

describe('#compileListenerRules()', () => {
let awsCompileAlbEvents;

beforeEach(() => {
const serverless = new Serverless();
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };

awsCompileAlbEvents = new AwsCompileAlbEvents(serverless);
});

it('should create ELB listener rule resources', () => {
awsCompileAlbEvents.validated = {
events: [
{
functionName: 'first',
// eslint-disable-next-line max-len
listenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2',
priority: 1,
conditions: {
host: 'example.com',
path: '/hello',
},
},
{
functionName: 'second',
// eslint-disable-next-line max-len
listenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2',
priority: 2,
conditions: {
path: '/world',
},
},
],
};

return awsCompileAlbEvents.compileListenerRules().then(() => {
const resources = awsCompileAlbEvents.serverless.service.provider
.compiledCloudFormationTemplate.Resources;

expect(resources.FirstAlbListenerRule1).to.deep.equal({
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule',
Properties: {
Actions: [
{
TargetGroupArn: {
Ref: 'FirstAlbTargetGroup',
},
Type: 'forward',
},
],
Conditions: [
{
Field: 'path-pattern',
Values: [
'/hello',
],
},
{
Field: 'host-header',
Values: [
'example.com',
],
},
],
// eslint-disable-next-line max-len
ListenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2',
Priority: 1,
},
});
expect(resources.SecondAlbListenerRule2).to.deep.equal({
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule',
Properties: {
Actions: [
{
TargetGroupArn: {
Ref: 'SecondAlbTargetGroup',
},
Type: 'forward',
},
],
Conditions: [
{
Field: 'path-pattern',
Values: [
'/world',
],
},
],
// eslint-disable-next-line max-len
ListenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2',
Priority: 2,
},
});
});
});
});
52 changes: 0 additions & 52 deletions lib/plugins/aws/package/compile/events/alb/lib/listeners.js

This file was deleted.

Loading

0 comments on commit 21e0d93

Please sign in to comment.