[英]AWS CDK. How to pass `id` of first stack to second stack
I've a project with multiple stacks, but with common stacks.ts
file.我有一个具有多个堆栈的项目,但具有通用的stacks.ts
文件。
Stack vpc.tf
堆栈vpc.tf
export class stack-vpc extends Stack {
constructor(scope: Construct, id: string, props?: props) {
super(scope, id, props);
new ec2.Vpc(this, 'dev-vpc', {
cidr: "10.0.0.0/16",
vpcName: "dev-vpc"
})
}
}
Stack ecs.tf
堆栈ecs.tf
export interface Istack-ecs extends StackProps {
vpc: ec2.Vpc;
}
export class stack-ecs extends Construct {
constructor(scope: Construct, id: string, props: Istack-ecs) {
super(scope, id);
new ecs.Cluster(this, "fargate", {
vpc: props.vpc,
clusterName: "test",
enableFargateCapacityProviders: true
});
}
}
Here is the stacks.ts
file这是stacks.ts
文件
const app = new cdk.App();
const vpc = new stack-vpc(app, "Dev_VPC_CF", props);
const ecs = new stack-ecs(app, "Dev_ECS_CF", props);
The vpc
is creating without any problem, however once I'm putting an ecs to my stack the cdk gives me the error: vpc
的创建没有任何问题,但是一旦我将 ecs 放入我的堆栈中,cdk 就会给我错误:
[Error at /stack-ecs] Could not find any VPCs matching {"account":"__--__","region":"eu-central-1",
"filter":{"tag:Name":"stack-vpc","isDefault":"false"},
"returnAsymmetricSubnets":true,
"lookupRoleArn":"arn:aws:iam::__--__:role/cdk-hnb659fds-lookup-role-__--__-eu-central-1"}
cdk.context.json
is retrieving correct values from my aws credentials, so I can't blame the authentification, so my problem I need to somehow wait until one stack will give me an output so I can use it in my other stacks cdk.context.json
正在从我的 aws 凭据中检索正确的值,所以我不能责怪身份验证,所以我的问题我需要以某种方式等到一个堆栈给我一个 output,这样我就可以在我的其他堆栈中使用它
The fromLookup
methods are for referencing existing deployed resources. fromLookup
方法用于引用现有的已部署资源。 This is not what you want.这不是你想要的。 Instead, use Typescript language features to pass constructs within and between stacks.相反,使用 Typescript 语言功能在堆栈内和堆栈之间传递构造。 As @gshpychka explains in the comments, there are several CDK patterns:正如@gshpychka 在评论中解释的那样,有几种 CDK 模式:
For a simple app, a single stack with a Vpc and Ecs resources is a good approach .对于一个简单的应用程序,具有 Vpc 和 Ecs 资源的单个堆栈是一个很好的方法。 The cdk-examples
repo has several such ECS examples with a Vpc : cdk-examples
repo 有几个这样的带有 Vpc 的 ECS 示例:
export class MySingleStack extends Stack {
constructor(scope: Construct, id: string, props?: props) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'dev-vpc', {
cidr: '10.0.0.0/16',
vpcName: 'dev-vpc',
});
const cluster = new ecs.Cluster(this, 'Ec2Cluster', { vpc });
}
}
Alternatively, pass a Vpc reference between stacks in the same app.或者,在同一应用程序的堆栈之间传递 Vpc 引用。 VpcStack exports a Vpc. VpcStack 导出一个 Vpc。 EcsStack's props should extend cdk.StackProps
, adding a vpc: ec2.Vpc
attribute. EcsStack 的 props 应该扩展cdk.StackProps
,添加一个vpc: ec2.Vpc
属性。 The vpc is exported from VpcStack and passed to EcsStack as a prop: vpc 从 VpcStack 导出并作为 prop 传递给 EcsStack:
// vpc.ts
export class VpcStack extends Stack {
readonly vpc: ec2.Vpc;
constructor(scope: Construct, id: string, props?: props) {
super(scope, id, props);
this.vpc = new ec2.Vpc(this, 'dev-vpc', {
cidr: '10.0.0.0/16',
vpcName: 'dev-vpc',
});
}
}
// stacks.ts
const app = new cdk.App();
const { vpc } = new VpcStack(app, 'Dev_VPC_CF', props);
new EcsStack(app, 'Dev_ECS_CF', { ...props, vpc });
As @fedonev said正如@fedonev所说
The fromLookup methods are for referencing existing deployed resources. fromLookup 方法用于引用现有的已部署资源。
So instead you can do something like this:所以你可以这样做:
vpc
should export ec2.Vpc
您的vpc
应该导出ec2.Vpc
Construct
won't work in your case, you need to extend from cdk.Stack
Construct
在您的情况下不起作用,您需要从cdk.Stack
扩展ecs
interface Istack-ecs
already expecting a vpc
因为你的ecs
接口Istack-ecs
已经在期待一个vpc
const app = new cdk.App();
const your_vpc = new stack-vpc(app, "Dev_VPC_CF", props);
const your_ecs = new stack-ecs(app, "Dev_ECS_CF", {
vpc: your_vpc.vpc // As your props expects one single value
});
your_ecs.addDependency(your_vpc); // Guarantees That your `vpc` will be created firstly
app.synth();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.