-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipes-targets): add lambda function
- Loading branch information
Showing
20 changed files
with
33,905 additions
and
2 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
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 './lambda'; | ||
export * from './sqs'; | ||
export * from './stepfunctions'; |
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,79 @@ | ||
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; | ||
import { IRole } from 'aws-cdk-lib/aws-iam'; | ||
import * as lambda from 'aws-cdk-lib/aws-lambda'; | ||
|
||
/** | ||
* Parameters for the LambdaFunction target | ||
*/ | ||
export interface LambdaFunctionParameters { | ||
|
||
/** | ||
* The input transformation to apply to the message before sending it to the target. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate | ||
* @default none | ||
*/ | ||
readonly inputTransformation?: IInputTransformation; | ||
|
||
/** | ||
* Specify whether to invoke the Lambda Function synchronously (`REQUEST_RESPONSE`) or asynchronously (`FIRE_AND_FORGET`). | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetlambdafunctionparameters.html | ||
* @default LambdaFunctionInvocationType.REQUEST_RESPONSE | ||
*/ | ||
readonly invocationType?: LambdaFunctionInvocationType; | ||
} | ||
|
||
/** | ||
* InvocationType for invoking the Lambda Function. | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetlambdafunctionparameters.html | ||
*/ | ||
export enum LambdaFunctionInvocationType { | ||
/** | ||
* Invoke Lambda Function asynchronously (`Invoke`). `InvocationType` is set to `Event` on `Invoke`, see https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html for more details. | ||
*/ | ||
FIRE_AND_FORGET = 'FIRE_AND_FORGET', | ||
|
||
/** | ||
* Invoke Lambda Function synchronously (`Invoke`) and wait for the response. `InvocationType` is set to `RequestResponse` on `Invoke`, see https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html for more details. | ||
*/ | ||
REQUEST_RESPONSE = 'REQUEST_RESPONSE', | ||
} | ||
|
||
/** | ||
* An EventBridge Pipes target that sends messages to an AWS Lambda Function. | ||
*/ | ||
export class LambdaFunction implements ITarget { | ||
public readonly targetArn: string; | ||
|
||
private readonly lambdaFunction: lambda.IFunction; | ||
private readonly invocationType: LambdaFunctionInvocationType; | ||
private readonly inputTemplate?: IInputTransformation; | ||
|
||
constructor( | ||
lambdaFunction: lambda.IFunction, | ||
parameters: LambdaFunctionParameters, | ||
) { | ||
this.lambdaFunction = lambdaFunction; | ||
this.targetArn = lambdaFunction.functionArn; | ||
this.invocationType = | ||
parameters.invocationType ?? | ||
LambdaFunctionInvocationType.REQUEST_RESPONSE; | ||
this.inputTemplate = parameters.inputTransformation; | ||
} | ||
|
||
grantPush(grantee: IRole): void { | ||
this.lambdaFunction.grantInvoke(grantee); | ||
} | ||
|
||
bind(pipe: IPipe): TargetConfig { | ||
return { | ||
targetParameters: { | ||
inputTemplate: this.inputTemplate?.bind(pipe).inputTemplate, | ||
lambdaFunctionParameters: { | ||
invocationType: this.invocationType, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
packages/@aws-cdk/aws-pipes-targets-alpha/test/__snapshots__/lambda.test.ts.snap
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`lambda-function should grant lambda function invoke 1`] = ` | ||
{ | ||
"MyLambdaPipeRoleEF32F0E5": { | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "pipes.amazonaws.com", | ||
}, | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
}, | ||
"Type": "AWS::IAM::Role", | ||
}, | ||
} | ||
`; |
14 changes: 14 additions & 0 deletions
14
packages/@aws-cdk/aws-pipes-targets-alpha/test/integ.lambda.handler/handler.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,14 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { LambdaClient, TagResourceCommand } from '@aws-sdk/client-lambda'; | ||
|
||
exports.handler = async (event: any, context: any) => { | ||
const client = new LambdaClient(); | ||
const input = { | ||
Resource: context.invokedFunctionArn, | ||
Tags: { | ||
Identifier: event[0].body, // event is received in batches, we just take the first message to update the tag. See https://docs.aws.amazon.com/eventbridge/latest/userguide/pipes-targets-specifics.html | ||
}, | ||
}; | ||
const command = new TagResourceCommand(input); | ||
await client.send(command); | ||
}; |
1 change: 1 addition & 0 deletions
1
...set.6f50ad1670be7195f01a64eddc86541c357b2087e992904bdcb9f5ae67c82ecb.handler/handler.d.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
...asset.6f50ad1670be7195f01a64eddc86541c357b2087e992904bdcb9f5ae67c82ecb.handler/handler.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
...asset.6f50ad1670be7195f01a64eddc86541c357b2087e992904bdcb9f5ae67c82ecb.handler/handler.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.