Skip to content

Commit d9380d8

Browse files
committed
refactor: Auto-detect AgentCore network type from VPC configuration
- Remove agentCoreNetworkType parameter from user input - Add isAgentCoreNetworkPrivate computed in parameter.ts - Automatically set to true when both agentCoreVpcId and agentCoreSubnetIds are provided - Add validation to ensure VPC settings are provided together - Update GenericAgentCore to use boolean flag instead of enum - Applies to both Generic Runtime and Agent Builder Runtime
1 parent 7b59ec1 commit d9380d8

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

packages/cdk/cdk.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"createGenericAgentCoreRuntime": false,
6969
"agentCoreRegion": null,
7070
"agentCoreExternalRuntimes": [],
71-
"agentCoreNetworkType": "PUBLIC",
7271
"agentCoreVpcId": null,
7372
"agentCoreSubnetIds": null,
7473
"allowedIpV4AddressRanges": null,

packages/cdk/lib/agent-core-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AgentCoreStack extends Stack {
2222
env: params.env,
2323
createGenericRuntime: params.createGenericAgentCoreRuntime,
2424
createAgentBuilderRuntime: params.agentBuilderEnabled,
25-
agentCoreNetworkType: params.agentCoreNetworkType,
25+
isAgentCoreNetworkPrivate: params.isAgentCoreNetworkPrivate,
2626
agentCoreVpcId: params.agentCoreVpcId,
2727
agentCoreSubnetIds: params.agentCoreSubnetIds,
2828
});

packages/cdk/lib/construct/generic-agent-core.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface GenericAgentCoreProps {
3838
env: string;
3939
createGenericRuntime?: boolean;
4040
createAgentBuilderRuntime?: boolean;
41-
agentCoreNetworkType: 'PUBLIC' | 'PRIVATE';
41+
isAgentCoreNetworkPrivate?: boolean;
4242
agentCoreVpcId?: string | null;
4343
agentCoreSubnetIds?: string[] | null;
4444
agentCoreEnvironmentVariables?: Record<string, string>;
@@ -67,7 +67,7 @@ export class GenericAgentCore extends Construct {
6767
env,
6868
createGenericRuntime = false,
6969
createAgentBuilderRuntime = false,
70-
agentCoreNetworkType = 'PUBLIC',
70+
isAgentCoreNetworkPrivate = false,
7171
agentCoreVpcId = null,
7272
agentCoreSubnetIds = null,
7373
} = props;
@@ -85,11 +85,7 @@ export class GenericAgentCore extends Construct {
8585
let vpc: IVpc | undefined;
8686
let subnets: ISubnet[] | undefined;
8787

88-
if (
89-
agentCoreNetworkType === 'PRIVATE' &&
90-
agentCoreVpcId &&
91-
agentCoreSubnetIds
92-
) {
88+
if (isAgentCoreNetworkPrivate && agentCoreVpcId && agentCoreSubnetIds) {
9389
vpc = Vpc.fromLookup(this, 'AgentCoreVpc', { vpcId: agentCoreVpcId });
9490
subnets = agentCoreSubnetIds.map((subnetId, index) =>
9591
Subnet.fromSubnetId(this, `AgentCoreSubnet${index}`, subnetId)
@@ -126,7 +122,7 @@ export class GenericAgentCore extends Construct {
126122
this.resources = this.createResources(
127123
createGenericRuntime,
128124
createAgentBuilderRuntime,
129-
agentCoreNetworkType,
125+
isAgentCoreNetworkPrivate,
130126
vpc,
131127
subnets,
132128
securityGroup
@@ -184,7 +180,7 @@ export class GenericAgentCore extends Construct {
184180
private createResources(
185181
createGeneric: boolean,
186182
createAgentBuilder: boolean,
187-
agentCoreNetworkType: 'PUBLIC' | 'PRIVATE',
183+
isAgentCoreNetworkPrivate: boolean,
188184
vpc?: IVpc,
189185
subnets?: ISubnet[],
190186
securityGroup?: SecurityGroup
@@ -201,7 +197,7 @@ export class GenericAgentCore extends Construct {
201197
'Generic',
202198
this.genericRuntimeConfig,
203199
role,
204-
agentCoreNetworkType,
200+
isAgentCoreNetworkPrivate,
205201
vpc,
206202
subnets,
207203
securityGroup
@@ -213,7 +209,7 @@ export class GenericAgentCore extends Construct {
213209
'AgentBuilder',
214210
this.agentBuilderRuntimeConfig,
215211
role,
216-
agentCoreNetworkType,
212+
isAgentCoreNetworkPrivate,
217213
vpc,
218214
subnets,
219215
securityGroup
@@ -228,13 +224,13 @@ export class GenericAgentCore extends Construct {
228224
type: string,
229225
config: AgentCoreRuntimeConfig,
230226
role: Role,
231-
agentCoreNetworkType: 'PUBLIC' | 'PRIVATE',
227+
isAgentCoreNetworkPrivate: boolean,
232228
vpc?: IVpc,
233229
subnets?: ISubnet[],
234230
securityGroup?: SecurityGroup
235231
): Runtime {
236232
const networkConfig = this.createNetworkConfiguration(
237-
agentCoreNetworkType,
233+
isAgentCoreNetworkPrivate,
238234
vpc,
239235
subnets,
240236
securityGroup
@@ -253,15 +249,15 @@ export class GenericAgentCore extends Construct {
253249
}
254250

255251
private createNetworkConfiguration(
256-
agentCoreNetworkType: 'PUBLIC' | 'PRIVATE',
252+
isAgentCoreNetworkPrivate: boolean,
257253
vpc?: IVpc,
258254
subnets?: ISubnet[],
259255
securityGroup?: SecurityGroup
260256
): RuntimeNetworkConfiguration {
261-
if (agentCoreNetworkType === 'PRIVATE') {
257+
if (isAgentCoreNetworkPrivate) {
262258
if (!vpc || !subnets) {
263259
throw new Error(
264-
'VPC and Subnets are required for PRIVATE network type'
260+
'VPC and Subnets are required for private network configuration'
265261
);
266262
}
267263

packages/cdk/lib/stack-input.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ const baseStackInputSchema = z.object({
161161
)
162162
.default([]),
163163
// Agent Core Network Configuration
164-
agentCoreNetworkType: z.enum(['PUBLIC', 'PRIVATE']).default('PUBLIC'),
165164
agentCoreVpcId: z.string().nullish(),
166165
agentCoreSubnetIds: z.array(z.string()).nullish(),
167166
// MCP
@@ -222,20 +221,18 @@ export const stackInputSchema = baseStackInputSchema
222221
)
223222
.refine(
224223
(data) => {
225-
// Validate AgentCore network configuration
226-
if (data.agentCoreNetworkType === 'PRIVATE') {
227-
return (
228-
data.agentCoreVpcId &&
229-
data.agentCoreSubnetIds &&
230-
data.agentCoreSubnetIds.length > 0
231-
);
232-
}
233-
return true;
224+
// Validate AgentCore VPC configuration consistency
225+
const hasVpcId = !!data.agentCoreVpcId;
226+
const hasSubnetIds =
227+
data.agentCoreSubnetIds && data.agentCoreSubnetIds.length > 0;
228+
229+
// Both must be provided or both must be empty
230+
return hasVpcId === hasSubnetIds;
234231
},
235232
{
236233
message:
237-
'VPC ID and Subnet IDs are required when agentCoreNetworkType is PRIVATE',
238-
path: ['agentCoreNetworkType'],
234+
'Both VPC ID and Subnet IDs must be provided together for AgentCore network configuration',
235+
path: ['agentCoreVpcId'],
239236
}
240237
);
241238

@@ -277,6 +274,8 @@ export const processedStackInputSchema = baseStackInputSchema.extend({
277274
),
278275
// Processed agentCoreRegion (null -> modelRegion)
279276
agentCoreRegion: z.string(),
277+
// Computed from VPC configuration (computed in parameter.ts)
278+
isAgentCoreNetworkPrivate: z.boolean().optional(),
280279
// Branding configuration
281280
brandingConfig: z
282281
.object({

packages/cdk/parameter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export const getParams = (app: cdk.App): ProcessedStackInput => {
7878
),
7979
// Process agentCoreRegion: null -> modelRegion
8080
agentCoreRegion: params.agentCoreRegion || params.modelRegion,
81+
// Compute isAgentCoreNetworkPrivate from VPC configuration
82+
isAgentCoreNetworkPrivate: !!(
83+
params.agentCoreVpcId &&
84+
params.agentCoreSubnetIds &&
85+
params.agentCoreSubnetIds.length > 0
86+
),
8187
// Load branding configuration
8288
brandingConfig: loadBrandingConfig(),
8389
};

packages/cdk/test/generative-ai-use-cases.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ describe('GenerativeAiUseCases', () => {
215215

216216
const params = processedStackInputSchema.parse({
217217
...stackInput,
218-
agentCoreNetworkType: 'PRIVATE',
219218
agentCoreVpcId: 'vpc-12345678',
220219
agentCoreSubnetIds: ['subnet-12345678', 'subnet-87654321'],
220+
isAgentCoreNetworkPrivate: true,
221221
});
222222

223223
const {

0 commit comments

Comments
 (0)