ã¯ããã«
DynamoDB Streamsããåãã§ããããï¼ç¥ã£ã¦ãã人ã¯å¤ããã¨æãã¾ãããæå¤ã¨ä½¿ã£ããã¨ã®ãã人ã¯å°ãªãããããªããã¨æã£ã¦ãã¾ãã
ã©ãããASé¨DS2課ã®å¹³æ¾ã§ãã
ãã®ããã°ã¯ãµã¼ãã¼ã¯ã¼ã¯ã¹ã¢ããã³ãã«ã¬ã³ãã¼ã®21æ¥ç®ã®æ稿ã§ããææ°æã ã¨ããã°æ¸ãã¾ãã¨æãæãããã®ã®ãæ å½å½æ¥ã«å¿ æ»ã«æ¸ãã¦ãã¾ããç¡äºå ¬éããã¦ãããã¨ãç¥ãã¾ãã
ãã¦ãæ¬é¡ã«å ¥ãã¾ãã DynamoDB Streamsã¯ãç°¡åã«è¨ãã¨DynamoDB ãã¼ãã«å ã®é ç®ã¬ãã«ã®å¤æ´ããã£ããã£ãã¦ããããµã¼ãã¹ã§ãã ã¾ããAWS Lambda ããªã¬ã¼ãä½æãã¦ããã£ã«ã¿ãªã³ã°æ¡ä»¶ãå®ç¾©ãããã¨ã§ãDynamoDB ãã¼ãã«ããã®ã¹ããªã¼ã ã®ä¸é¨ã®ã¤ãã³ãã®ã¿ãLambdaã«å¦çããããã¨ãã§ãã¾ãã
詳細ã¯å ¬å¼ããã¥ã¡ã³ãã覧ãã ããã
ãããªãããªã§ãããã¨ãCDK for TypeScript ã§ããã·ã¹ãã ãæ§ç¯ãã¦ããã¨ãã«ãDynamoDB ã®ããå±æ§ããç¹å®ã®å¤ã«å¤æ´ãããã¨ãã«Lambdaãå®è¡ãããããã«ãããã¨ãªãã¾ããã
ä¼ããã¾ããï¼
å ·ä½ä¾ã§è¨ãã¨ãname ã¨ããå±æ§ã®å¤ããtaro ã«å¤æ´ãããã¨ãã ãLambdaãå®è¡ãããããã«ãããã¨ãããã¨ã§ãã
ããããããã¨ããã¨ãã«å°ãè©°ã¾ã£ãã®ã§ãããèªå以å¤ã«ããå°ãã®æ¹ãããã°ï¼ã¨æããããã°ã«æ®ãã¦ããã¾ãã ã¨ãã«DynamoEventSourceã¯ã©ã¹ã使ç¨ããæ¸ãæ¹ããã¾ãæ¢ãã¦ãè¦ã¤ãããªãã£ãã®ã§ã
æ§æ
è¶ ã·ã³ãã«ã§ãã æ¤è¨¼ã®ããã ããªã®ã§ãDynamoDB ãä½æãã¦ãDynamoDB Streams ãè¨å®ãã¦ããã£ã«ã¿ãªã³ã°æ¡ä»¶ãã¨ã«Lambdaã2ã¤ä½ã£ãã®ã¿ã§ãã
DynamoDBã¯ä»¥ä¸ã®ãããªãã¼ãã«æ§æã§ãitem_rarityå±æ§ã®å¤ãgoldãwhiteã«ããå½¢ã§æ¤è¨¼ãã¾ãã
ã³ã¼ã
/lib/dynamodb-streams-sample-stack.ts
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as dynamodb from "aws-cdk-lib/aws-dynamodb"; import * as lambda from "aws-cdk-lib/aws-lambda"; import * as path from "path"; export class DynamodbStreamsSampleStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // DynamoDB const dynamoTable = new dynamodb.Table( this, `Dynamodb`, { partitionKey: { name: "user_id", type: dynamodb.AttributeType.NUMBER }, sortKey: { name: "user_name", type: dynamodb.AttributeType.STRING }, tableName: `SampleDynamodbStreams`, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, removalPolicy: cdk.RemovalPolicy.DESTROY, stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES, // DynamoDB Streamsãæå¹å } ); // Lambda const goldLambdaForDynamodbStreams = new cdk.aws_lambda_nodejs.NodejsFunction(this, "goldLambda", { entry: path.join( __dirname, `./lambda/goldLambda.ts` ), handler: "handler", functionName: "goldLambda", runtime: lambda.Runtime.NODEJS_18_X, }); // Lambdaã«DynamoDBã®èªã¿åã権éä»ä¸ dynamoTable.grantReadData(goldLambdaForDynamodbStreams); // Lambdaã«ã¤ãã³ãã½ã¼ã¹(DynamoDB Streams)追å goldLambdaForDynamodbStreams.addEventSource( new cdk.aws_lambda_event_sources.DynamoEventSource(dynamoTable, { startingPosition: lambda.StartingPosition.TRIM_HORIZON, filters: [ lambda.FilterCriteria.filter({ eventName: lambda.FilterRule.isEqual("MODIFY"), dynamodb: { NewImage: { item_rarity: { S: lambda.FilterRule.isEqual("gold")}}, }, }), ], batchSize: 1, }) ); // Lambda const whiteLambda2ForDynamodbStreams = new cdk.aws_lambda_nodejs.NodejsFunction(this, "whiteLambda", { entry: path.join( __dirname, `./lambda/whiteLambda.ts` ), handler: "handler", functionName: "whiteLambda", runtime: lambda.Runtime.NODEJS_18_X, }); // Lambdaã«DynamoDBã®èªã¿åã権éä»ä¸ dynamoTable.grantReadData(whiteLambda2ForDynamodbStreams); // Lambdaã«ã¤ãã³ãã½ã¼ã¹(DynamoDB Streams)追å whiteLambda2ForDynamodbStreams.addEventSource( new cdk.aws_lambda_event_sources.DynamoEventSource(dynamoTable, { startingPosition: lambda.StartingPosition.TRIM_HORIZON, filters: [ lambda.FilterCriteria.filter({ eventName: lambda.FilterRule.isEqual("MODIFY"), dynamodb: { NewImage: { item_rarity: { S: lambda.FilterRule.isEqual("white")}}, }, }), ], batchSize: 1, }) ); } }
10ï½21è¡ç®ã§DynamoDBã®è¨å®ããããããã¦ãã¾ãã
ç¹ã«ã19è¡ç®ã®stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES
ã§DynamoDB Streamsãæå¹åãã表示ã¿ã¤ãï¼StreamViewTypeï¼ã§ãæ°æ§ã¤ã¡ã¼ã¸ããé¸æãã¦ãã¾ãã
ããã«ãããDynamoDBãå¤æ´ããã£ããã£ããã¹ããªã¼ã ã«æ¸ãè¾¼ã¾ããéã«ãå¤æ´åã¨å¤æ´å¾ã®ãã¼ã¿ä¸¡æ¹ãæ¸ãè¾¼ã¾ãããã¨ã«ãªãã¾ãã ã¤ã¾ããä»åã®ãããªå ´åãLambdaã«æ¸¡ãããeventãã¼ã¿ã®æ§é ããã©ããªè¡¨ç¤ºã¿ã¤ãï¼StreamViewTypeï¼é¸ã¶ãã«ãã£ã¦å¤ããã¾ãã ï¼è©³ç´°ã¯ãã¡ãï¼
ãã£ã¡ãä»åã®ã¡ã¤ã³ã§ãã 35ï½48è¡ç®ã§Lambdaã«ã¤ãã³ãã½ã¼ã¹ã追å ãã¦ããã£ã«ã¿ãªã³ã°æ¡ä»¶ãå®ç¾©ãã¦ãã¾ãã
filters: [ lambda.FilterCriteria.filter({ eventName: lambda.FilterRule.isEqual("MODIFY"), dynamodb: { NewImage: { item_rarity: { S: lambda.FilterRule.isEqual("gold")}}, }, }), ],
ä¸è¨ã®ãã£ã«ã¿ãªã³ã°æ¡ä»¶ã®è¨è¿°ã§
é ç®ãä¿®æ£ï¼Modifyï¼ãããã¨ãããã¤ãå¤æ´å¾ï¼NewImageï¼ã®item_rarityå±æ§ã®å¤ã"gold"ã ã£ãã¨ãã«Lambdaãå®è¡ãããããã«ãªãã¾ãã
/lib/lambda/goldLambda.ts
export const handler = async (event: any = {}): Promise<any> => { const oldItem = JSON.stringify(event.Records[0].dynamodb.OldImage); const newItem = JSON.stringify(event.Records[0].dynamodb.NewImage); console.log(`Old:${oldItem}`); console.log(`New:${newItem}`); return }
DyanmoDB Streamsãã渡ã£ã¦ããã¹ããªã¼ã ãã¼ã¿ããå¤æ´åã®é ç®ã¨ãå¤æ´å¾ã®é ç®ãæ½åºãã¦åºåãã¦ã¾ãã whiteLambdaãå ¨ãåãã³ã¼ãã§ãã
å®éã«å±æ§ã®å¤å¤æ´ãã£ã¦ã¿ã
goldã«ããã¨ã
goldLambdaã®ãã°ãè¦ã«ããã¨ãã¡ããã¨å¤æ´åã®é
ç®ã®ãã¼ã¿ã¨ãå¤æ´å¾ã®é
ç®ã®ãã¼ã¿ãåãã¦ãã¦ãitem_rarityå±æ§ã®å¤ããwhite
ããgold
ã«ãªã£ã¦ãããã¨ããããã¾ãã
2023-12-21T09:57:57.283Z a65a9363-1195-4fb8-8ec1-eeb94933467e INFO Old:{ "user_id": { "N": "1" }, "user_name": { "S": "sabawa" }, "item_rarity": { "S": "white" } } 2023-12-21T09:57:57.283Z a65a9363-1195-4fb8-8ec1-eeb94933467e INFO New:{ "user_id": { "N": "1" }, "user_name": { "S": "sabawa" }, "item_rarity": { "S": "gold" } }
whiteã«ããã¨ã
whiteLambdaã®ãã°ãè¦ã«ããã¨ããã¡ããã¡ããã¨å¤æ´åã®é ç®ã®ãã¼ã¿ã¨ãå¤æ´å¾ã®é ç®ã®ãã¼ã¿ãåãã¦ã¾ããã
2023-12-21T10:12:57.195Z c3323172-99fb-4213-a4b0-2a9af4f6748b INFO Old:{ "user_id": { "N": "1" }, "user_name": { "S": "sabawa" }, "item_rarity": { "S": "gold" } } 2023-12-21T10:12:57.196Z c3323172-99fb-4213-a4b0-2a9af4f6748b INFO New:{ "user_id": { "N": "1" }, "user_name": { "S": "sabawa" }, "item_rarity": { "S": "white" } }
ãã¾ã
ã¡ãªã¿ã«ã以ä¸ã®ããã«item_rarity : white
ã®é
ç®ãæ°ãã追å ããã¨ãããã©ããªãããããã¾ããï¼
ã¯ããLambdaã¯åãã¾ããããªãããããã¾ããï¼ããã§ããå
ã»ã©èª¬æãããã£ã«ã¿ãªã³ã°ã®æ¡ä»¶ã§"MODIFY"=é
ç®ãä¿®æ£ãããã¨ã
ã¨è¨å®ãã¦ããããã§ãã
ãããæ°è¦é
ç®ä½ææã«ããããã£ããã£ãã¦Lambdaå®è¡ããããå ´åã¯ã"INSERT"=é
ç®ã追å ãããã¨ã
ã¨è¨å®ããå¿
è¦ãããã¾ãã
ä»ã«ããå±æ§å¤ãæ°å¤ã ã£ãå ´åãæ¯è¼æ¼ç®åã使ããããå¤ã®åå¨æç¡ã§ããã£ã«ã¿ãªã³ã°ãããã¨ãã§ãã¾ãã æ§ã ãªãã£ã«ã¿ãªã³ã°æ¡ä»¶ãç¶²ç¾ ã§ããããã°ãä»åº¦æ¸ãã¦ã¿ããã¨æãã¾ãã
æå¾ã«
æå¾ã¾ã§èªãã§ããã ããããã¨ããããã¾ããã ã©ãªãããå©ãã«å°ãã§ãæ £ãã¦ããã°å¹¸ãã§ãï¼
å¼ç¤¾ã¡ã³ãã¼ã¯ä»ã«ãé¢ç½ãè¨äºãããããæ¸ãã¦ããã®ã§ããã²ã¢ããã³ãã«ã¬ã³ãã¼ã®ããã¦ãã£ã¦ãã ããï½
å¹³æ¾ æ¢é¡ (è¨äºä¸è¦§)
22åãæ¥ã åå¼·ä¸ã
å°ãã§ããå½¹ã«ç«ã¦ãæ å ±ãçºä¿¡ã§ããããé å¼µãã¾ã