简体   繁体   English

使用aws cdk将一个堆栈中定义的所有资源获取到另一个堆栈

[英]Get all the resources defined in one stack to another stack using aws cdk

I am creating two stacks and want to reference first stacks resources like Lambda, API Gateway, DyanamoDb into the second stack either using the name of reference to object to the first stack.我正在创建两个堆栈,并想将第一个堆栈资源(如 Lambda、API Gateway、DyanamoDb)引用到第二个堆栈中,或者使用对第一个堆栈的引用名称 object。 Note: I do not want to use Stack Props to hard code all the resources into second stack.注意:我不想使用 Stack Props 将所有资源硬编码到第二个堆栈中。 Eg File 1例如文件 1

export class StackOne extends cdk.Stack {
    constructor(scope: Construct, id: string, props: StackOneProps) {
    super(scope, id, { env: props.env });
    
    const lambda1 = new lambda.Function();
    const lambda2 = new lambda.Function();
    const api = new apigateway.RestApi()
    new apigateway.LambdaIntegration(
      lambda1
    );
    new apigateway.LambdaIntegration(
      lambda2
    );

    }
}

File 2文件 2

export class StackTwo extends cdk.Stack {
    constructor(scope: Construct, id: string, props: StackTwoProps) {
    super(scope, id, { env: props.env });
    
    const StackOne = //Get the StackOne Reference
    StackOne.Resourcs.forEach(rsourcs ==> {} )

    }
}

If you assign the resources as the public readonly properties, you can iterate over the properties of the stack object and detect those of IResource (or other that interests you) type.如果将资源分配为公共只读属性,则可以迭代堆栈 object 的属性并检测IResource (或您感兴趣的其他)类型的属性。

export class StackOne extends cdk.Stack {
    public readonly lambda1: Function;
    public readonly lambda2: Function;
    // etc.

    constructor(scope: Construct, id: string, props: StackOneProps) {
    super(scope, id, { env: props.env });
    
    this.lambda1 = new lambda.Function();
    this.lambda2 = new lambda.Function();
    // etc.
    }
}
export interface StackTwoProps { 
    stackOne: Stack
}

export class StackTwo extends cdk.Stack {
    constructor(scope: Construct, id: string, props: StackTwoProps) {
    super(scope, id, { env: props.env });
    
    const getResource = (r: any) => r instanceof IResource
        ? r
        : undefined;
    const keys = Object.keys(props.stackOne);
    const resources = keys
        .map(x => getResource(props.stackOne[x]))
        .filter(x => x !== undefined) as IResource);

    }
}

In theory you can access a Stack's synth-ed children (the processed resources, not the original constructs you defined) with escape hatch syntax: myStack.node.children .理论上,您可以使用转义舱口语法访问 Stack 的合成子项(已处理的资源,而不是您定义的原始构造): myStack.node.children

This is probably a bad idea unless your use case is unusual.除非您的用例不寻常,否则这可能是个坏主意。

Passing resource dependencies as props is the standard solution.将资源依赖项作为 props 传递是标准解决方案。 If you find yourself having to pass lots and lots of resources between stacks, it may be a sign that you should keep the resources in a single stack .如果您发现自己必须在堆栈之间传递大量资源,这可能表明您应该将资源保留在一个堆栈中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CDK:如何使用/导入一个堆栈中定义的 s3 到另一个堆栈(在不同的文件中)? - CDK: How to use/import s3 defined in one stack to another stack (in a different file)? 使用 CDK cli 获取堆栈依赖项列表 - Get a list of stack dependencies using CDK cli 在 AWS CDK 堆栈中指定 MSK 凭证 - Specifying MSK credentials in an AWS CDK stack 在不同 AWS CDK 堆栈之间共享资源(如 VPC、安全规则)的最佳和推荐方式 - Best & Recommended way to share resources like VPC, Security Rules between different AWS CDK Stack 如何检查我的 CDK 堆栈中的所有资源是否都具有某些属性? - How do I check if all resources in my CDK stack have certain properties? AWS CDK。 如何将第一个堆栈的 `id` 传递给第二个堆栈 - AWS CDK. How to pass `id` of first stack to second stack 无法在 CDK 应用程序中为 AWS S3 Glacier 创建 CDK 堆栈? - Cannot create CDK stack for AWS S3 Glacier in CDK app? 使用 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? AWS CDK - 从其他堆栈访问资源 - AWS CDK- Access resource from other stack 如何使用 aws cdk 列出所有已部署的 aws 资源 - How to list all deployed aws resources with aws cdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM