準備
npm i -g aws-cdk npm i @aws-cdk/aws-dynamodb
実行
mkdir fanzamodb cd fanzamodb cdk init sample-app --language=typescript
lib/fanzamodb-stack.tsを編集
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import { Duration, Stack, StackProps, RemovalPolicy } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as lambda from "@aws-cdk/aws-lambda";
export class FanzamodbStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const table = new dynamodb.Table(this, "Table", {
partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PROVISIONED,
readCapacity: 20,
writeCapacity: 20,
removalPolicy: RemovalPolicy.DESTROY,
sortKey: { name: "createdAt", type: dynamodb.AttributeType.NUMBER },
pointInTimeRecovery: true,
tableClass: dynamodb.TableClass.STANDARD_INFREQUENT_ACCESS,
});
table.addLocalSecondaryIndex({
indexName: "statusIndex",
sortKey: { name: "status", type: dynamodb.AttributeType.STRING },
projectionType: dynamodb.ProjectionType.ALL,
});
const readScaling = table.autoScaleReadCapacity({
minCapacity: 1,
maxCapacity: 50,
});
readScaling.scaleOnUtilization({
targetUtilizationPercent: 50,
});
const globalTable = new dynamodb.Table(this, "Table", {
partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
replicationRegions: ["us-east-1", "us-east-2", "us-west-1"],
billingMode: dynamodb.BillingMode.PROVISIONED,
replicationTimeout: Duration.hours(3),
});
globalTable
.autoScaleWriteCapacity({
minCapacity: 1,
maxCapacity: 10,
})
.scaleOnUtilization({ targetUtilizationPercent: 75 });
}
}
cdk deploy
確認
AWSコンソール->DynamoDB->リージョン ["us-east-1", "us-east-2", "us-west-1"]それぞれにテーブルが作成されていることを確認