概要
Auth.jsでDynamoDBアダプタを利用を検証。
実装サンプルが想定と異なるデフォルト値で、うっかり起動してると課金されてしまうのでメモ。
実装
Auth.jsのサンプル
DynamoDB
Authentication for the Web
new dynamodb.Table(this, `NextAuthTable`, {
tableName: "next-auth",
partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
timeToLiveAttribute: "expires",
}).addGlobalSecondaryIndex({
indexName: "GSI1",
partitionKey: { name: "GSI1PK", type: dynamodb.AttributeType.STRING },
sortKey: { name: "GSI1SK", type: dynamodb.AttributeType.STRING },
});
dynamodb.Table
を利用していることが注意点。
こちらはオンデマンドではなくプロビジョンがデフォルトとなっている。
v2のノリでやるとうっかり課金される。
Table
docs.aws.amazon.com
Type: BillingMode (optional,
default: PROVISIONED
if replicationRegions is not specified, PAY_PER_REQUEST otherwise)
TableV2
docs.aws.amazon.com
Type: Billing (optional,
default: Billing.onDemand()
)
デフォルト値が変わることはないだろうと思ったのだけど、より利用者目線で使いやすいように変わっているのかもしれない。
作成したコンストラクタ
ほぼ変わってないけど TableV2
を利用し、削除ポリシーとオンデマンドを明示する形で修正。
lib\constractor\dynamodb.ts
import { aws_dynamodb, RemovalPolicy } from "aws-cdk-lib";
import { Construct } from "constructs";
export class DynamoDbConstruct extends Construct {
public readonly table: aws_dynamodb.TableV2;
constructor(scope: Construct, id: string) {
super(scope, id);
new aws_dynamodb.TableV2(this, `NextAuthTable`, {
tableName: "next-auth",
partitionKey: { name: "pk", type: aws_dynamodb.AttributeType.STRING },
sortKey: { name: "sk", type: aws_dynamodb.AttributeType.STRING },
timeToLiveAttribute: "expires",
billing: aws_dynamodb.Billing.onDemand(),
removalPolicy: RemovalPolicy.DESTROY,
}).addGlobalSecondaryIndex({
indexName: "GSI1",
partitionKey: { name: "GSI1PK", type: aws_dynamodb.AttributeType.STRING },
sortKey: { name: "GSI1SK", type: aws_dynamodb.AttributeType.STRING },
});
}
}