问题
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