简体   繁体   English

AWS CDK - 从其他堆栈访问资源

[英]AWS CDK- Access resource from other stack

Suppose i have a resource, say stepfunction activity, in one stack.假设我在一个堆栈中有一个资源,比如 stepfunction 活动。 How can i access it's arn in other stack?我如何访问它在其他堆栈中的 arn?

I found CfnConstruct suitables for exporting ( https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.CfnOutput.html ).我发现 CfnConstruct 适合导出 ( https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.CfnOutput.html )。 As of now i have used CfnConstruct to export it:截至目前,我已经使用 CfnConstruct 将其导出:

this.initiateValidationActivityArn = new cdk.CfnOutput(this, 'InitiateValidationActivityArn', {
      value: igvsStateMachine.initiateValidationActivity.activityArn
    });

Now how can i access it in other file.现在我如何在其他文件中访问它。 I have tried this:我试过这个:

ecsService.initiateValidationActivityArn.value

But value is private to construct so can't use it.但是价值是私有的,所以不能使用它。

If you have the stacks in one deployable cdk-app , you can access the property value from the stack by making it accessible from outside / not-private.如果您在一个可部署的 cdk-app 中拥有堆栈,则可以通过使其从外部/非私有访问来访问堆栈中的属性值。 What I recommend doing is to keep it readonly so that it can't be re-initialized from outside the stack.我建议做的是将其保持为readonly状态,这样就无法从堆栈外部重新初始化它。

// file: ./lib/first-stack.ts

// import statements

export class FirstStack extends Stack {
  readonly vpc: IVpc;
  
  constructor(...) {
     // setup
     this.vpc = new Vpc(this, "VPC", {...});
  }
}

In the dependent stack, you can pass it via (custom) props.在依赖堆栈中,您可以通过(自定义)道具传递它。

// file: ./lib/second-stack.ts

export interface SecondStackProps extends StackProps {
   importedVpc: IVpc;
}

export class SecondStack extends Stack {
  constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
    super(scope, id, props);
    const importedVpc = props.importedVpc;

    // do sth. with your imported resource
  }
}

Why is that working you may ask...?你可能会问为什么这样工作......? It works because CDK generates the Resource IDs and during the synth phase it can put the tokens for the resources inside the generated templates.它之所以有效,是因为 CDK 生成资源 ID,并且在合成阶段,它可以将资源的令牌放入生成的模板中。

This doesn't work, when you have separated cdk-apps / deployments of stacks, because CDK can't resolve the (existing) Resource ID Tokens during cdk-synth.当您分离了 cdk-apps/堆栈部署时,这不起作用,因为 CDK 无法在 cdk-synth 期间解析(现有的)资源 ID 令牌。 With that, you need to have another approach:有了这个,你需要有另一种方法:

// file: ./lib/first-stack-separated-deployment.ts

// import statements

export class FirstStackSeparatedDeployment extends Stack {
  cfnVpcId: CfnOutput;
  
  constructor(...) {
     // setup
     const vpc = new Vpc(this, "VPC", {...});
     this.cfnVpcId= new cdk.CfnOutput(this, "FirstStackCfnVpcId", {
      value: vpc.vpcId,
      exportName: "UniqueNameForYourVpcId"
    });
  }
}

In the other stack, which requires the already deployed resource, you do the following:在需要已部署资源的另一个堆栈中,您执行以下操作:

// file: ./lib/second-stack-separated-deployment.ts
export class SecondStackSeparatedDeployment extends Stack {
  constructor(...) {
    // setup
    const vpcId = Fn.importValue("UniqueNameForYourVpcId")
    const importedVpc = ec2.Vpc.fromVpcAttributes(this, "ImportedVpc", {
      vpcId: vpcId,
      availabilityZones: [this.region],
    })

    // proceed further...
  }
}

Basically, you take the exportName from the CfnOutput Construct as an identifier and import it via the core Fn.importValue .基本上,您将exportName构造中的CfnOutput作为标识符,并通过核心Fn.importValue将其导入。 With that approach, the first stack already needs to be deployed.使用这种方法,已经需要部署第一个堆栈。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM