Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
WIP for terraform files
elijahbenizzy1927d046d766
diff --git a/burr/tracking/server/s3/deployment/Dockerfile b/burr/tracking/server/s3/deployment/Dockerfileindex 0d2c63ba..2d7c2c7a 100644--- a/burr/tracking/server/s3/deployment/Dockerfile+++ b/burr/tracking/server/s3/deployment/Dockerfile@@ -1,6 +1,7 @@# Use an official Python runtime as a parent imageFROM python:3.11-slim+# Set environment variablesENV PYTHONUNBUFFERED=1@@ -14,12 +15,13 @@ COPY . /appRUN apt-get update && apt-get install -y \git \nginx \+ gcc \&& apt-get clean# Install the dependencies# TODO -- use the right version#RUN pip install "git+https://github.com/dagworks-inc/burr.git@tracker-s3#egg=burr[tracking-server-s3]"-RUN pip install "burr[tracking-server-s3]==0.26.0rc4"+RUN pip install "burr[tracking-server-s3]==0.26.3rc0"# Copy the nginx config fileCOPY nginx.conf /etc/nginx/nginx.conf@@ -31,7 +33,7 @@ EXPOSE 80ENV BURR_S3_BUCKET=burr-prod-testENV BURR_load_snapshot_on_start=TrueENV BURR_snapshot_interval_milliseconds=3_600_000-ENV BURR_update_interval_milliseconds=120_000+ENV BURR_update_interval_milliseconds=150_000ENV BURR_BACKEND_IMPL=s3ENV ENV DEBIAN_FRONTEND=noninteractiveENV BURR_BACKEND_IMPL=burr.tracking.server.s3.backend.SQLiteS3Backenddiff --git a/burr/tracking/server/s3/deployment/terraform/.gitignore b/burr/tracking/server/s3/deployment/terraform/.gitignorenew file mode 100644index 00000000..b41b8184--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/.gitignore@@ -0,0 +1,10 @@+# Compiled files+*.tfstate+*.tfstate.backup+*.tfvars+.terraform.tfstate.lock.info++++# Module directory+.terraform/diff --git a/burr/tracking/server/s3/deployment/terraform/.terraform.lock.hcl b/burr/tracking/server/s3/deployment/terraform/.terraform.lock.hclnew file mode 100644index 00000000..c1b586a8--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/.terraform.lock.hcl@@ -0,0 +1,31 @@+# This file is maintained automatically by "terraform init".+# Manual edits may be lost in future updates.++provider "registry.terraform.io/hashicorp/aws" {+ version = "5.61.0"+ hashes = [+ "h1:qYXhPfMOxgOYuSjfe7+P2wdqx4oMkPYgH4XUN3fJb54=",+ "zh:1a0a150b6adaeacc8f56763182e76c6219ac67de1217b269d24b770067b7bab0",+ "zh:1d9c3a8ac3934a147569254d6e2e6ea5293974d0595c02c9e1aa31499a8f0042",+ "zh:1f4d1d5e2e02fd5cccafa28dade8735a3059ed1ca3284fb40116cdb67d0e7ee4",+ "zh:26be6f759bded469de477f54c7eb7a9ca9f137a3b52f9fd26cbd864f16973912",+ "zh:276e308ae7aa281fe24f7275673aa05f00cb830b83c2b9797f9aa55f10769c52",+ "zh:45c09beeadb4269d518de0bd341cbe9f061157ab54c543d39168ecefff40bbe2",+ "zh:58fb5ef076dc63e284ce28b47b7cc35a17d2596f11e2373fe568c6140277e9d8",+ "zh:64d51cc1ad412379f64b75883a881a5d682a8e9737ad14479f6a2d62e77f7dbe",+ "zh:71e2e332317cf095288d65a801e95b65fd696204997b2db5250862d6c5669518",+ "zh:9864014aa4716b5bfb3b27d009f158dd6a67c215fd0dfbe3a5d1a7cee72c5677",+ "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",+ "zh:d8bf9ba43bd938faab37d8fb515c32a905d6dace60f5ff2663b06ffdc89a62e9",+ "zh:e654be9d3980e7cc70f9825fe0d0205e254edd87832f18b2d7f9c72b09b776cd",+ "zh:ee5ce6fbe75be3e90cabba3fad76fcfde50ab795e523b4ee917cfe8ba8ad42fe",+ "zh:ef12098e7b3ddf9ab286bb209de87dfa8e52106049ced0841e3e6487dbff3659",+ ]+}++provider "registry.terraform.io/hashicorp/template" {+ version = "2.2.0"+ hashes = [+ "h1:fWFikA+NOeeN7c0so3ULsaMyLlkJ8USk80pqRaSuj48=",+ ]+}diff --git a/burr/tracking/server/s3/deployment/terraform/alb.tf b/burr/tracking/server/s3/deployment/terraform/alb.tfnew file mode 100644index 00000000..cb1b0b14--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/alb.tf@@ -0,0 +1,35 @@+resource "aws_alb" "main" {+ name = "burr-load-balancer"+ subnets = aws_subnet.public.*.id+ security_groups = [aws_security_group.lb.id]+}++resource "aws_alb_target_group" "app" {+ name = "burr-target-group"+ port = 80+ protocol = "HTTP"+ vpc_id = aws_vpc.main.id+ target_type = "ip"++ health_check {+ healthy_threshold = "3"+ interval = "30"+ protocol = "HTTP"+ matcher = "200"+ timeout = "3"+ path = var.health_check_path+ unhealthy_threshold = "2"+ }+}++# Redirect all traffic from the ALB to the target group+resource "aws_alb_listener" "front_end" {+ load_balancer_arn = aws_alb.main.id+ port = var.app_port+ protocol = "HTTP"++ default_action {+ target_group_arn = aws_alb_target_group.app.id+ type = "forward"+ }+}diff --git a/burr/tracking/server/s3/deployment/terraform/auto_scaling.tf b/burr/tracking/server/s3/deployment/terraform/auto_scaling.tfnew file mode 100644index 00000000..39aa5a96--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/auto_scaling.tf@@ -0,0 +1,90 @@+# auto_scaling.tf++resource "aws_appautoscaling_target" "target" {+ service_namespace = "ecs"+ resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"+ scalable_dimension = "ecs:service:DesiredCount"+ role_arn = aws_iam_role.ecs_auto_scale_role.arn+ min_capacity = 1+ max_capacity = 2+}++# Automatically scale capacity up by one+resource "aws_appautoscaling_policy" "up" {+ name = "burr_scale_up"+ service_namespace = "ecs"+ resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"+ scalable_dimension = "ecs:service:DesiredCount"++ step_scaling_policy_configuration {+ adjustment_type = "ChangeInCapacity"+ cooldown = 60+ metric_aggregation_type = "Maximum"++ step_adjustment {+ metric_interval_lower_bound = 0+ scaling_adjustment = 1+ }+ }++ depends_on = [aws_appautoscaling_target.target]+}++# Automatically scale capacity down by one+resource "aws_appautoscaling_policy" "down" {+ name = "burr_scale_down"+ service_namespace = "ecs"+ resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"+ scalable_dimension = "ecs:service:DesiredCount"++ step_scaling_policy_configuration {+ adjustment_type = "ChangeInCapacity"+ cooldown = 60+ metric_aggregation_type = "Maximum"++ step_adjustment {+ metric_interval_lower_bound = 0+ scaling_adjustment = -1+ }+ }++ depends_on = [aws_appautoscaling_target.target]+}++# CloudWatch alarm that triggers the autoscaling up policy+resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {+ alarm_name = "burr_cpu_utilization_high"+ comparison_operator = "GreaterThanOrEqualToThreshold"+ evaluation_periods = "2"+ metric_name = "CPUUtilization"+ namespace = "AWS/ECS"+ period = "60"+ statistic = "Average"+ threshold = "85"++ dimensions = {+ ClusterName = aws_ecs_cluster.main.name+ ServiceName = aws_ecs_service.main.name+ }++ alarm_actions = [aws_appautoscaling_policy.up.arn]+}++# CloudWatch alarm that triggers the autoscaling down policy+resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {+ alarm_name = "burr_cpu_utilization_low"+ comparison_operator = "LessThanOrEqualToThreshold"+ evaluation_periods = "2"+ metric_name = "CPUUtilization"+ namespace = "AWS/ECS"+ period = "60"+ statistic = "Average"+ threshold = "10"++ dimensions = {+ ClusterName = aws_ecs_cluster.main.name+ ServiceName = aws_ecs_service.main.name+ }++ alarm_actions = [aws_appautoscaling_policy.down.arn]+}diff --git a/burr/tracking/server/s3/deployment/terraform/ecs.tf b/burr/tracking/server/s3/deployment/terraform/ecs.tfnew file mode 100644index 00000000..0c9e6b12--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/ecs.tf@@ -0,0 +1,56 @@++resource "aws_ecs_cluster" "main" {+ name = "burr-cluster"+}++data "template_file" "burr_app" {+ template = file("./templates/ecs/burr_app.json.tpl")++ vars = {+ app_image = var.app_image+ app_port = var.app_port+ fargate_cpu = var.fargate_cpu+ fargate_memory = var.fargate_memory+ aws_region = var.aws_region+ }+}++resource "aws_ecs_task_definition" "app" {+ family = "burr-app-task"+ # TODO -- customize/create execution role+ execution_role_arn = aws_iam_role.ecs_task_execution_role.arn+ task_role_arn = aws_iam_role.ecs_task_role.arn+ network_mode = "awsvpc"+ requires_compatibilities = ["FARGATE"]+ cpu = var.fargate_cpu+ memory = var.fargate_memory+ container_definitions = data.template_file.burr_app.rendered+}++resource "aws_ecs_service" "main" {+ name = "burr-service"+ cluster = aws_ecs_cluster.main.id+ task_definition = aws_ecs_task_definition.app.arn+ desired_count = var.app_count+ launch_type = "FARGATE"++ network_configuration {+ security_groups = [aws_security_group.ecs_tasks.id]+ subnets = aws_subnet.private.*.id+ assign_public_ip = true+ }++ load_balancer {+ target_group_arn = aws_alb_target_group.app.id+ container_name = "burr-app"+ container_port = var.app_port+ }++ depends_on = [+ aws_alb_listener.front_end,+ aws_iam_role_policy_attachment.ecs_task_execution_role_policy_attachment,+ aws_iam_role_policy_attachment.ecs_task_role_policy_attachment,+ aws_iam_role_policy_attachment.task_s3,+ # aws_iam_role_policy_attachment.task_s3_execution_role,+ ]+}diff --git a/burr/tracking/server/s3/deployment/terraform/iam.tf b/burr/tracking/server/s3/deployment/terraform/iam.tfnew file mode 100644index 00000000..cf333719--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/iam.tf@@ -0,0 +1,119 @@+resource "aws_iam_role" "ecs_task_execution_role" {+ name = "burr-ecs"++ assume_role_policy = <<EOF+{+ "Version": "2012-10-17",+ "Statement": [+ {+ "Action": "sts:AssumeRole",+ "Principal": {+ "Service": "ecs-tasks.amazonaws.com"+ },+ "Effect": "Allow",+ "Sid": ""+ }+ ]+}+EOF+}+resource "aws_iam_role" "ecs_task_role" {+ name = "burr-ecs-task"++ assume_role_policy = <<EOF+{+ "Version": "2012-10-17",+ "Statement": [+ {+ "Action": "sts:AssumeRole",+ "Principal": {+ "Service": "ecs-tasks.amazonaws.com"+ },+ "Effect": "Allow",+ "Sid": ""+ }+ ]+}+EOF+}++resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attachment" {+ role = aws_iam_role.ecs_task_execution_role.name+ policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"+}++resource "aws_iam_role_policy_attachment" "ecs_task_role_policy_attachment" {+ role = aws_iam_role.ecs_task_role.name+ policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"+}++resource "aws_iam_policy" "custom_s3_policy" {+ name = "custom-s3-policy"+ description = "Custom policy for read/write/metadata access to burr-prod-test S3 bucket"+ policy = jsonencode({+ "Version": "2012-10-17",+ "Statement": [+ {+ "Effect": "Allow",+ "Action": [+ "s3:GetObject",+ "s3:PutObject",+ "s3:ListBucket",+ "s3:GetBucketLocation",+ "s3:DeleteObject",+ "s3:PutObjectTagging",+ "s3:GetObjectTagging",+ "s3:DeleteObjectTagging",+ "s3:GetObjectVersion",+ "s3:ListBucketVersions",+ "s3:ListBucketMultipartUploads",+ "s3:AbortMultipartUpload"+ ],+ "Resource": [+ "arn:aws:s3:::burr-prod-test",+ "arn:aws:s3:::burr-prod-test/*"+ ]+ }+ ]+ })+}++resource "aws_iam_role_policy_attachment" "task_s3" {+ role = aws_iam_role.ecs_task_role.name+ policy_arn = aws_iam_policy.custom_s3_policy.arn+}++# resource "aws_iam_role_policy_attachment" "task_s3" {+# role = aws_iam_role.ecs_task_role.name+# policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"+# }++# resource "aws_iam_role_policy_attachment" "task_s3_execution_role" {+# role = "${aws_iam_role.ecs_task_execution_role.name}"+# policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"+# }++data "aws_iam_policy_document" "ecs_auto_scale_role" {+ version = "2012-10-17"+ statement {+ effect = "Allow"+ actions = ["sts:AssumeRole"]++ principals {+ type = "Service"+ identifiers = ["application-autoscaling.amazonaws.com"]+ }+ }+}++# ECS auto scale role+resource "aws_iam_role" "ecs_auto_scale_role" {+ name = var.ecs_auto_scale_role_name+ assume_role_policy = data.aws_iam_policy_document.ecs_auto_scale_role.json+}++# ECS auto scale role policy attachment+resource "aws_iam_role_policy_attachment" "ecs_auto_scale_role" {+ role = aws_iam_role.ecs_auto_scale_role.name+ policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceAutoscaleRole"+}diff --git a/burr/tracking/server/s3/deployment/terraform/logs.tf b/burr/tracking/server/s3/deployment/terraform/logs.tfnew file mode 100644index 00000000..03d9ecf6--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/logs.tf@@ -0,0 +1,16 @@+# logs.tf++# Set up CloudWatch group and log stream and retain logs for 30 days+resource "aws_cloudwatch_log_group" "burr_log_group" {+ name = "/ecs/burr-app"+ retention_in_days = 30++ tags = {+ Name = "burr-log-group"+ }+}++resource "aws_cloudwatch_log_stream" "burr_log_stream" {+ name = "burr-log-stream"+ log_group_name = aws_cloudwatch_log_group.burr_log_group.name+}diff --git a/burr/tracking/server/s3/deployment/terraform/network.tf b/burr/tracking/server/s3/deployment/terraform/network.tfnew file mode 100644index 00000000..80934198--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/network.tf@@ -0,0 +1,71 @@+# network.tf++# Fetch AZs in the current region+data "aws_availability_zones" "available" {+}++resource "aws_vpc" "main" {+ # cidr_block = "172.17.0.0/16"+ cidr_block = "172.31.16.0/20"+ # ipv4_netmask_length = 20+}++# Create var.az_count private subnets, each in a different AZ+resource "aws_subnet" "private" {+ count = var.az_count+ cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)+ availability_zone = data.aws_availability_zones.available.names[count.index]+ vpc_id = aws_vpc.main.id+}++# Create var.az_count public subnets, each in a different AZ+resource "aws_subnet" "public" {+ count = var.az_count+ cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, var.az_count + count.index)+ availability_zone = data.aws_availability_zones.available.names[count.index]+ vpc_id = aws_vpc.main.id+ map_public_ip_on_launch = true+}++# Internet Gateway for the public subnet+resource "aws_internet_gateway" "gw" {+ vpc_id = aws_vpc.main.id+}++# Route the public subnet traffic through the IGW+resource "aws_route" "internet_access" {+ route_table_id = aws_vpc.main.main_route_table_id+ destination_cidr_block = "0.0.0.0/0"+ gateway_id = aws_internet_gateway.gw.id+}++# Create a NAT gateway with an Elastic IP for each private subnet to get internet connectivity+resource "aws_eip" "gw" {+ count = var.az_count+ domain = "vpc"+ depends_on = [aws_internet_gateway.gw]+}++resource "aws_nat_gateway" "gw" {+ count = var.az_count+ subnet_id = element(aws_subnet.public.*.id, count.index)+ allocation_id = element(aws_eip.gw.*.id, count.index)+}++# Create a new route table for the private subnets, make it route non-local traffic through the NAT gateway to the internet+resource "aws_route_table" "private" {+ count = var.az_count+ vpc_id = aws_vpc.main.id++ route {+ cidr_block = "0.0.0.0/0"+ nat_gateway_id = element(aws_nat_gateway.gw.*.id, count.index)+ }+}++# Explicitly associate the newly created route tables to the private subnets (so they don't default to the main route table)+resource "aws_route_table_association" "private" {+ count = var.az_count+ subnet_id = element(aws_subnet.private.*.id, count.index)+ route_table_id = element(aws_route_table.private.*.id, count.index)+}diff --git a/burr/tracking/server/s3/deployment/terraform/outputs.tf b/burr/tracking/server/s3/deployment/terraform/outputs.tfnew file mode 100644index 00000000..323347bb--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/outputs.tf@@ -0,0 +1,3 @@+output "alb_hostname" {+ value = "${aws_alb.main.dns_name}:3000"+}diff --git a/burr/tracking/server/s3/deployment/terraform/provider.tf b/burr/tracking/server/s3/deployment/terraform/provider.tfnew file mode 100644index 00000000..8909ec46--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/provider.tf@@ -0,0 +1,7 @@+# Specify the provider and access details++provider "aws" {+ access_key = var.aws_access_key+ secret_key = var.aws_secret_key+ region = var.aws_region+}diff --git a/burr/tracking/server/s3/deployment/terraform/security.tf b/burr/tracking/server/s3/deployment/terraform/security.tfnew file mode 100644index 00000000..547b5184--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/security.tf@@ -0,0 +1,41 @@+# ALB security Group: Edit to restrict access to the application+resource "aws_security_group" "lb" {+ name = "burr-load-balancer-security-group"+ description = "controls access to the ALB"+ vpc_id = aws_vpc.main.id++ ingress {+ protocol = "tcp"+ from_port = var.app_port+ to_port = var.app_port+ cidr_blocks = ["0.0.0.0/0"]+ }++ egress {+ protocol = "-1"+ from_port = 0+ to_port = 0+ cidr_blocks = ["0.0.0.0/0"]+ }+}++# Traffic to the ECS cluster should only come from the ALB+resource "aws_security_group" "ecs_tasks" {+ name = "burr-ecs-tasks-security-group"+ description = "allow inbound access from the ALB only"+ vpc_id = aws_vpc.main.id++ ingress {+ protocol = "tcp"+ from_port = var.app_port+ to_port = var.app_port+ security_groups = [aws_security_group.lb.id]+ }++ egress {+ protocol = "-1"+ from_port = 0+ to_port = 0+ cidr_blocks = ["0.0.0.0/0"]+ }+}diff --git a/burr/tracking/server/s3/deployment/terraform/templates/ecs/burr_app.json.tpl b/burr/tracking/server/s3/deployment/terraform/templates/ecs/burr_app.json.tplnew file mode 100644index 00000000..ba610dba--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/templates/ecs/burr_app.json.tpl@@ -0,0 +1,23 @@+[+ {+ "name": "burr-app",+ "image": "${app_image}",+ "cpu": ${fargate_cpu},+ "memory": ${fargate_memory},+ "networkMode": "awsvpc",+ "logConfiguration": {+ "logDriver": "awslogs",+ "options": {+ "awslogs-group": "/ecs/burr-app",+ "awslogs-region": "${aws_region}",+ "awslogs-stream-prefix": "ecs"+ }+ },+ "portMappings": [+ {+ "containerPort": ${app_port},+ "hostPort": ${app_port}+ }+ ]+ }+]diff --git a/burr/tracking/server/s3/deployment/terraform/variable.tf b/burr/tracking/server/s3/deployment/terraform/variable.tfnew file mode 100644index 00000000..6ab5861f--- /dev/null+++ b/burr/tracking/server/s3/deployment/terraform/variable.tf@@ -0,0 +1,59 @@+# variables.tf++variable "aws_access_key" {+ description = "The IAM public access key"+}++variable "aws_secret_key" {+ description = "IAM secret access key"+}++variable "aws_region" {+ description = "The AWS region things are created in"+}++variable "ec2_task_execution_role_name" {+ description = "ECS task execution role name"+ default = "myEcsTaskExecutionRole"+}++variable "ecs_auto_scale_role_name" {+ description = "ECS auto scale role name"+ default = "myEcsAutoScaleRole"+}++variable "az_count" {+ description = "Number of AZs to cover in a given region"+ default = "2"+}++# TODO -- get this to use a public image that is pre-built+variable "app_image" {+ description = "Docker image to run in the ECS cluster"+ default = "929301070231.dkr.ecr.us-west-2.amazonaws.com/burr-prod-test:latest"+}++variable "app_port" {+ description = "Port exposed by the docker image to redirect traffic to"+ default = 8000++}++variable "app_count" {+ description = "Number of docker containers to run"+ default = 1+}++variable "health_check_path" {+ default = "/ready"+}++variable "fargate_cpu" {+ description = "Fargate instance CPU units to provision (1 vCPU = 1024 CPU units)"+ default = "1024"+}++variable "fargate_memory" {+ description = "Fargate instance memory to provision (in MiB)"+ default = "2048"+}