スキップしてコンテンツを表示

CDK synth for only one stack errors when resources are missing for the other stack

0

My CDK app has two stacks defined, one for a prod environment and one for a dev environment. I have a CloudFront function defined in the stack with a different file path for dev and prod builds (the behavior of the function necessarily needs to be different for dev and prod, i.e. the dev function implements basic authentication so that the dev site will not be publicly accessible and I don't want that code on the prod side). So for example, the CloudFront function is defined as follows:

const cfViewerRequest = new cloudfront.Function(this, "MySite-CFViewerRequest", {
  code: cloudfront.FunctionCode.fromFile({filePath: `backend/functions/cf-viewer-request/build-${props.stage}/index.js`}),
  functionName: `MySite-CFViewerRequest-${props.stage}`,
  comment: `MySite-CFViewerRequest-${props.stage}`
});

The idea here is that when the "stage" prop is passed to the stack constructor (it is set to either "dev" or "prod"), the resulting file path for the function will either be "build-dev" or "build-prod". However, if I only build the dev version, I get an error ENOENT: no such file or directory, open 'backend/functions/cf-viewer-request/build-prod/index.js' when running cdk synth with the dev stack name, since only the dev resource was built. The same goes for other resources, such as the CloudFront deployment, which also has specific dev and prod build directories. How can I run CDK commands for the dev stack without having to build the prod resources beforehand?

Or is there a better way I should be going about this?

Here is the entry point code:

#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { MySiteStack } from "../lib/my-site-stack";

const app = new cdk.App();
new MySiteStack(app, "MySiteDev", {
  env: { account: "012345678901", region: "us-east-1" },
  stage: "dev",
});

new MySiteStack(app, "MySiteProd", {
  env: { account: "109876543210", region: "us-east-1" },
  stage: "prod",
});

質問済み 3年前1023ビュー
1回答
0

If you don't need to deploy production and dev stacks in same account, then you don't need two stacks to be declared -

new MySiteStack(app, MySite${process.env.ENV_NAME}, { env: { account: "012345678901", region: "us-east-1" }, stage: ${process.env.ENV_NAME}, });

AWS
回答済み 3年前
  • Thanks. When I do it that way any time I do "cdk synth" for either dev or prod, the other stack disappears from the AWS Toolkit developer tools pane in VSCode. Does that mean anything?

  • Yes, that's expected, this is because in this way, its going to only generate the stack for the environment that you're working on.

  • And just to confirm, when I do cdk deploy for one stack, it won't delete the other stack?

  • It should not, because the stack name is generated dynamically, based on the env variable that you set. So it should only work on that stack.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

関連するコンテンツ