2
0

More than 1 year has passed since last update.

[AWS CDK]RDSのInstancePropsとInstancesのDeprecatedに対応する

Last updated at Posted at 2023-07-27

CDKで出力された以下のRDSに関するWARNINGへの対応を調査・検証した結果を共有したいと思います。現状作成されているRDSリソースを傷つけることなく修正する方法が提供されていました。

[WARNING] aws-cdk-lib.aws_rds.DatabaseClusterProps#instanceProps is deprecated.
  - use writer and readers instead
  This API will be removed in the next major release.
[WARNING] aws-cdk-lib.aws_rds.DatabaseClusterProps#instances is deprecated.
  - use writer and readers instead
  This API will be removed in the next major release.

全コードはGithubを参照してください。
Githubはここ

前提条件

  • AWS CDK v.2 がインストールされていること
  • Python 3.x がインストールされていること
  • AWSアカウントがあり、AWS CLIが設定されていること

※Cloud9を使うとこの辺りがPassできるため、Cloud9を使って今回の記事の内容は作成しています。

検証

WARNINGが出たコード

※VPCの作成等は省いております。
以下では、InstancePropsとInstancesを使っており、これらがDeprecatedとなるとのこと。

# RDS Instanceの設定値を定義
instance_props = rds.InstanceProps(
    vpc=vpc,
    allow_major_version_upgrade=False,
    auto_minor_version_upgrade=True,
    instance_type=ec2.InstanceType.of(
        ec2.InstanceClass.BURSTABLE3,
        ec2.InstanceSize.MEDIUM,
    ),
    publicly_accessible=False,
    security_groups=[rds_security_group],
)

# DB Clusterを作成

db_cluster = rds.DatabaseCluster(
    self,
    'AuroraDatabaseCluster',
    engine=rds.DatabaseClusterEngine.aurora_postgres(
        version=rds.AuroraPostgresEngineVersion.VER_14_5
    ),
    instance_props=instance_props,
    cluster_identifier='aurora-postgres-cluster',
    deletion_protection=True,
    instances=1,
    storage_encrypted=True,
)

デプロイやSynthをすると冒頭で紹介したWARNINGが出る。

修正したコード

既存のリソースを傷つけることなくデプロイするために変更を加えたコードです。
大きな変更点としては、インスタンス設定としてwrite=・・・を使っている部分です。今回は一つのインスタンスのみとしていたためwriterのみですが、二つ以上となる場合はreaders=・・・として定義するようです。
また、InstancePropsの設定を引き継ぐため、is_from_legacy_instance_props=Trueを設定しています。
詳細はドキュメントを参照してください。

cluster_name = 'aurora-postgres-cluster'
# DB Cluster作成
db_cluster = rds.DatabaseCluster(
    self,
    'AuroraDatabaseCluster',
    engine=rds.DatabaseClusterEngine.aurora_postgres(
        version=rds.AuroraPostgresEngineVersion.VER_14_5
    ),
    cluster_identifier=cluster_name,
    deletion_protection=True,
    security_groups=[rds_security_group],
    storage_encrypted=True,
    vpc=vpc,
    vpc_subnets=ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS,
    ),
    writer=rds.ClusterInstance.provisioned(
        'Instance1',   # Writer instance must be named 'Instance1'.
        instance_type=ec2.InstanceType.of(
            ec2.InstanceClass.BURSTABLE3,
            ec2.InstanceSize.MEDIUM,
        ),
        is_from_legacy_instance_props=True,    # For migrating existing clusters.
        allow_major_version_upgrade=False,
        auto_minor_version_upgrade=True,
        instance_identifier=f'{cluster_name}instance1',    # 指定しないとリソース作り直しとなる
        publicly_accessible=False,
    )
)

cdk diffで検証

cdk diffコマンドを使って、リソースが変更されていないか確認を行います。

$ cdk diff
Stack ModifyDeprecatedApiStack
There were no differences

No differencesとなっており、変更が無いことが確認できました。
※instance_identifierを明示的に指定しないとリソースが変更されるようになっていました。

デプロイしてみる

diffを使って変更が無いことを確認したため、デプロイをしてみます。

$ cdk deploy --require-approval never

✨  Synthesis time: 13.36s

 ✅  ModifyDeprecatedApiStack (no changes)

✨  Deployment time: 0.43s

デプロイをした場合もNo changesとなっており、問題なく変更できたことが確認できました。

まとめ

この記事では、DeprecatedとなるCDKのAPIについて検証・修正した内容を共有しました。どなたかの参考となれば幸いです。

参考

Github discussions: What does the next major release mean?
CDK API reference: Migrating from instanceProps
CDK API reference: DatabaseCluster
CDK API reference: ClusterInstance

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0