-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Application Load Balancer event source #6073
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
<!-- | |||
title: Serverless Framework - AWS Lambda Events - ALB | |||
menuText: Application Load Balancer | |||
menuOrder: 8 | |||
description: Setting up AWS Application Load Balancer events with AWS Lambda via the Serverless Framework | |||
layout: Doc | |||
--> | |||
|
|||
<!-- DOCS-SITE-LINK:START automatically generated --> | |||
### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/aws/events/alb) | |||
<!-- DOCS-SITE-LINK:END --> | |||
|
|||
# Application Load Balancer | |||
|
|||
[Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html) can be used to re-route requests when certain traffic patterns are met. While traffic can be routed to services such as EC2 it [can also be routed to Lambda functions](https://aws.amazon.com/de/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/) which can in turn be used process incoming requests. | |||
|
|||
The Serverless Framework makes it possible to setup the connection between Application Load Balancers and Lamdba functions with the help of the `alb` event. | |||
|
|||
## Event definition | |||
|
|||
```yml | |||
functions: | |||
albEventConsumer: | |||
handler: handler.hello | |||
events: | |||
- alb: | |||
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/ | |||
priority: 1 | |||
conditions: | |||
host: example.com | |||
path: /hello | |||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
'use strict'; | |||
|
|||
const BbPromise = require('bluebird'); | |||
|
|||
const validate = require('./lib/validate'); | |||
const compileTargetGroups = require('./lib/targetGroups'); | |||
const compileListenerRules = require('./lib/listenerRules'); | |||
const compilePermissions = require('./lib/permissions'); | |||
|
|||
class AwsCompileAlbEvents { | |||
constructor(serverless, options) { | |||
this.serverless = serverless; | |||
this.options = options; | |||
this.provider = this.serverless.getProvider('aws'); | |||
|
|||
Object.assign( | |||
this, | |||
validate, | |||
compileTargetGroups, | |||
compileListenerRules, | |||
compilePermissions | |||
); | |||
|
|||
this.hooks = { | |||
'package:compileEvents': () => { | |||
return BbPromise.try(() => { | |||
pmuens marked this conversation as resolved.
Show resolved
Hide resolved
|
|||
this.validated = this.validate(); | |||
if (this.validated.events.length === 0) return; | |||
|
|||
this.compileTargetGroups(); | |||
this.compileListenerRules(); | |||
this.compilePermissions(); | |||
}); | |||
}, | |||
}; | |||
} | |||
} | |||
|
|||
module.exports = AwsCompileAlbEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,83 @@ | |||
'use strict'; | |||
|
|||
const expect = require('chai').expect; | |||
const sinon = require('sinon'); | |||
const AwsProvider = require('../../../../provider/awsProvider'); | |||
const AwsCompileAlbEvents = require('./index'); | |||
const Serverless = require('../../../../../../Serverless'); | |||
|
|||
describe('AwsCompileAlbEvents', () => { | |||
let awsCompileAlbEvents; | |||
|
|||
beforeEach(() => { | |||
const serverless = new Serverless(); | |||
const options = { | |||
stage: 'dev', | |||
region: 'us-east-1', | |||
}; | |||
serverless.setProvider('aws', new AwsProvider(serverless, options)); | |||
awsCompileAlbEvents = new AwsCompileAlbEvents(serverless, options); | |||
}); | |||
|
|||
describe('#constructor()', () => { | |||
let compileTargetGroupsStub; | |||
let compileListenerRulesStub; | |||
let compilePermissionsStub; | |||
|
|||
beforeEach(() => { | |||
compileTargetGroupsStub = sinon | |||
.stub(awsCompileAlbEvents, 'compileTargetGroups').resolves(); | |||
compileListenerRulesStub = sinon | |||
.stub(awsCompileAlbEvents, 'compileListenerRules').resolves(); | |||
compilePermissionsStub = sinon | |||
.stub(awsCompileAlbEvents, 'compilePermissions').resolves(); | |||
}); | |||
|
|||
afterEach(() => { | |||
awsCompileAlbEvents.compileTargetGroups.restore(); | |||
awsCompileAlbEvents.compileListenerRules.restore(); | |||
awsCompileAlbEvents.compilePermissions.restore(); | |||
}); | |||
|
|||
it('should have hooks', () => expect(awsCompileAlbEvents.hooks).to.be.not.empty); | |||
|
|||
it('should set the provider variable to be an instanceof AwsProvider', () => | |||
expect(awsCompileAlbEvents.provider).to.be.instanceof(AwsProvider)); | |||
|
|||
describe('"package:compileEvents" promise chain', () => { | |||
afterEach(() => { | |||
awsCompileAlbEvents.validate.restore(); | |||
}); | |||
|
|||
it('should run the promise chain in order', () => { | |||
const validateStub = sinon | |||
.stub(awsCompileAlbEvents, 'validate').returns({ | |||
events: [{ | |||
functionName: 'first', | |||
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(compileListenerRulesStub.calledAfter(compileTargetGroupsStub)).to.be.equal(true); | |||
expect(compilePermissionsStub.calledAfter(compileListenerRulesStub)).to.be.equal(true); | |||
}); | |||
}); | |||
}); | |||
|
|||
it('should resolve if no functions are given', () => { | |||
awsCompileAlbEvents.serverless.service.functions = {}; | |||
|
|||
return awsCompileAlbEvents.hooks['package:compileEvents'](); | |||
}); | |||
}); | |||
}); |
44 changes: 44 additions & 0 deletions
44
lib/plugins/aws/package/compile/events/alb/lib/listenerRules.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
'use strict'; | |||
|
|||
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, | |||
}, | |||
}, | |||
}); | |||
}); | |||
}, | |||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo here. Should be
./events/alb.md
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I submitted a separate pull request to fix the typo, since this one is already marked as Done.