我正在经历一个奇怪的行为与CDK。
我在这里定义了一个父堆栈:
import * as cdk from "aws-cdk-lib";
import { CdkTsWorkshopStack } from "../lib/cdk-ts-workshop-stack";
function main() {
const app = new cdk.App();
const env_type = app.node.tryGetContext("env");
const env_context = app.node.tryGetContext(env_type);
const suffix = env_context["suffix"];
const region = env_context["region"];
const account = env_context["account"];
const stackName = `${suffix}-testStack`;
const stackProps = {
env: { account: account, region: region },
stackName: stackName,
};
const context = {
env: env_type,
suffix: suffix,
region: region,
account: account,
};
console.log(stackProps)
console.log(process.env.CDK_DEFAULT_ACCOUNT)
console.log(process.env.CDK_DEFAULT_REGION)
new CdkTsWorkshopStack(app, "CdkTsWorkshopStack", stackProps, context);
app.synth();
}
main();
字符串
在另一个文件中,我定义了这些:
import { aws_dynamodb, RemovalPolicy, Stack, StackProps } from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { CDKContext } from "../types";
import { StatelessStack } from "./stateless-stack";
import { StatefullStack } from "./storage-stack";
export class CdkTsWorkshopStack extends Stack {
constructor(
scope: Construct,
id: string,
props?: StackProps,
context?: CDKContext,
) {
super(scope, id, props);
new StatelessStack(this, "StatelessStack",props, context);
new StatefullStack(this, "StatefullStack", props, context);
}
}
型
而Stateless和Statefull堆栈的定义如下:
import { aws_dynamodb, RemovalPolicy, Stack, StackProps, NestedStackProps, NestedStack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { CDKContext } from "../types";
import * as lambda from "aws-cdk-lib/aws-lambda";
export class StatelessStack extends Stack {
constructor(
scope: Construct,
id: string,
props?: StackProps,
context?: CDKContext,
) {
super(scope, id, props);
const suffix = context?.suffix || "no-suffix";
const env = context?.env || "dev";
const hello = new lambda.Function(this, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_16_X,
code: lambda.Code.fromAsset("lambda"),
handler: "hello.handler",
functionName: `${env}-hello-${suffix}`,
});
}
}
型
和
import { aws_dynamodb, RemovalPolicy, Stack, StackProps, NestedStackProps, NestedStack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { CDKContext } from "../types";
export class StatefullStack extends Stack {
constructor(
scope: Construct,
id: string,
props?: StackProps,
context?: CDKContext,
) {
super(scope, id, props);
const suffix = context?.suffix || "no-suffix";
const env = context?.env || "dev";
const OrderTable = new aws_dynamodb.Table(this, "OrderTable", {
tableName: `${env}-order-management-${suffix}`,
partitionKey: {
name: "orderId",
type: aws_dynamodb.AttributeType.STRING,
},
billingMode: aws_dynamodb.BillingMode.PAY_PER_REQUEST,
timeToLiveAttribute: "ttl",
removalPolicy: RemovalPolicy.DESTROY,
});
}
}
型
所以它并不复杂,两个堆栈只是创建了一个lambda和一个dynamodb表。然后我把它们 Package 在父堆栈下,并从主文件调用它。
然而,发生的事情是,当我使用例如cdk deploy -c env=prod --all
时,堆栈正在创建,并且我立即从cloudformation控制台看到update_complete_cleanup_in_progress和delete启动。所有的栈都被删除了,除了父栈(只包含CDKMetadata),lambda和表都消失了。我错过了什么?
还有一件事如果我只专门部署一个堆栈,让我们说无状态就可以了。如果我决定在第二次运行时部署Statefull,那么Stateless就会被删除!!
发生的另一件有趣的事情是,如果我从示例化无状态和状态完整堆栈的行中删除props和context值,这些将变得与env-agnostic无关,因此它们将部署到它们可以从我的配置文件中识别的区域/帐户(即us-east-1),而父堆栈将部署到eu-central-1(env=prod)。那就没问题了资源在那里。我对发生的事情感到非常困惑(尤其是这种env特定的行为)
感谢任何帮助。
1条答案
按热度按时间nx7onnlm1#
定义 prop 时:
字符串
并将其传递给所有嵌套的堆栈:
型
你显式地将所有堆栈命名为相同的。