0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GenUのバックエンド (CDK) 詳細解説 ②AWS CDKの動作確認

Last updated at Posted at 2025-03-11

はじめに

皆さん、こんにちは。

私は業務でデータ利活用基盤を取り扱っていること、2024 AWS Japan Top Engineer に選出されたということから、AWS GenU およびそれに必要なデータ基盤の探求 (Snowflake, dbt, Iceberg, etc) に取り組む必要があると考えています。

本投稿では、GenU のバックエンドである CDK コードを詳細に解説します。
自身そして閲覧して頂いた皆様の GenU への理解が少しでも深まり、生成 AI の民主化につながっていければと考えています。

前回までのおさらい

前回までで、以下が完了しました。

今回は CDK の動作確認をしたいと思います。

CDK の記法(レイヤー)について

CDK にはレイヤーという概念があります。

  • L1は CloudFormation 相当の記述です。
  • L2は L1 から CloudFormation 構文の共通的な記載を排除し、よりユーザライクでわかりやすい記述となります。最も広く使用されているレイヤータイプとのことです。
  • L3は特定のユースケースに即した、複数の AWS リソースを構築する記法と読み取りました。

動作確認用 CDK ファイルの定義

以下は L2 での S3 バケット作成の記載例です。
lib/cdk_tutorial-stack.ts に、以下のように S3 バケットを定義します。

lib/cdk_tutorial-stack.ts
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

export class CdkTutorialStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const accountId = cdk.Stack.of(this).account; // AWSアカウントID

    new s3.Bucket(this, 'MyBucket', {
      bucketName: `my-cdk-app-bucket-${accountId}`,
      versioned: true, // バージョニングを有効化
      removalPolicy: cdk.RemovalPolicy.DESTROY, // スタック削除時にバケットを削除
      autoDeleteObjects: true, // バケット削除時に中身を削除
    });
  }
}

CDK の動作確認

バケットを AWS にデプロイするには、以下のコマンドを実行します。

cdk synth # or npm run cdk synth
cdk deploy # or npm run cdk deploy

cdk deploy でエラーが発生しました。

current credentials could not be used to assume 'arn:aws:iam::xxxxxxxxxxxx:role/cdk-hnb659fds-deploy-role-xxxxxxxxxxxx-ap-northeast-1', but are for the right account. Proceeding anyway.

AWS CDK のブートストラップができていなかったようです。

aws cloudformation list-stacks --query "StackSummaries[?StackName=='CDKToolkit']" # 空
cdk bootstrap aws://[AWSアカウントID]/ap-northeast-1

CDKToolkit スタックが作成されました。

image.png

再度cdk deploy を行ったところ、AWS アカウント ID が取得できていないというエラーが発生しました。
env を明示的に指定します。

bin/cdk_tutorial.ts
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkTutorialStack } from '../lib/cdk_tutorial-stack';

const app = new cdk.App();
new CdkTutorialStack(app, 'CdkTutorialStack', {
  /* If you don't specify 'env', this stack will be environment-agnostic.
   * Account/Region-dependent features and context lookups will not work,
   * but a single synthesized template can be deployed anywhere. */
  /* Uncomment the next line to specialize this stack for the AWS Account
   * and Region that are implied by the current CLI configuration. */
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION,
  },
  /* Uncomment the next line if you know exactly what Account and Region you
   * want to deploy the stack to. */
  // env: { account: '123456789012', region: 'us-east-1' },
  /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});

再度cdk deploy を行ったところ、エラーが解消しました。
無事に CdkTutorialStack が作成されています。

image.png

aws s3 ls | grep my-cdk-app-bucket # my-cdk-app-bucket-${accountId}

これにて動作確認は終了です。
次回からはいよいよ GenU の解説に入っていきます。

(参考) GenU のバックエンド (CDK) 詳細解説投稿一覧

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?