-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathagent-vpc.ts
More file actions
151 lines (133 loc) · 5.35 KB
/
agent-vpc.ts
File metadata and controls
151 lines (133 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* MIT No Attribution
*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { RemovalPolicy } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as logs from 'aws-cdk-lib/aws-logs';
import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
/**
* Properties for the AgentVpc construct.
*/
export interface AgentVpcProps {
/**
* Maximum number of availability zones to use.
* @default 2
*/
readonly maxAzs?: number;
/**
* Number of NAT gateways to provision.
* @default 1
*/
readonly natGateways?: number;
/**
* Removal policy for VPC flow log resources.
* @default RemovalPolicy.DESTROY
*/
readonly removalPolicy?: RemovalPolicy;
}
/**
* VPC with restricted egress for the AgentCore Runtime.
*
* Provides HTTPS-only outbound access, VPC endpoints for AWS services,
* and NAT for GitHub API access. Flow logs are enabled for audit.
*/
export class AgentVpc extends Construct {
/** The VPC where the Runtime will be deployed. */
public readonly vpc: ec2.Vpc;
/** Security group for the Runtime ENIs — allows only TCP 443 egress. */
public readonly runtimeSecurityGroup: ec2.SecurityGroup;
constructor(scope: Construct, id: string, props: AgentVpcProps = {}) {
super(scope, id);
const maxAzs = props.maxAzs ?? 2;
const natGateways = props.natGateways ?? 1;
const removalPolicy = props.removalPolicy ?? RemovalPolicy.DESTROY;
// --- VPC ---
this.vpc = new ec2.Vpc(this, 'Vpc', {
maxAzs,
natGateways,
restrictDefaultSecurityGroup: true,
subnetConfiguration: [
{
name: 'Public',
subnetType: ec2.SubnetType.PUBLIC,
cidrMask: 24,
},
{
name: 'Private',
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
cidrMask: 24,
},
],
});
// --- Flow logs (satisfies AwsSolutions-VPC7) ---
const flowLogGroup = new logs.LogGroup(this, 'FlowLogGroup', {
retention: logs.RetentionDays.ONE_MONTH,
removalPolicy,
});
this.vpc.addFlowLog('FlowLog', {
destination: ec2.FlowLogDestination.toCloudWatchLogs(flowLogGroup),
trafficType: ec2.FlowLogTrafficType.ALL,
});
NagSuppressions.addResourceSuppressions(this.vpc, [
{
id: 'AwsSolutions-IAM5',
reason: 'VPC flow log role requires wildcard permissions for CloudWatch Logs — generated by CDK',
},
], true);
// --- Security group (HTTPS-only egress) ---
this.runtimeSecurityGroup = new ec2.SecurityGroup(this, 'RuntimeSG', {
vpc: this.vpc,
description: 'AgentCore Runtime - egress TCP 443 only',
allowAllOutbound: false,
});
this.runtimeSecurityGroup.addEgressRule(
ec2.Peer.anyIpv4(),
ec2.Port.tcp(443),
'Allow HTTPS egress (GitHub API via NAT, AWS services via endpoints)',
);
// --- Gateway endpoints (free, no ENI cost) ---
this.vpc.addGatewayEndpoint('S3Endpoint', {
service: ec2.GatewayVpcEndpointAwsService.S3,
});
this.vpc.addGatewayEndpoint('DynamoDBEndpoint', {
service: ec2.GatewayVpcEndpointAwsService.DYNAMODB,
});
// --- Interface endpoints (private DNS enabled, placed in private subnets) ---
const interfaceEndpoints: Array<{ id: string; service: ec2.InterfaceVpcEndpointAwsService }> = [
{ id: 'EcrApiEndpoint', service: ec2.InterfaceVpcEndpointAwsService.ECR },
{ id: 'EcrDockerEndpoint', service: ec2.InterfaceVpcEndpointAwsService.ECR_DOCKER },
{ id: 'CloudWatchLogsEndpoint', service: ec2.InterfaceVpcEndpointAwsService.CLOUDWATCH_LOGS },
{ id: 'SecretsManagerEndpoint', service: ec2.InterfaceVpcEndpointAwsService.SECRETS_MANAGER },
{ id: 'BedrockRuntimeEndpoint', service: ec2.InterfaceVpcEndpointAwsService.BEDROCK_RUNTIME },
{ id: 'StsEndpoint', service: ec2.InterfaceVpcEndpointAwsService.STS },
{ id: 'XRayEndpoint', service: ec2.InterfaceVpcEndpointAwsService.XRAY },
{ id: 'EcsEndpoint', service: ec2.InterfaceVpcEndpointAwsService.ECS },
{ id: 'EcsAgentEndpoint', service: ec2.InterfaceVpcEndpointAwsService.ECS_AGENT },
{ id: 'EcsTelemetryEndpoint', service: ec2.InterfaceVpcEndpointAwsService.ECS_TELEMETRY },
{ id: 'StepFunctionsEndpoint', service: ec2.InterfaceVpcEndpointAwsService.STEP_FUNCTIONS },
];
for (const ep of interfaceEndpoints) {
this.vpc.addInterfaceEndpoint(ep.id, {
service: ep.service,
privateDnsEnabled: true,
subnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
});
}
}
}