【问题标题】:Best practices for sharing resources between multiple AWS cdks?在多个 AWS cdk 之间共享资源的最佳实践?
【发布时间】:2020-12-15 16:04:31
【问题描述】:

我是 AWS CDK 的新手,但基本上我在 lambda 和 api 网关中有一个 UI 服务,在 ECS 中有另一个服务。两种服务都有自己的 CDK,但需要使用相同的 Dynamo 表。是否可以在 2 个 CDK 之间共享相同的 dynamodb 资源?有没有围绕这个的最佳实践?我们应该从我们的堆栈中排除 Dynamo 吗?

【问题讨论】:

  • 当您说“两种服务都有自己的 CDK”时,您是指同一个 repo 中的不同堆栈还是完全不同的 repos?

标签: aws-lambda aws-api-gateway amazon-ecs aws-cdk


【解决方案1】:

我有一个类似的用例,我只是使用 Cloudformation 输出来共享表 ARN。

例如

在 StackA 中,我有 Dynamodb 表

import * as cdk from '@aws-cdk/core';
import { Table, AttributeType, BillingMode } from '@aws-cdk/aws-dynamodb';
export class StackA extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const table = new Table(this, 'my-table', {
      partitionKey: { name: 'id', type: AttributeType.STRING },
      billingMode: BillingMode.PAY_PER_REQUEST,
      tableName: 'mytable'
    });

    new cdk.CfnOutput(this, 'my-table-output', {
      value: table.tableArn,
      description: 'Data Storage',
      exportName: this.stackName + '-tablearn'
    });
  }
}

在堆栈B中

import * as cdk from '@aws-cdk/core';
import { Table } from '@aws-cdk/aws-dynamodb';
export class StackB extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const stackATable = Table.fromTableArn(this, "stackA-table", cdk.Fn.importValue("StackA-tablearn"));
  }
}

【讨论】:

  • 堆栈 A 不能独立更新,堆栈 B 现在依赖于它,不会造成问题吗?
  • 更新应该没有问题,只有在尝试删除StackA时,Cloudformation才会阻止你,因为StackB依赖它。
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 2016-01-19
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多