From c3f4dfe09030ec02892908df0c2f44c707504ca0 Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Tue, 10 Mar 2020 08:44:53 +0000 Subject: [PATCH] added IEventSourceMapping --- .../aws-lambda/lib/event-source-mapping.ts | 28 +++++++++++++++++-- packages/@aws-cdk/aws-lambda/package.json | 1 - .../test/test.event-source-mapping.ts | 11 +++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index 99e05e61d345f..a32a8ffd9b23b 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -93,6 +93,9 @@ export interface EventSourceMappingOptions { readonly parallelizationFactor?: number; } +/** + * Properties for declaring a new event source mapping. + */ export interface EventSourceMappingProps extends EventSourceMappingOptions { /** * The target AWS Lambda function. @@ -100,6 +103,18 @@ export interface EventSourceMappingProps extends EventSourceMappingOptions { readonly target: IFunction; } +/** + * Represents an event source mapping for a lambda function. + * @see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventsourcemapping.html + */ +export interface IEventSourceMapping extends cdk.IResource { + /** + * The identifier for this EventSourceMapping + * @attribute + */ + readonly eventSourceMappingId: string; +} + /** * Defines a Lambda EventSourceMapping resource. * @@ -112,11 +127,18 @@ export interface EventSourceMappingProps extends EventSourceMappingOptions { * The `SqsEventSource` class will automatically create the mapping, and will also * modify the Lambda's execution role so it can consume messages from the queue. */ -export class EventSourceMapping extends cdk.Resource { +export class EventSourceMapping extends cdk.Resource implements IEventSourceMapping { + /** - * The identifier for this EventSourceMapping - * @attribute + * Import an event source into this stack from its event source id. */ + public static fromEventSourceMappingId(scope: cdk.Construct, id: string, eventSourceMappingId: string): IEventSourceMapping { + class Import extends cdk.Resource implements IEventSourceMapping { + public readonly eventSourceMappingId = eventSourceMappingId; + } + return new Import(scope, id); + } + public readonly eventSourceMappingId: string; constructor(scope: cdk.Construct, id: string, props: EventSourceMappingProps) { diff --git a/packages/@aws-cdk/aws-lambda/package.json b/packages/@aws-cdk/aws-lambda/package.json index 91c1b317dfd5b..8064ad98ffa18 100644 --- a/packages/@aws-cdk/aws-lambda/package.json +++ b/packages/@aws-cdk/aws-lambda/package.json @@ -164,7 +164,6 @@ "props-default-doc:@aws-cdk/aws-lambda.CodeConfig.inlineCode", "props-default-doc:@aws-cdk/aws-lambda.CodeConfig.s3Location", "docs-public-apis:@aws-cdk/aws-lambda.EventSourceMappingOptions", - "docs-public-apis:@aws-cdk/aws-lambda.EventSourceMappingProps", "props-default-doc:@aws-cdk/aws-lambda.FunctionAttributes.role", "props-default-doc:@aws-cdk/aws-lambda.FunctionAttributes.securityGroup", "props-default-doc:@aws-cdk/aws-lambda.FunctionAttributes.securityGroupId", diff --git a/packages/@aws-cdk/aws-lambda/test/test.event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/test/test.event-source-mapping.ts index 16e5f41aac1e9..3de9bbfeb5dab 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.event-source-mapping.ts @@ -142,5 +142,14 @@ export = { }), /parallelizationFactor must be between 1 and 10 inclusive, got 11/); test.done(); - } + }, + + 'import event source mapping'(test: Test) { + const stack = new cdk.Stack(undefined, undefined, { stackName: 'test-stack' }); + const imported = EventSourceMapping.fromEventSourceMappingId(stack, 'imported', '14e0db71-5d35-4eb5-b481-8945cf9d10c2'); + + test.equals(imported.eventSourceMappingId, '14e0db71-5d35-4eb5-b481-8945cf9d10c2'); + test.equals(imported.stack.stackName, 'test-stack'); + test.done(); + }, };