これは AWS CDK v2 デベロッパーガイドです。旧版の CDK v1 は 2022 年 6 月 1 日にメンテナンスを開始し、2023 年 6 月 1 日にサポートを終了しました。
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
高度な CDK Toolkit Library の例
実用的な例を使用して、 AWS CDK Toolkit Library の高度な機能を使用する方法について説明します。このガイドでは、他のセクションで説明されている基本概念に基づいて構築された、エラー処理、デプロイモニタリング、クラウドアセンブリ管理の詳細なコードサンプルを提供します。
機能の統合
次の例は、クラウドアセンブリソース、カスタム io ホスト実装、デプロイオプションを組み合わせる方法を示しています。
import { Toolkit, StackSelectionStrategy, IIoHost } from '@aws-cdk/toolkit-lib'; async function deployApplication(appPath, environment, options = {}) { // Create toolkit with custom message handling const toolkit = new Toolkit({ ioHost: { notify: async (msg) => { // Add environment to all messages console.log(`[${environment}][${msg.time}] ${msg.level}: ${msg.message}`); }, requestResponse: async (msg) => { // In production environments, use default responses if (environment === 'production') { console.log(`Auto-approving for production: ${msg.message}`); return msg.defaultResponse; } // For other environments, implement custom approval logic return promptForApproval(msg); } } as IIoHost }); try { // Create cloud assembly source from the CDK app console.log(`Creating cloud assembly source from ${appPath}`); const cloudAssemblySource = await toolkit.fromCdkApp(appPath); // Synthesize the cloud assembly console.log(`Synthesizing cloud assembly`); const cloudAssembly = await toolkit.synth(cloudAssemblySource); try { // Deploy with environment-specific options console.log(`Deploying to ${environment} environment`); return await toolkit.deploy(cloudAssembly, { stacks: options.stacks || { strategy: StackSelectionStrategy.ALL_STACKS }, parameters: options.parameters || {}, tags: { Environment: environment, DeployedBy: 'CDK-Toolkit-Library', DeployTime: new Date().toISOString() } }); } finally { // Always dispose when done await cloudAssembly.dispose(); } } catch (error) { console.error(`Deployment to ${environment} failed:`, error); throw error; } } // Example usage await deployApplication('ts-node app.ts', 'staging', { parameters: { MyStack: { InstanceType: 't3.small' } } });
デプロイの進行状況の追跡
詳細なステータス更新を使用してデプロイの進行状況を追跡します。
import { Toolkit, StackSelectionStrategy, IIoHost } from '@aws-cdk/toolkit-lib'; // Create a progress tracker class DeploymentTracker { private startTime: Date; private resources = new Map<string, string>(); constructor() { this.startTime = new Date(); } onStackEvent(stackName: string, event: string, timestamp: string) { // Calculate elapsed time if needed, or use the provided timestamp const elapsed = (new Date().getTime() - this.startTime.getTime()) / 1000; console.log(`[${timestamp}] (${elapsed.toFixed(1)}s elapsed) Stack ${stackName}: ${event}`); } onResourceEvent(resourceId: string, status: string) { this.resources.set(resourceId, status); this.printProgress(); } private printProgress() { console.log('\nResource Status:'); for (const [id, status] of this.resources.entries()) { console.log(`- ${id}: ${status}`); } console.log(); } } // Use the tracker with the toolkit const tracker = new DeploymentTracker(); const toolkit = new Toolkit({ ioHost: { notify: async (msg) => { if (msg.code.startsWith('CDK_DEPLOY')) { // Track deployment events if (msg.data && 'stackName' in msg.data) { tracker.onStackEvent(msg.data.stackName, msg.message, msg.time); } } else if (msg.code.startsWith('CDK_RESOURCE')) { // Track resource events if (msg.data && 'resourceId' in msg.data) { tracker.onResourceEvent(msg.data.resourceId, msg.message); } } } } as IIoHost }); // Example usage with progress tracking async function deployWithTracking(cloudAssemblySource: any) { try { // Synthesize the cloud assembly const cloudAssembly = await toolkit.synth(cloudAssemblySource); try { // Deploy using the cloud assembly await toolkit.deploy(cloudAssembly, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); } finally { // Always dispose when done await cloudAssembly.dispose(); } } catch (error) { // Display the error message console.error("Operation failed:", error.message); throw error; } }
復旧によるエラーの処理
復旧戦略を使用して堅牢なエラー処理を実装します。
import { Toolkit, ToolkitError, StackSelectionStrategy } from '@aws-cdk/toolkit-lib'; async function deployWithRetry(toolkit: Toolkit, cloudAssemblySource: any) { try { // Synthesize the cloud assembly const cloudAssembly = await toolkit.synth(cloudAssemblySource); try { // Deploy using the cloud assembly await toolkit.deploy(cloudAssembly, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); } finally { // Always dispose when done await cloudAssembly.dispose(); } } catch (error) { // Simply show the error to the user console.error("Operation failed:", error.message); throw error; } } // Example usage try { await deployWithRetry(toolkit, cloudAssemblySource); } catch (error) { console.error("Operation failed:", error.message); process.exit(1); }
CI/CD パイプラインとの統合
CDK Toolkit Library を CI/CD パイプラインに統合します。
import { Toolkit, StackSelectionStrategy, IIoHost } from '@aws-cdk/toolkit-lib'; import * as fs from 'fs'; import * as path from 'path'; async function cicdDeploy() { // Create a non-interactive toolkit for CI/CD environments const toolkit = new Toolkit({ ioHost: { notify: async (msg) => { // Write to both console and log file const logMessage = `${msg.time} [${msg.level}] ${msg.message}`; console.log(logMessage); // Append to deployment log file fs.appendFileSync('deployment.log', logMessage + '\n'); }, requestResponse: async (msg) => { // Always use default responses in CI/CD console.log(`Auto-responding to: ${msg.message} with: ${msg.defaultResponse}`); return msg.defaultResponse; } } as IIoHost }); // Determine environment from CI/CD variables const environment = process.env.DEPLOYMENT_ENV || 'development'; // Load environment-specific parameters const paramsPath = path.join(process.cwd(), `params.${environment}.json`); const parameters = fs.existsSync(paramsPath) ? JSON.parse(fs.readFileSync(paramsPath, 'utf8')) : {}; try { // Use pre-synthesized cloud assembly from build step const cloudAssemblySource = await toolkit.fromAssemblyDirectory('cdk.out'); // Synthesize the cloud assembly const cloudAssembly = await toolkit.synth(cloudAssemblySource); try { // Deploy with CI/CD specific options const result = await toolkit.deploy(cloudAssembly, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS }, parameters, tags: { Environment: environment, BuildId: process.env.BUILD_ID || 'unknown', CommitHash: process.env.COMMIT_HASH || 'unknown' } }); // Write outputs to a file for subsequent pipeline steps fs.writeFileSync( 'stack-outputs.json', JSON.stringify(result.outputs, null, 2) ); return result; } finally { // Always dispose when done await cloudAssembly.dispose(); } } catch (error) { // Display the error message console.error("Operation failed:", error.message); process.exit(1); } } // Run the CI/CD deployment cicdDeploy().then(() => { console.log('CI/CD deployment completed successfully'); });
追加リソース
これらの例で使用される特定のコンポーネントの詳細については、以下を参照してください。
-
クラウドアセンブリソースの管理 - クラウドアセンブリソースを作成および管理する方法を説明します。
-
メッセージとインタラクションの設定 -
IIoHost
インターフェイスのカスタマイズに関する詳細なガイド。