-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.tf
More file actions
174 lines (147 loc) · 4.84 KB
/
main.tf
File metadata and controls
174 lines (147 loc) · 4.84 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
##########################
# Data Sources
##########################
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
##########################
# Ollama API Key
##########################
resource "random_password" "ollama_api_key" {
length = 32
special = false
}
##########################
# Lambda Packaging
##########################
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "${path.module}/package"
output_path = "${path.module}/lambda_function.zip"
excludes = []
}
##########################
# S3 Module
##########################
module "s3" {
source = "./modules/s3"
bucket_name = "${local.s3_bucket_prefix}-${data.aws_caller_identity.current.account_id}"
common_tags = local.common_tags
purpose = "Store archived user conversation transcripts"
versioning_enabled = true
# Lifecycle configuration
enable_lifecycle_rules = true
archive_prefix = "archives/"
transition_to_ia_days = 90
transition_to_glacier_days = 180
noncurrent_version_expiration_days = 30
}
##########################
# DynamoDB Module
##########################
module "dynamodb" {
source = "./modules/dynamodb"
table_name = local.dynamodb_table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "pk"
hash_key_type = "N"
range_key = "sk"
range_key_type = "S"
additional_attributes = [
{ name = "model_name", type = "S" },
{ name = "session_id", type = "S" },
{ name = "is_active", type = "N" },
{ name = "last_message_ts", type = "N" }
]
global_secondary_indexes = [
{
name = "model_index"
hash_key = "model_name"
range_key = "session_id"
projection_type = "ALL"
},
{
name = "active_sessions_index"
hash_key = "is_active"
range_key = "last_message_ts"
projection_type = "ALL"
}
]
ttl_enabled = true
ttl_attribute_name = "ttl"
common_tags = local.common_tags
purpose = "Store user sessions and conversation data"
}
##########################
# Lambda Module
##########################
module "lambda" {
source = "./modules/lambda"
function_name = local.lambda_function_name
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
handler = local.lambda_handler
runtime = local.lambda_runtime
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size
role_arn = var.lab_role_arn
environment_variables = {
TELEGRAM_TOKEN = var.telegram_token
S3_BUCKET_NAME = module.s3.bucket_name
ENVIRONMENT = var.environment
OLLAMA_URL = module.ec2_ollama.ollama_url
OLLAMA_API_KEY = random_password.ollama_api_key.result
}
log_retention_days = var.log_retention_days
common_tags = local.common_tags
purpose = "Telegram bot message handler"
depends_on = [module.s3, module.dynamodb, module.ec2_ollama]
}
##########################
# API Gateway Module
##########################
module "api_gateway" {
source = "./modules/api_gateway"
api_name = local.api_gateway_name
api_description = "API Gateway for Telegram Bot webhook"
endpoint_type = "REGIONAL"
resource_path = "webhook"
http_method = "POST"
authorization = "NONE"
lambda_invoke_arn = module.lambda.invoke_arn
lambda_function_name = module.lambda.function_name
stage_name = var.environment == "prod" ? "prod" : "dev"
common_tags = local.common_tags
depends_on = [module.lambda]
}
##########################
# Monitoring Module
##########################
module "monitoring" {
source = "./modules/monitoring"
function_name = module.lambda.function_name
log_group_name = module.lambda.log_group_name
metric_namespace = "TelegramBot"
error_threshold = 1
evaluation_period_minutes = 5
common_tags = local.common_tags
depends_on = [module.lambda]
}
##########################
# EC2 Ollama Module
##########################
module "ec2_ollama" {
source = "./modules/ec2"
instance_name = local.ec2_instance_name
instance_type = var.ec2_instance_type
key_pair_name = var.ec2_key_pair_name
security_group_name = local.ec2_sg_name
ssh_allowed_cidr = var.ssh_allowed_cidr
ollama_model = var.ollama_model
api_key = random_password.ollama_api_key.result
instance_profile_name = "LabInstanceProfile"
models_s3_bucket = module.s3.bucket_name
models_s3_prefix = local.models_s3_prefix
common_tags = local.common_tags
purpose = "Ollama AI inference server for Telegram bot"
depends_on = [module.s3]
}