在多个AWS cdk之间共享资源的最佳实践



我是AWS CDK的新手,但本质上我在lambda和api网关中有一个UI服务,在ECS中有另一个服务。这两个服务都有自己的CDK,但需要使用相同的Dynamo表。是否可以在两个CDK之间共享相同的dynamodb资源?这方面有最佳实践吗?我们应该把迪纳摩排除在外吗?

我有一个类似的用例,我只是使用Cloudformation Output来共享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'
});
}
}

在StackB 中

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"));
}
}

最新更新