-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenhands-infrastructure.tf
More file actions
594 lines (504 loc) · 12.9 KB
/
openhands-infrastructure.tf
File metadata and controls
594 lines (504 loc) · 12.9 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# OpenHands Isolated AWS Infrastructure - Terraform Configuration
# This template creates a secure, isolated environment for OpenHands with LLM-only access
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Variables
variable "aws_region" {
description = "AWS region for deployment"
type = string
default = "us-west-2"
}
variable "your_ip_cidr" {
description = "Your IP address in CIDR format for access"
type = string
# Get your IP: curl ifconfig.me
validation {
condition = can(cidrhost(var.your_ip_cidr, 0))
error_message = "Must be a valid CIDR block (e.g., 1.2.3.4/32)."
}
}
variable "llm_endpoint_url" {
description = "LLM endpoint URL"
type = string
default = "https://api.openai.com"
}
variable "key_pair_name" {
description = "AWS key pair name for EC2 instances"
type = string
}
variable "environment" {
description = "Environment name"
type = string
default = "production"
}
variable "project_name" {
description = "Project name for resource naming"
type = string
default = "openhands"
}
# Provider configuration
provider "aws" {
region = var.aws_region
default_tags {
tags = {
Project = var.project_name
Environment = var.environment
ManagedBy = "Terraform"
Purpose = "OpenHands-Isolated-Environment"
}
}
}
# Data sources
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
# VPC
resource "aws_vpc" "openhands_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.project_name}-isolated-vpc"
}
}
# Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.openhands_vpc.id
tags = {
Name = "${var.project_name}-igw"
}
}
# Public Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.openhands_vpc.id
cidr_block = "10.0.0.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true
tags = {
Name = "${var.project_name}-public-subnet"
Type = "Public"
}
}
# Private Subnet
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.openhands_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
Name = "${var.project_name}-private-subnet"
Type = "Private"
}
}
# Elastic IP for NAT Gateway
resource "aws_eip" "nat_eip" {
domain = "vpc"
depends_on = [aws_internet_gateway.igw]
tags = {
Name = "${var.project_name}-nat-eip"
}
}
# NAT Gateway
resource "aws_nat_gateway" "nat_gw" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "${var.project_name}-nat-gateway"
}
depends_on = [aws_internet_gateway.igw]
}
# Route Table for Public Subnet
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.openhands_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${var.project_name}-public-rt"
}
}
# Route Table for Private Subnet
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.openhands_vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gw.id
}
tags = {
Name = "${var.project_name}-private-rt"
}
}
# Route Table Associations
resource "aws_route_table_association" "public_rta" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
resource "aws_route_table_association" "private_rta" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private_rt.id
}
# Security Group for Web Proxy
resource "aws_security_group" "web_proxy_sg" {
name_prefix = "${var.project_name}-web-proxy-"
description = "Security group for OpenHands web proxy"
vpc_id = aws_vpc.openhands_vpc.id
# SSH access
ingress {
description = "SSH access"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.your_ip_cidr]
}
# HTTP access
ingress {
description = "HTTP access"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [var.your_ip_cidr]
}
# HTTPS access
ingress {
description = "HTTPS access"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [var.your_ip_cidr]
}
# SSH to private subnet
egress {
description = "SSH to private subnet"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.1.0/24"]
}
# OpenHands web interface
egress {
description = "OpenHands web interface"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["10.0.1.0/24"]
}
# HTTPS outbound for updates and certificates
egress {
description = "HTTPS outbound"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP outbound for updates
egress {
description = "HTTP outbound"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# DNS
egress {
description = "DNS"
from_port = 53
to_port = 53
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
# NTP
egress {
description = "NTP"
from_port = 123
to_port = 123
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.project_name}-web-proxy-sg"
}
lifecycle {
create_before_destroy = true
}
}
# Security Group for OpenHands Server
resource "aws_security_group" "openhands_server_sg" {
name_prefix = "${var.project_name}-server-"
description = "Security group for isolated OpenHands server"
vpc_id = aws_vpc.openhands_vpc.id
# SSH from web proxy
ingress {
description = "SSH from web proxy"
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.web_proxy_sg.id]
}
# OpenHands web interface from proxy
ingress {
description = "OpenHands web interface"
from_port = 3000
to_port = 3000
protocol = "tcp"
security_groups = [aws_security_group.web_proxy_sg.id]
}
# HTTPS outbound (restricted by iptables)
egress {
description = "HTTPS outbound"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP outbound (restricted by iptables)
egress {
description = "HTTP outbound"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# DNS to VPC resolver
egress {
description = "DNS to VPC"
from_port = 53
to_port = 53
protocol = "udp"
cidr_blocks = ["10.0.0.0/16"]
}
# NTP
egress {
description = "NTP"
from_port = 123
to_port = 123
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.project_name}-server-sg"
}
lifecycle {
create_before_destroy = true
}
}
# Network ACL for Private Subnet (Additional Security Layer)
resource "aws_network_acl" "private_nacl" {
vpc_id = aws_vpc.openhands_vpc.id
subnet_ids = [aws_subnet.private_subnet.id]
# Allow SSH from public subnet
ingress {
rule_no = 100
protocol = "tcp"
from_port = 22
to_port = 22
cidr_block = "10.0.0.0/24"
action = "allow"
}
# Allow OpenHands web interface from public subnet
ingress {
rule_no = 110
protocol = "tcp"
from_port = 3000
to_port = 3000
cidr_block = "10.0.0.0/24"
action = "allow"
}
# Allow return traffic
ingress {
rule_no = 200
protocol = "tcp"
from_port = 1024
to_port = 65535
cidr_block = "0.0.0.0/0"
action = "allow"
}
# Allow HTTPS outbound
egress {
rule_no = 100
protocol = "tcp"
from_port = 443
to_port = 443
cidr_block = "0.0.0.0/0"
action = "allow"
}
# Allow HTTP outbound
egress {
rule_no = 110
protocol = "tcp"
from_port = 80
to_port = 80
cidr_block = "0.0.0.0/0"
action = "allow"
}
# Allow DNS
egress {
rule_no = 200
protocol = "udp"
from_port = 53
to_port = 53
cidr_block = "10.0.0.0/16"
action = "allow"
}
# Allow NTP
egress {
rule_no = 210
protocol = "udp"
from_port = 123
to_port = 123
cidr_block = "0.0.0.0/0"
action = "allow"
}
# Allow SSH to public subnet
egress {
rule_no = 300
protocol = "tcp"
from_port = 22
to_port = 22
cidr_block = "10.0.0.0/24"
action = "allow"
}
# Allow OpenHands responses to public subnet
egress {
rule_no = 310
protocol = "tcp"
from_port = 3000
to_port = 3000
cidr_block = "10.0.0.0/24"
action = "allow"
}
# Allow ephemeral ports
egress {
rule_no = 400
protocol = "tcp"
from_port = 1024
to_port = 65535
cidr_block = "0.0.0.0/0"
action = "allow"
}
tags = {
Name = "${var.project_name}-private-nacl"
}
}
# User Data Templates
locals {
web_proxy_user_data = base64encode(templatefile("${path.module}/scripts/web-proxy-userdata.sh", {
project_name = var.project_name
}))
openhands_server_user_data = base64encode(templatefile("${path.module}/scripts/openhands-server-userdata.sh", {
project_name = var.project_name
llm_endpoint = var.llm_endpoint_url
}))
}
# Web Proxy Instance
resource "aws_instance" "web_proxy" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.small"
key_name = var.key_pair_name
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.web_proxy_sg.id]
user_data = local.web_proxy_user_data
root_block_device {
volume_type = "gp3"
volume_size = 20
encrypted = true
}
tags = {
Name = "${var.project_name}-web-proxy"
Role = "WebProxy"
}
}
# OpenHands Server Instance
resource "aws_instance" "openhands_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.large"
key_name = var.key_pair_name
subnet_id = aws_subnet.private_subnet.id
vpc_security_group_ids = [aws_security_group.openhands_server_sg.id]
user_data = local.openhands_server_user_data
root_block_device {
volume_type = "gp3"
volume_size = 50
encrypted = true
}
tags = {
Name = "${var.project_name}-server"
Role = "OpenHandsServer"
}
}
# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "openhands_logs" {
name = "/aws/ec2/${var.project_name}-server"
retention_in_days = 30
tags = {
Name = "${var.project_name}-logs"
}
}
resource "aws_cloudwatch_log_group" "web_proxy_logs" {
name = "/aws/ec2/${var.project_name}-web-proxy"
retention_in_days = 30
tags = {
Name = "${var.project_name}-web-proxy-logs"
}
}
# Outputs
output "web_proxy_public_ip" {
description = "Public IP address of the web proxy"
value = aws_instance.web_proxy.public_ip
}
output "web_proxy_public_dns" {
description = "Public DNS name of the web proxy"
value = aws_instance.web_proxy.public_dns
}
output "openhands_server_private_ip" {
description = "Private IP address of the OpenHands server"
value = aws_instance.openhands_server.private_ip
}
output "vpc_id" {
description = "ID of the VPC"
value = aws_vpc.openhands_vpc.id
}
output "public_subnet_id" {
description = "ID of the public subnet"
value = aws_subnet.public_subnet.id
}
output "private_subnet_id" {
description = "ID of the private subnet"
value = aws_subnet.private_subnet.id
}
output "web_proxy_security_group_id" {
description = "ID of the web proxy security group"
value = aws_security_group.web_proxy_sg.id
}
output "openhands_server_security_group_id" {
description = "ID of the OpenHands server security group"
value = aws_security_group.openhands_server_sg.id
}
output "access_instructions" {
description = "Instructions for accessing the OpenHands interface"
value = <<-EOT
1. SSH to web proxy: ssh -i ${var.key_pair_name}.pem ubuntu@${aws_instance.web_proxy.public_ip}
2. Update Nginx config: sudo /root/update_openhands_ip.sh ${aws_instance.openhands_server.private_ip}
3. SSH to OpenHands server: ssh openhands@${aws_instance.openhands_server.private_ip}
4. Set API key and start service
5. Access web interface: https://${aws_instance.web_proxy.public_ip}
EOT
}