forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(events-targets): LambdaFunction (aws#2350)
The `LambdaFunction` class can be used to bind an AWS Lambda function as an event rule target. Related aws#1663 BREAKING CHANGE: `lambda.Function` no longer implements `IEventRuleTarget`. Instead, use `@aws-cdk/aws-events-targets.LambdaFunction`.
- Loading branch information
1 parent
33b67f2
commit 6850a0b
Showing
9 changed files
with
117 additions
and
79 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './sns'; | ||
export * from './codebuild'; | ||
export * from './codebuild'; | ||
export * from './lambda'; |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import events = require('@aws-cdk/aws-events'); | ||
import iam = require('@aws-cdk/aws-iam'); | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
|
||
/** | ||
* Use an AWS Lambda function as an event rule target. | ||
*/ | ||
export class LambdaFunction implements events.IEventRuleTarget { | ||
|
||
/** | ||
* @param handler The lambda function | ||
*/ | ||
constructor(private readonly handler: lambda.IFunction) { | ||
|
||
} | ||
|
||
/** | ||
* Returns a RuleTarget that can be used to trigger this Lambda as a | ||
* result from a CloudWatch event. | ||
*/ | ||
public asEventRuleTarget(ruleArn: string, ruleId: string): events.EventRuleTargetProps { | ||
const permissionId = `AllowEventRule${ruleId}`; | ||
if (!this.handler.node.tryFindChild(permissionId)) { | ||
this.handler.addPermission(permissionId, { | ||
action: 'lambda:InvokeFunction', | ||
principal: new iam.ServicePrincipal('events.amazonaws.com'), | ||
sourceArn: ruleArn | ||
}); | ||
} | ||
|
||
return { | ||
id: this.handler.node.id, | ||
arn: this.handler.functionArn, | ||
}; | ||
} | ||
} |
File renamed without changes.
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
62 changes: 62 additions & 0 deletions
62
packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { countResources, expect, haveResource } from '@aws-cdk/assert'; | ||
import events = require('@aws-cdk/aws-events'); | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import targets = require('../../lib'); | ||
|
||
test('use lambda as an event rule target', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
const fn = newTestLambda(stack); | ||
const rule1 = new events.EventRule(stack, 'Rule', { scheduleExpression: 'rate(1 minute)' }); | ||
const rule2 = new events.EventRule(stack, 'Rule2', { scheduleExpression: 'rate(5 minutes)' }); | ||
|
||
// WHEN | ||
rule1.addTarget(new targets.LambdaFunction(fn)); | ||
rule2.addTarget(new targets.LambdaFunction(fn)); | ||
|
||
// THEN | ||
const lambdaId = "MyLambdaCCE802FB"; | ||
|
||
expect(stack).to(haveResource('AWS::Lambda::Permission', { | ||
Action: "lambda:InvokeFunction", | ||
FunctionName: { | ||
"Fn::GetAtt": [ | ||
lambdaId, | ||
"Arn" | ||
] | ||
}, | ||
Principal: "events.amazonaws.com", | ||
SourceArn: { "Fn::GetAtt": ["Rule4C995B7F", "Arn"] } | ||
})); | ||
|
||
expect(stack).to(haveResource('AWS::Lambda::Permission', { | ||
Action: "lambda:InvokeFunction", | ||
FunctionName: { | ||
"Fn::GetAtt": [ | ||
lambdaId, | ||
"Arn" | ||
] | ||
}, | ||
Principal: "events.amazonaws.com", | ||
SourceArn: { "Fn::GetAtt": ["Rule270732244", "Arn"] } | ||
})); | ||
|
||
expect(stack).to(countResources('AWS::Events::Rule', 2)); | ||
expect(stack).to(haveResource('AWS::Events::Rule', { | ||
Targets: [ | ||
{ | ||
Arn: { "Fn::GetAtt": [lambdaId, "Arn"] }, | ||
Id: "MyLambda" | ||
} | ||
] | ||
})); | ||
}); | ||
|
||
function newTestLambda(scope: cdk.Construct) { | ||
return new lambda.Function(scope, 'MyLambda', { | ||
code: new lambda.InlineCode('foo'), | ||
handler: 'bar', | ||
runtime: lambda.Runtime.Python27 | ||
}); | ||
} |
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