はじめに
CDKを使う中で、アカウント・リージョン指定が無い為にハマったので、そのエラー対処の記録です。
検証環境
- cdk(v2.44.0)
- typescript(v4.8.4)
- node.js(v16.14.0)
問題の内容
CDKでcodepipelineのDeployステージを構築しようとした際、以下のエラーメッセージが表示されました。
Error: Pipeline stack which uses cross-environment actions must have an explicitly set region
at Pipeline.requireRegion (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:19177)
at Pipeline.ensureReplicationResourcesExistFor (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:8416)
at Pipeline._attachActionToPipeline (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:7708)
at Stage.attachActionToPipeline (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:3087)
at Stage.addAction (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:1716)
at new Stage (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:678)
at Pipeline.addStage (/home/ec2-user/environment/cdk-cicd/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:6662)
at new CdkCicdStack (/home/ec2-user/environment/cdk-cicd/lib/cdk-cicd-stack.ts:137:34)
at Object.<anonymous> (/home/ec2-user/environment/cdk-cicd/bin/cdk-cicd.ts:7:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
他は省略しますが、構築しようとしたのは以下のソースです。
cdk-cicd-stack.ts
// ECS Service
const service = ecs.BaseService.fromServiceArnWithCluster(this, 'EcsService',
'arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:service/cluster-name/service-name'
);
// Code Pipeline(Deploy)
const deployStage = pipeline.addStage({
stageName: 'Deploy',
actions: [
new codepipeline_actions.EcsDeployAction({
actionName: 'DeployAction',
service: service,
input: buildOutput,
}),
],
});
Errorの内容を訳すと、
クロスエンバイロメントアクションを使用するパイプラインスタックは、明示的にリージョンを設定する必要があります。
とあり、要はリージョンを設定する必要があるとの事でした。
エラーの内容を検索して辿り着いたのがこちら。
https://github.com/aws/aws-cdk/issues/20011
やり取りによると、パイプラインがクロスアカウントかクロスリージョンかを決めるのはsynth
時なので、このsynth
自体がどのAWSアカウントで実行するかは明示する必要がある、という事みたいですね。
振り返ってみてcdk-cicd.ts
を見てみると、以下の記載がありました。
cdk-cicd.ts
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkCicdStack } from '../lib/cdk-cicd-stack';
const app = new cdk.App();
new CdkCicdStack(app, 'CdkCicdStack', {
/* 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 */
});
コメントアウトの部分を最初は読み飛ばしていましたが、正にその事が記載されており、
-
env
を指定しない場合はスタックが環境に依存しないものとなる - コメントを外す事で、AWSアカウントとリージョンの為にスタックを特化できる
という事のようです。
対処内容
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
または
// env: { account: '123456789012', region: 'us-east-1' },
のどちらかのコメントアウトを無くし、account
とregion
の指定をする事で、エラーは解消出来ました!