Skip to content

Commit

Permalink
router
Browse files Browse the repository at this point in the history
  • Loading branch information
peytontolbert committed Mar 14, 2024
1 parent f80a3e2 commit d5467a2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 48 deletions.
Binary file added terraform.zip
Binary file not shown.
10 changes: 0 additions & 10 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ resource "aws_key_pair" "ssh_key" {
public_key = file("cog_rsa.pub") # path to your public key
}

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}

resource "aws_security_group" "model_api_sg" {
name = "model_api_sg"
description = "Security group for model API EC2 instances"
Expand Down
4 changes: 4 additions & 0 deletions terraform/main10.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ resource "aws_iam_role" "lambda_exec_role" {

# Deploy API Gateway
resource "aws_api_gateway_deployment" "api_deployment" {
depends_on = [
aws_api_gateway_method.model_post_method,
aws_api_gateway_resource.model_routing_resource
]
rest_api_id = aws_api_gateway_rest_api.model_routing_api.id
stage_name = "prod"
# Other configurations
Expand Down
8 changes: 8 additions & 0 deletions terraform/main2.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}

resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
Expand Down
32 changes: 0 additions & 32 deletions terraform/main5.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,3 @@ resource "aws_iam_role_policy_attachment" "ssm" {
role = aws_iam_role.ssm.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}


resource "aws_launch_configuration" "model_api_conf" {
name_prefix = "model-api-"
image_id = "ami-048eeb679c8e04a87"
instance_type = "p3.2xlarge" # Choose an appropriate instance type
security_groups = [aws_security_group.model_api_sg.id]
iam_instance_profile = aws_iam_instance_profile.ssm.name
key_name = aws_key_pair.ssh_key.key_name # Add this line

root_block_device {
volume_size = 130
delete_on_termination = true
volume_type = "gp2" # General Purpose SSD
}

user_data = <<-EOF
#!/bin/bash
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
# Install the latest version of Docker CE and containerd
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
sudo docker pull public.ecr.aws/d6u1k1m2/cogvlmpub:latest
sudo docker run --gpus all -d -p 8000:8000 public.ecr.aws/d6u1k1m2/cogvlmpub:latest
EOF

lifecycle {
create_before_destroy = true
}
}
30 changes: 30 additions & 0 deletions terraform/main6.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
resource "aws_launch_configuration" "model_api_conf" {
name_prefix = "model-api-"
image_id = "ami-048eeb679c8e04a87"
instance_type = "p3.2xlarge" # Choose an appropriate instance type
security_groups = [aws_security_group.model_api_sg.id]
iam_instance_profile = aws_iam_instance_profile.ssm.name
key_name = aws_key_pair.ssh_key.key_name # Add this line

root_block_device {
volume_size = 130
delete_on_termination = true
volume_type = "gp2" # General Purpose SSD
}

user_data = <<-EOF
#!/bin/bash
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
# Install the latest version of Docker CE and containerd
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
sudo docker pull public.ecr.aws/d6u1k1m2/cogvlmpub:latest
sudo docker run --gpus all -d -p 8000:8000 public.ecr.aws/d6u1k1m2/cogvlmpub:latest
EOF

lifecycle {
create_before_destroy = true
}
}

resource "aws_launch_template" "k8s_worker_lt" {
name_prefix = "k8s-worker-"
Expand Down
38 changes: 32 additions & 6 deletions terraform/main9.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# AWS Lambda function for request routing
resource "aws_lambda_function" "request_router" {
function_name = "requestRouter"
# other configurations like handler, runtime, and environment variables
# Your function's source code would route the requests based on the model name
handler = "index.handler" # make sure this handler aligns with your code's entry point
role = aws_iam_role.lambda_exec.arn
runtime = "nodejs14.x" # or whatever runtime you're using

s3_bucket = "swarmslambda"
s3_key = "code.zip"

environment {
variables = {
COGVLM_ENDPOINT = "http://model1-service",
QWENVL_ENDPOINT = "http://model2-service",
// Add more environment variables as necessary
COGVLM_ENDPOINT = "http://${aws_lb.cogvlm_service.dns_name}",
QWENVL_ENDPOINT = "http://${aws_lb.qwenvl_service.dns_name}",
}
}
}


# API Gateway to expose the Lambda Function
resource "aws_api_gateway_rest_api" "model_routing_api" {
name = "ModelRoutingAPI"
Expand Down Expand Up @@ -40,6 +43,29 @@ resource "aws_api_gateway_integration" "model_lambda_integration" {
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.request_router.invoke_arn
integration_http_method = "POST" # Use the POST method for Lambda invocation
}

resource "aws_api_gateway_deployment" "api_deployment" {
depends_on = [
aws_api_gateway_integration.lambda_integration
]

rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "v1"
}

resource "aws_lambda_permission" "api_lambda_permission" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.function_name
principal = "apigateway.amazonaws.com"
# The /*/* allows for all methods and resources
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*"
}

resource "aws_api_gateway_stage" "api_stage" {
deployment_id = aws_api_gateway_deployment.api_deployment.id
rest_api_id = aws_api_gateway_rest_api.model_routing_api.id
stage_name = "v1"
}

0 comments on commit d5467a2

Please sign in to comment.