Ask Your Question
4

How can I configure a documentDB cluster in an aws-cdk TypeScript file and use it as an event source via EventSourceMapping?

asked 2022-05-07 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-08 00:00:00 +0000

lalupa gravatar image
  1. Import the necessary AWS SDK packages:
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as eventSources from 'aws-cdk-lib/aws-lambda-event-sources';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
  1. Create a new documentDB cluster:
const cluster = new documentdb.CfnDBCluster(this, 'Cluster', {
    dbClusterIdentifier: 'myCluster',
    engine: 'docdb',
    masterUsername: 'myUsername',
    masterUserPassword: 'myPassword',
    backtrackWindow: 0,
    deletionProtection: false,
    backupRetentionPeriod: 1,
    preferredBackupWindow: '07:00-09:00',
    dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
    vpcSecurityGroupIds: [securityGroup.securityGroupId]
});
  1. Create a new DynamoDB table:
const table = new dynamodb.Table(this, 'Table', {
    partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
    sortKey: { name: 'sk', type: dynamodb.AttributeType.STRING },
    billingMode: dynamodb.BillingMode.PAY_PER_REQUEST
});
  1. Create a new Lambda function:
const fn = new lambda.Function(this, 'Function', {
    runtime: lambda.Runtime.NODEJS_12_X,
    handler: 'index.handler',
    code: lambda.Code.fromAsset('lambda'),
    environment: {
        TABLE_NAME: table.tableName
    },
    timeout: cdk.Duration.seconds(30)
});
  1. Add an event source mapping to the Lambda function:
const mapping = new eventSources.DynamoEventSource(table, {
    startingPosition: lambda.StartingPosition.TRIM_HORIZON,
    batchSize: 100
});

fn.addEventSourceMapping('Mapping', mapping);
  1. Add a VPC, subnets, and security group to the cluster:
const vpc = new ec2.Vpc(this, 'Vpc', {
    cidr: '10.0.0.0/16',
    maxAzs: 2,
    subnetConfiguration: [
        { name: 'public', subnetType: ec2.SubnetType.PUBLIC }
    ]
});

const subnetGroup = new documentdb.CfnDBSubnetGroup(this, 'SubnetGroup', {
    dbSubnetGroupName: 'mySubnetGroup',
    subnetIds: vpc.publicSubnets.map(subnet => subnet.subnetId)
});

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
    vpc,
    allowAllOutbound: true
});

securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(27017));
  1. Add the cluster to the VPC:
cluster.addOverride('Properties.VpcSecurityGroupIds', [securityGroup.securityGroupId]);
  1. Deploy everything using the AWS CDK:
cdk deploy
  1. Use the Lambda function as an event source in other parts of your AWS infrastructure:
const myLambda = new lambda.Function(stack, 'MyLambda', { ... });

myLambda.addEventSourceMapping('Mapping', {
    batchSize: 10,
    eventSourceArn: mapping.eventSourceArn,
    function: myLambda
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-07 11:00:00 +0000

Seen: 8 times

Last updated: Aug 08 '21