Upgrade to Pro — share decks privately, control downloads, hide ads and more …

全AWSアカウント×全CDKアプリで LegacyテンプレートをMigrationした話 / A story about Migration of Legacy template with all AWS accounts x all CDK apps

全AWSアカウント×全CDKアプリで LegacyテンプレートをMigrationした話 / A story about Migration of Legacy template with all AWS accounts x all CDK apps

YutaOkoshi

April 08, 2022
Tweet

Other Decks in Programming

Transcript

  1. \ 積極採用中 / 自己紹介
 • 大越 雄太 (Yuta Okoshi) 


    • Twitter: @cossy70461807 
 • 2021年11月にjustincaseのSREチームにJOIN 
 • 趣味:
 サバイバルゲーム
 チンチラのお世話
 2
  2. \ 積極採用中 / 自己紹介
 • 小笠原 寛明
 • justInCase ソフトウェアエンジニア

    
 • 好きなAWSのサービス: CDK! 
 • 👇のChrome拡張の作者です 😀
 3
  3. \ 積極採用中 / 会社紹介
 そのノウハウを元に保険SaaSを提供しています。 
 6 顧客 保険会社 *

    *事業会社や保険代理店 のご利用も可能 プラン選択 本人認証 告知・重要事項説明 会員資格確認 商品ページ(LP) & 申込フォーム 契約参照 異動・解約 決済 契約更新 お客様 ポータル 査定・承認 問合せ 提出書類の参照 (電子データ) 支払記録 保険金 請求フォーム
  4. \ 積極採用中 / ことのはじまり
 v0.8.0 first public release of the

    AWS CDK! 2018/9 v1.18.0 AWS CDK for Java 一般公開! 2019/11 v1.0.0 一般公開! 2019/7 「AWS CDK」のキャッ チアップ開始 2019/2 cdk v0.33をKotlinから 動かすことに成功 2019/6 aws-cdk justincase v2.0.0 aws-cdk-lib is now stable! 2021/12 マイグレーション計画始動 2021/12
  5. \ 積極採用中 / cdkを利用するにはcdk bootstrapを行いCDKToolkitというスタックを
 利用するリージョンにデプロイする必要があります。
 そのCDKToolkitのテンプレートには
 modern templateとlegacy templateの2種類存在します。


    もともとはlegacy templateのみ存在していて後にmodern templateが登場しました。
 2022年4月現在はどちらも生成・利用が可能です。
 
 「DevelopersIO | CDK BootstrapのModern templateで何が変わるのか」 
 https://dev.classmethod.jp/articles/cdk-bootstrap-modern-template/ 
 
 オススメ!
 この問題を解決するために知っておくべきこと

  6. \ 積極採用中 / この問題を解決するために知っておくべきこと
 「AWS Document | What is the

    AWS CDK?」より 
 https://docs.aws.amazon.com/ja_jp/cdk/v2/guide/migrating-v2.html 
 
 cdk v1の初期ではlegacy templateしか利用できないので、 justincaseではlegacy templateでのデプロイが行われていました。 なお、cdk v1のデフォルト設定ではmodern templateを利用したデプロイはサポートしていません
  7. \ 積極採用中 / この問題を解決するために知っておくべきこと
 また、cdk v2ではlegacy templateによるデプロイもサポートされていません。
 なので、cdk v2を利用するためにはmodern templateを利用する必要がありました。


    本文)
 CDK v2 requires that the environments you deploy into be boostrapped using the modern bootstrap stack. 
 the legacy bootstrap stack (the default under v1) is no longer supported. 
 意訳)
 CDK v2 では、デプロイする環境はmodern bootstrap stackを使用してbootstrapする必要があります。 
 legacy templateではCDK v2のデプロイがサポートされなくなりました。 
 
 「AWS Document | What is the AWS CDK?」より 
 https://docs.aws.amazon.com/ja_jp/cdk/v2/guide/migrating-v2.html 
 

  8. \ 積極採用中 / LegacyとModernの共存について
 共存する方法
 cdk deploy前にModernとLegacyのCDKToolkitを作成しておき、
 deploy時にどのCDKToolkitを利用するのかをオプションで指定する方法があります
 以下ドキュメントで手順が指定方法が記載されています
 •

    AWS Cloud Development Kit | Developer Guide > Customizing synthesis
 https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html#bootstrapping-custom-synth
 • AWS CDK Reference | class DefaultStackSynthesizer 
 https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_core.DefaultStackSynthesizer.html

  9. \ 積極採用中 / LegacyとModernの共存について
 共存のメリット・デメリット
 • メリット
 ◦ 既存のv1をほぼそのまま使える
 


    • デメリット
 ◦ リージョンごとにLegacyとModernの2つのCDKToolkitをデプロイする必要がある
 ◦ cdkのオプションでわざわざ指定することが負担

  10. \ 積極採用中 / LegacyとModernの共存について
 完全移行のメリット・デメリット
 • デメリット、短期的に作業が大変
 ◦ 利用するAWSアカウントとリージョンすべての環境でCDKToolkitをmodernへ変更する必要
 ◦

    CDK v1リポジトリすべてでmodernフラグを設定
 
 • メリット
 ◦ 長期的な作業はほぼなし!
 ◦ AWSアカウント作成時のCDKToolkitのキッティングが楽に!
 ◦ エンジニアがcdk deployするときに、余計なオプションを付けないで済む
 ◦ 誤ってv1が使われることを防ぐことで、v2への移行を後押しできる

  11. \ 積極採用中 / cloudformation_includeの便利さについて
 既存スタックのcfnテンプレートをファイルとして管理。
 このソースコードのようにtemplate fileとして読み込めばCDKで利用することが可能になります
 
 https://docs.aws.amazon.com/cdk/api/v1/docs/cloudformation-include-readme.html 


    https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.cloudformation_include-readme.html 
 
 import * as cfn_inc from "@aws-cdk/cloudformation-include"; const template = new cfn_inc.CfnInclude(this, `${env}-amplify-app-xxxx-xxxx-xxxx-frontend`, { templateFile: `template/amplify-app-xxxx-xxxx-xxxx-frontend.yaml`, parameters: { "ENV": env }, });
  12. \ 積極採用中 / cloudformation_includeの便利さについて
 例えば
 cfnを利用しているリポジトリや、 
 エンジニアが行方不明になり管理できなくなってしまったCDKリポジトリなど 
 cloudformation_includeを使えば簡単にダウンタイムなしで移行することができる!

    
 
 環境ごとにパラメーターや設定値を 
 オーバライドすることも可能! 
 import * as cfn_inc from "@aws-cdk/cloudformation-include"; const template = new cfn_inc.CfnInclude(this, `${env}-amplify-app-xxxx-xxxx-xxxx-frontend`, { templateFile: `template/amplify-app-xxxx-xxxx-xxxx-frontend.yaml`, parameters: {"ENV": env}, }); const domain = template.getResource(`amplifyappxxxxxxxxfrontenddomain`) as amplify.CfnDomain; const amplifyApp = template.getResource(`amplifyappxxxxxxxxfrontend75681723`) as amplify.CfnApp; switch (env) { case 'demo': domain.domainName = `xxxxxxxx.${env}.justincase.jp`; break; case 'dev': domain.domainName = `xxxxxxxx.${env}.justincase.jp`; amplifyApp.autoBranchCreationConfig = {enablePullRequestPreview: true}; break; }
  13. \ 積極採用中 / Bootstrapするときのtemplateバージョンについて
 - AWS CDK Reference | The

    bootstrapping template contract>Template historyより 
 - https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html#bootstrapping-contract 
 bootstrapを実行するCDK CLIのバージョンによって 
 展開されるmodern templateの内容が異なります! 

  14. \ 積極採用中 / Bootstrapするときのtemplateバージョンについて
 v2で生成したcfnテンプレートには、modern templateのバージョンが6以上でないとエラーになるRuleが追加さ れています!
 (2022/04/06時点)
 追加されるRule:
 "Parameters":

    { "BootstrapVersion": { "Type": "AWS::SSM::Parameter::Value<String>", "Default": "/cdk-bootstrap/hnb659fds/version", "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" } }, "Rules": { "CheckBootstrapVersion": { "Assertions": [ { "Assert": { "Fn::Not": [ { "Fn::Contains": [["1","2","3","4","5"],{"Ref": "BootstrapVersion"}] } ] }, "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." } ] } }