Skip to content

Commit af9123b

Browse files
authored
Merge pull request #134 from aws-samples/wangzt-dev
cdk robustness update
2 parents bc5133f + b6512a2 commit af9123b

File tree

4 files changed

+70
-44
lines changed

4 files changed

+70
-44
lines changed

source/resources/bin/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
#!/usr/bin/env node
21
import * as cdk from 'aws-cdk-lib';
32
import { MainStack } from '../lib/main-stack';
3+
44
const devEnv = {
55
account: process.env.CDK_DEFAULT_ACCOUNT,
66
region: process.env.CDK_DEFAULT_REGION,
77
};
88

99
const app = new cdk.App();
10-
new MainStack(app, 'GenBiMainStack', { env: devEnv });
10+
const deployRds = process.argv.includes('--deploy-rds'); // Check if --deploy-rds flag is present
11+
12+
new MainStack(app, 'GenBiMainStack', { env: devEnv, deployRds }); // Pass deployRDS flag to MainStack constructor
1113
app.synth();

source/resources/lib/aos/aos-stack.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@ import { AnyPrincipal, Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
77
import * as crypto from 'crypto';
88

99
export class AOSStack extends cdk.Stack {
10-
_vpc;
1110
_securityGroup;
1211
public readonly endpoint: string;
1312
public readonly OSMasterUserSecretName: string;
1413
public readonly OSHostSecretName: string;
1514

16-
constructor(scope: Construct, id: string, props: cdk.StackProps) {
15+
constructor(scope: Construct, id: string, props: cdk.StackProps & {vpc: ec2.Vpc} & { subnets: cdk.aws_ec2.ISubnet[] }) {
1716
super(scope, id, props);
1817

19-
this._vpc = ec2.Vpc.fromLookup(this, "VPC", {
20-
isDefault: true,
21-
});
22-
// Lookup a VPC
23-
// this._vpc = props.vpc;
24-
2518
// Create a Security Group for OpenSearch
2619
this._securityGroup = new ec2.SecurityGroup(this, 'GenBIOpenSearchSG', {
27-
vpc: this._vpc,
20+
vpc: props.vpc,
2821
description: 'Allow access to OpenSearch',
2922
allowAllOutbound: true
3023
});
@@ -52,14 +45,15 @@ export class AOSStack extends cdk.Stack {
5245
this._securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443), 'Allow HTTPS access');
5346

5447
// Find subnets in different availability zones
55-
const subnets = this._vpc.selectSubnets({
56-
subnetType: ec2.SubnetType.PUBLIC,
48+
const subnets = props.vpc.selectSubnets({
49+
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
5750
}).subnets;
51+
// const subnets = this._vpc.selectSubnets().subnets;
5852

5953
// Create the OpenSearch domain
6054
const domain = new opensearch.Domain(this, 'GenBiOpenSearchDomain', {
6155
version: opensearch.EngineVersion.OPENSEARCH_2_9,
62-
vpc: this._vpc,
56+
vpc: props.vpc,
6357
securityGroups: [this._securityGroup],
6458
accessPolicies: [new PolicyStatement({
6559
effect: Effect.ALLOW,
@@ -68,21 +62,26 @@ export class AOSStack extends cdk.Stack {
6862
resources: [`arn:${this.partition}:es:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:domain/*`]
6963
})]
7064
,
71-
vpcSubnets: [
72-
{ subnets: [subnets[0]] },
73-
],
74-
// vpcSubnets: SubnetSelection(one_per_az=True, subnet_type=aws_ec2.SubnetType.PUBLIC),
65+
vpcSubnets: [ {subnets: subnets.slice(0, 2)}],
66+
// vpcSubnets: [
67+
// { subnets: [subnets[0]] },
68+
// ],
7569
capacity: {
76-
dataNodes: 1,
70+
dataNodes: 2,
7771
dataNodeInstanceType: 'm5.large.search',
7872
multiAzWithStandbyEnabled: false
7973
},
74+
// capacity: {
75+
// dataNodes: 1,
76+
// dataNodeInstanceType: 'm5.large.search',
77+
// multiAzWithStandbyEnabled: false
78+
// },
8079
ebs: {
8180
volumeType: ec2.EbsDeviceVolumeType.GP3,
8281
volumeSize: 20,
8382
},
8483
zoneAwareness: {
85-
enabled: false
84+
availabilityZoneCount: 2
8685
},
8786
nodeToNodeEncryption: true,
8887
encryptionAtRest: {

source/resources/lib/ecs/ecs-stack.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
99
import * as path from 'path';
1010

1111
export class ECSStack extends cdk.Stack {
12-
_vpc;
1312
public readonly streamlitEndpoint: string;
1413
public readonly frontendEndpoint: string;
1514
public readonly apiEndpoint: string;
16-
constructor(scope: Construct, id: string, props: cdk.StackProps & { cognitoUserPoolId: string} & { cognitoUserPoolClientId: string} & {OSMasterUserSecretName: string} & {OSHostSecretName: string}) {
15+
constructor(scope: Construct, id: string, props: cdk.StackProps
16+
& { vpc: ec2.Vpc}
17+
& { subnets: cdk.aws_ec2.ISubnet[] } & { cognitoUserPoolId: string}
18+
& { cognitoUserPoolClientId: string} & {OSMasterUserSecretName: string}
19+
& {OSHostSecretName: string}) {
1720
super(scope, id, props);
18-
// Create a VPC
19-
this._vpc = ec2.Vpc.fromLookup(this, "VPC", {
20-
isDefault: true,
21-
});
21+
22+
// 选择所有的 isolated 和 private with egress 子网
23+
// const isolatedSubnets = this._vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_ISOLATED }).subnets;
24+
// const privateSubnets = this._vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnets;
25+
26+
// 合并所有非公共子网
27+
// const nonPublicSubnets = [...isolatedSubnets, ...privateSubnets];
28+
// const subnets = this._vpc.selectSubnets().subnets;
2229

2330
// Create ECR repositories and Docker image assets
2431
const services = [
@@ -47,7 +54,7 @@ constructor(scope: Construct, id: string, props: cdk.StackProps & { cognitoUserP
4754

4855
// Create an ECS cluster
4956
const cluster = new ecs.Cluster(this, 'GenBiCluster', {
50-
vpc: this._vpc,
57+
vpc: props.vpc,
5158
});
5259

5360
const taskExecutionRole = new iam.Role(this, 'TaskExecutionRole', {
@@ -164,8 +171,8 @@ constructor(scope: Construct, id: string, props: cdk.StackProps & { cognitoUserP
164171
cluster: cluster,
165172
taskDefinition: taskDefinitionStreamlit,
166173
publicLoadBalancer: true,
167-
// taskSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
168-
assignPublicIp: true
174+
taskSubnets: { subnets: props.subnets },
175+
assignPublicIp: false
169176
});
170177

171178
// ======= 2. API Service =======
@@ -204,8 +211,8 @@ constructor(scope: Construct, id: string, props: cdk.StackProps & { cognitoUserP
204211
cluster: cluster,
205212
taskDefinition: taskDefinitionAPI,
206213
publicLoadBalancer: true,
207-
// taskSubnets: { subnetType: ec2.SubnetType.PUBLIC },
208-
assignPublicIp: true
214+
taskSubnets: { subnets: props.subnets },
215+
assignPublicIp: false
209216
});
210217

211218
// ======= 3. Frontend Service =======
@@ -251,7 +258,8 @@ constructor(scope: Construct, id: string, props: cdk.StackProps & { cognitoUserP
251258
taskDefinition: taskDefinitionFrontend,
252259
publicLoadBalancer: true,
253260
// taskSubnets: { subnetType: ec2.SubnetType.PUBLIC },
254-
assignPublicIp: true
261+
taskSubnets: { subnets: props.subnets },
262+
assignPublicIp: false
255263
});
256264

257265
this.streamlitEndpoint = fargateServiceStreamlit.loadBalancer.loadBalancerDnsName;

source/resources/lib/main-stack.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Duration, Stack, StackProps, CfnParameter, CfnOutput } from 'aws-cdk-lib';
1+
import { StackProps, CfnParameter, CfnOutput } from 'aws-cdk-lib';
22
import * as cdk from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
44
import * as ec2 from 'aws-cdk-lib/aws-ec2';
@@ -7,14 +7,21 @@ import { LLMStack } from './model/llm-stack';
77
import { ECSStack } from './ecs/ecs-stack';
88
import { CognitoStack } from './cognito/cognito-stack';
99
import { RDSStack } from './rds/rds-stack';
10+
import { VPCStack } from './vpc/vpc-stack';
11+
12+
interface MainStackProps extends StackProps {
13+
deployRds?: boolean;
14+
}
1015

1116
export class MainStack extends cdk.Stack {
12-
constructor(scope: Construct, id: string, props: StackProps={}) {
17+
constructor(scope: Construct, id: string, props: MainStackProps={ deployRds: false }) {
1318
super(scope, id, props);
1419

15-
// Looking for the default VPC
16-
const vpc = ec2.Vpc.fromLookup(this, "VPC", {
17-
isDefault: true,
20+
const _deployRds = props.deployRds || false;
21+
22+
// ======== Step 0. Define the VPC =========
23+
const _VpcStack = new VPCStack(this, 'vpc-Stack', {
24+
env: props.env,
1825
});
1926

2027
// ======== Step 1. Define the LLMStack =========
@@ -26,7 +33,9 @@ export class MainStack extends cdk.Stack {
2633

2734
// ======== Step 2. Define the AOSStack =========
2835
const _AosStack = new AOSStack(this, 'aos-Stack', {
29-
env: props.env
36+
env: props.env,
37+
vpc: _VpcStack.vpc,
38+
subnets: _VpcStack.vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnets,
3039
});
3140

3241
const aosEndpoint = _AosStack.endpoint;
@@ -45,24 +54,32 @@ export class MainStack extends cdk.Stack {
4554
// pass the aosEndpoint and aosPassword to the ecs stack
4655
const _EcsStack = new ECSStack(this, 'ecs-Stack', {
4756
env: props.env,
57+
vpc: _VpcStack.vpc,
58+
subnets: _VpcStack.vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnets,
4859
cognitoUserPoolId: _CognitoStack.userPoolId,
4960
cognitoUserPoolClientId: _CognitoStack.userPoolClientId,
5061
OSMasterUserSecretName: _AosStack.OSMasterUserSecretName,
5162
OSHostSecretName: _AosStack.OSHostSecretName,
5263
});
53-
64+
_AosStack.addDependency(_VpcStack);
5465
_EcsStack.addDependency(_AosStack);
5566
_EcsStack.addDependency(_CognitoStack);
67+
_EcsStack.addDependency(_VpcStack);
5668

5769
new cdk.CfnOutput(this, 'AOSDomainEndpoint', {
5870
value: aosEndpoint,
5971
description: 'The endpoint of the OpenSearch domain'
6072
});
61-
62-
// new cdk.CfnOutput(this, 'RDSEndpoint', {
63-
// value: _RdsStack.endpoint,
64-
// description: 'The endpoint of the RDS instance'
65-
// });
73+
74+
if (_deployRds) {
75+
const _RdsStack = new RDSStack(this, 'rds-Stack', {
76+
env: props.env,
77+
});
78+
new cdk.CfnOutput(this, 'RDSEndpoint', {
79+
value: _RdsStack.endpoint,
80+
description: 'The endpoint of the RDS instance',
81+
});
82+
}
6683

6784
new cdk.CfnOutput(this, 'StreamlitEndpoint', {
6885
value: _EcsStack.streamlitEndpoint,

0 commit comments

Comments
 (0)