简体   繁体   English

AWS CDK。 如何将第一个堆栈的 `id` 传递给第二个堆栈

[英]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文件。

  • My 1 stack creates a VPC我的 1 个堆栈创建了一个 VPC
  • My 2 stack creates a ECS我的 2 堆栈创建了一个 ECS

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:所以你可以这样做:

  1. Your vpc should export ec2.Vpc您的vpc应该导出ec2.Vpc
  2. Construct won't work in your case, you need to extend from cdk.Stack Construct在您的情况下不起作用,您需要从cdk.Stack扩展
  3. Since your 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.

相关问题 使用 jenkins 管道进行部署时,如何将参数从第一个 cdk 堆栈的 output 传递到另一个 cdk 堆栈的输入? - How do i pass parameters from first cdk stack's output to another cdk stack's input, when deploying using jenkins pipeline? 如何将新的 cdk 堆栈与现有的 aws EventBridge (EventTarget) 集成 - How to integrate new cdk stack with existed aws EventBridge (EventTarget) AWS CDK:如何从单个堆栈部署多个堆栈 - AWS CDK: How can I deploy multiple stack from a single stack 在 AWS CDK 堆栈中指定 MSK 凭证 - Specifying MSK credentials in an AWS CDK stack 无法在 CDK 应用程序中为 AWS S3 Glacier 创建 CDK 堆栈? - Cannot create CDK stack for AWS S3 Glacier in CDK app? 总是在 aws-cdk 中出现 stack.regionalFact 错误 - always getting stack.regionalFact error in aws-cdk AWS CDK - 从其他堆栈访问资源 - AWS CDK- Access resource from other stack 使用aws cdk将一个堆栈中定义的所有资源获取到另一个堆栈 - Get all the resources defined in one stack to another stack using aws cdk 使用 Rest API 和 Lambda Function 集成部署 CDK 堆栈时出错 (AWS CDK 2 Python API) - Error deploying a CDK stack with a Rest API with a Lambda Function Integration (AWS CDK 2 Python API) 如何将 Jhipster 堆栈部署到 AWS - How to deploy Jhipster stack to AWS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM