Adding eventSource to Lambda by ARN in CDK

时光总嘲笑我的痴心妄想 提交于 2019-12-13 20:09:11

问题


I have a lambda function which reads from Dynamodb stream. I have the Dynamodb stream ARN exported from another stack in the same AWS account. Now, while adding eventSource in Lambda, it asks from Table construct.

const function = new lambda.Function(...);
function.addEventSource(new DynamoEventSource(table, {
  startingPosition: lambda.StartingPosition.TrimHorizon
}));

Ref: https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-lambda-event-sources.html#dynamodb-streams

But I have the stream ARN. Is there any way I can make use of this to add the event source. Or I have to export the table itself?


回答1:


It's currently not possible to import a DynamoDB table with the AWS CDK.

Still you can reach your goal by using the EventSourceMapping class from @aws-cdk/aws-lambda directly:

import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(...);

new lambda.EventSourceMapping(this, 'DynamoDBEventSource', {
  target: fn,
  batchSize: ...,
  eventSourceArn: <your stream arn>,
  startingPosition: lambda.StartingPosition.TrimHorizon
});

fn.addToRolePolicy(
  new iam.PolicyStatement()
    .addActions('dynamodb:DescribeStream', 'dynamodb:GetRecords', 'dynamodb:GetShardIterator', 'dynamodb:ListStreams')
    .addResource('<your stream arn>/*');
);


来源:https://stackoverflow.com/questions/55768319/adding-eventsource-to-lambda-by-arn-in-cdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!