diff --git a/accounts/swarms/main.tf b/accounts/swarms/main.tf index 2468b5b..60e68bb 100644 --- a/accounts/swarms/main.tf +++ b/accounts/swarms/main.tf @@ -44,6 +44,18 @@ module "swarms_api" { } +module "swarmdeploy" { + source = "../../environments/swarms-deploy/dev/us-east-1" + domain = local.dns + ami_id = data.aws_ami.ami.id + name = "swarmdeploy" + tags = {project="swarmdeploy"} + vpc_id = "vpc-0b4cedd083227068d" + subnet_id = "subnet-04b3bdd4b0dc877f0" + ssm_profile_arn= "arn:aws:iam::916723593639:instance-profile/ssm-swarms-profile" + ssm_profile_name = "ssm-swarms-profile" +} + output api { value = module.swarms_api } diff --git a/environments/swarms-deploy/dev/us-east-1/components/autoscaling_group/main.tf b/environments/swarms-deploy/dev/us-east-1/components/autoscaling_group/main.tf new file mode 100644 index 0000000..e82fa4a --- /dev/null +++ b/environments/swarms-deploy/dev/us-east-1/components/autoscaling_group/main.tf @@ -0,0 +1,130 @@ +variable aws_iam_instance_profile_ssm_arn {} +#variable target_group_arn{} +variable name {} +variable instance_type {} +variable launch_template_id {} +variable image_id {} +variable vpc_id {} +variable tags {} +variable ec2_subnet_id {} + +locals { + iam_instance_profile_arn = var.aws_iam_instance_profile_ssm_arn + instance_type = var.instance_type +} + +module "autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "8.0.0" + name = var.name + + health_check_type = "EC2" + desired_capacity = 1 + max_size = 5 + min_size = 1 + + create_launch_template = false + update_default_version = true + + launch_template_id = var.launch_template_id + launch_template_version = "$Latest" + + vpc_zone_identifier = [var.ec2_subnet_id] + + instance_market_options = { + market_type = "spot" + } + network_interfaces = [{ + associate_public_ip_address=true + device_index = 0 + delete_on_termination = true + description = "interface1" +# security_groups = [var.security_group_id] + } + ] + instance_type = var.instance_type + image_id = var.image_id + + create_iam_instance_profile = true + iam_role_name = "ssm-${var.name}" + iam_role_path = "/ec2/" + iam_role_description = "SSM IAM role for swarms" + iam_role_tags = { + CustomIamRole = "Yes" + } + + iam_role_policies = { + AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" + } + + # # target_group_arn = + # traffic_source_attachments = { + # ex-alb = { + # traffic_source_identifier = var.target_group_arn + # traffic_source_type = "elbv2" # default + # } + # } + + # Target scaling policy schedule based on average CPU load + scaling_policies = { + avg-cpu-policy-greater-than-50 = { + policy_type = "TargetTrackingScaling" + estimated_instance_warmup = 1200 + target_tracking_configuration = { + predefined_metric_specification = { + predefined_metric_type = "ASGAverageCPUUtilization" + } + target_value = 50.0 + } + }, + predictive-scaling = { + policy_type = "PredictiveScaling" + predictive_scaling_configuration = { + mode = "ForecastAndScale" + scheduling_buffer_time = 10 + max_capacity_breach_behavior = "IncreaseMaxCapacity" + max_capacity_buffer = 10 + metric_specification = { + target_value = 32 + predefined_scaling_metric_specification = { + predefined_metric_type = "ASGAverageCPUUtilization" + resource_label = "testLabel" + } + predefined_load_metric_specification = { + predefined_metric_type = "ASGTotalCPUUtilization" + resource_label = "testLabel" + } + } + } + } + # request-count-per-target = { + # policy_type = "TargetTrackingScaling" + # estimated_instance_warmup = 120 + # target_tracking_configuration = { + # predefined_metric_specification = { + # predefined_metric_type = "ALBRequestCountPerTarget" + # resource_label = "swarms1" + # #"${module.alb.arn_suffix}/${module.alb.target_groups["ex_asg"].arn_suffix}" + # } + # target_value = 800 + # } + # } + scale-out = { + name = "scale-out" + adjustment_type = "ExactCapacity" + policy_type = "StepScaling" + estimated_instance_warmup = 120 + step_adjustment = [ + { + scaling_adjustment = 1 + metric_interval_lower_bound = 0 + metric_interval_upper_bound = 10 + }, + { + scaling_adjustment = 2 + metric_interval_lower_bound = 10 + } + ] + } + } +} diff --git a/environments/swarms-deploy/dev/us-east-1/components/launch_template/main.tf b/environments/swarms-deploy/dev/us-east-1/components/launch_template/main.tf new file mode 100644 index 0000000..ff953d8 --- /dev/null +++ b/environments/swarms-deploy/dev/us-east-1/components/launch_template/main.tf @@ -0,0 +1,80 @@ +variable install_script {} +variable iam_instance_profile_name {} +variable security_group_id {} +variable name {} +variable vpc_id {} +variable ami_id {} +variable tags {} +variable key_name { + default = "mdupont-deployer-key" +} + +# dont use this +variable instance_type {} + +locals { + tags = { + project="swarms" + instance_type = var.instance_type + name = var.name + } +} +resource "aws_launch_template" "ec2_launch_template" { + name_prefix = "${var.name}-launch-template-" + image_id = var.ami_id + key_name = var.key_name + instance_type = var.instance_type + network_interfaces { + associate_public_ip_address = true + delete_on_termination = true + security_groups = [var.security_group_id] + } + + iam_instance_profile { + # iam_instance_profile_arn = aws_iam_instance_profile.ssm.arn + name = var.iam_instance_profile_name #aws_iam_instance_profile.ec2_instance_profile.name + } + lifecycle { + create_before_destroy = true + } + block_device_mappings { + device_name = "/dev/sda1" + ebs { + volume_size = 30 + volume_type = "gp3" + encrypted = true + } + } + + user_data = base64encode(<<-EOF + #!/bin/bash + export HOME=/root + apt update + apt-get install -y ec2-instance-connect git virtualenv + snap install amazon-ssm-agent --classic || echo oops1 + snap start amazon-ssm-agent || echo oops2 + apt-get install -y --no-install-recommends ca-certificates=20230311 curl=7.88.1-10+deb12u7 | echo oops + curl -O "https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/$(dpkg --print-architecture)/latest/amazon-cloudwatch-agent.deb" + dpkg -i -E amazon-cloudwatch-agent.deb + + if [ ! -d "/opt/swarms/" ]; then + git clone https://github.com/jmikedupont2/SwarmDeploy "/opt/swarms/" + fi + cd "/opt/swarms/" || exit 1 + export BRANCH=main + git stash + git checkout --force $BRANCH + git pull # get the latest version + bash -x ${var.install_script} + EOF + ) + tags = var.tags +} + + +output "lt" { + value = resource.aws_launch_template.ec2_launch_template +} +output "launch_template_id" { + value = resource.aws_launch_template.ec2_launch_template.id +} diff --git a/environments/swarms-deploy/dev/us-east-1/main.tf b/environments/swarms-deploy/dev/us-east-1/main.tf new file mode 100644 index 0000000..6e69834 --- /dev/null +++ b/environments/swarms-deploy/dev/us-east-1/main.tf @@ -0,0 +1,172 @@ +variable "ssm_profile_arn" {} +variable "ssm_profile_name" {} +variable vpc_id {} +variable subnet_id {} +locals { + # instance_type = "t3.large" + # instance_type = "t3.medium" + ami_name = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*" + name = "swarmdeploy" + region = "us-east-2" + domain = var.domain + tags = { + project="swarmdeploy" + } +} +variable domain {} +variable ami_id {} +variable tags {} +variable name {} + + +locals { + ami_id = var.ami_id + #new_ami_id = "ami-08093b6770af41b14" # environments/swarms-aws-agent-api/dev/us-east-1/components/machine_image/Readme.md +} + +# SLOW +locals { + root = "../../../swarms-aws-agent-api/dev/us-east-1/components/" +} +#module "vpc" { +# source = "${local.root}/vpc" +#} + +data "aws_vpc" "vpc" { + id = var.vpc_id +} + +locals { +# ec2_public_subnet_id_1 = module.vpc.ec2_public_subnet_id_1 +# ec2_public_subnet_id_2 = module.vpc.ec2_public_subnet_id_2 + #vpc_id = module.vpc.vpc_id + vpc_id = var.vpc_id +} + +module "security" { + source = "${local.root}/security" + vpc_id = local.vpc_id + tags = local.tags + name = local.name +} + +#module "kp" { +# source = "${local.root}/keypairs" +#} + +# module "lt" { +# instance_type = local.instance_type +# security_group_id = module.security.security_group_id +# source = "./components/launch_template" +# } + +# module "asg" { +# source = "./components/autoscaling_group" +# name="swarms" +# security_group_id = module.security.security_group_id +# instance_type = local.instance_type +# launch_template_id = module.lt.launch_template_id +# } + +variable "instance_types" { + type = list(string) + default = [ + # "t4g.nano", "t3a.nano", "t3.nano", "t2.nano", + # "t4g.micro", "t3a.micro", "t3.micro", "t2.micro", "t1.micro", + #"t4g.small", "t3a.small", + #"t3.small", + #"t2.small", not working + # "t2.medium" # + "t3.medium" + ] +} + +#module "roles" { +# source = "${local.root}/roles" +# +# tags = local.tags +#} + +module "lt_dynamic" { + vpc_id = local.vpc_id + for_each = toset(var.instance_types) + instance_type = each.key + name = "swarms-size-${each.key}" + security_group_id = module.security.internal_security_group_id + ami_id = var.ami_id + tags= local.tags + source = "./components/launch_template" + iam_instance_profile_name = var.ssm_profile_name + #aws_iam_instance_profile.ssm.name + install_script = "/opt/swarms/install.sh" +} + +# module "lt_dynamic_ami" { +# vpc_id = local.vpc_id +# for_each = toset(var.instance_types) +# instance_type = each.key +# name = "swarms-ami-${each.key}" +# security_group_id = module.security.internal_security_group_id +# ami_id = local.new_ami_id +# tags= local.tags +# source = "./components/launch_template" +# iam_instance_profile_name = module.roles.ssm_profile_name +# #aws_iam_instance_profile.ssm.name +# install_script = "/opt/swarms/api/just_run.sh" +# } + +output security_group_id { + value = module.security.security_group_id +} + +#output vpc { +# value = module.vpc +#} + +# module "alb" { +# source = "${local.root}/application_load_balancer" +# domain_name = local.domain +# security_group_id = module.security.security_group_id # allowed to talk to internal +# public_subnets = [ +# local.ec2_public_subnet_id_1, +# local.ec2_public_subnet_id_2 ] +# vpc_id = local.vpc_id +# name = local.name +# } + +# output alb { +# value = module.alb +# } + +module "asg_dynamic" { + tags = local.tags + vpc_id = local.vpc_id + image_id = local.ami_id + ec2_subnet_id = var.subnet_id + for_each = toset(var.instance_types) + aws_iam_instance_profile_ssm_arn = var.ssm_profile_arn + #iam_instance_profile_name = module.roles.ssm_profile_name + source = "./components/autoscaling_group" + # security_group_id = module.security.internal_security_group_id + instance_type = each.key + name = "swarmdeploy-${each.key}" + launch_template_id = module.lt_dynamic[each.key].launch_template_id +# target_group_arn = module.alb.alb_target_group_arn +} + +# module "asg_dynamic_new_ami" { +# # built with packer +# #count =0 +# tags = local.tags +# vpc_id = local.vpc_id +# image_id = local.new_ami_id +# ec2_subnet_id = module.vpc.ec2_public_subnet_id_1 +# for_each = toset(var.instance_types) +# aws_iam_instance_profile_ssm_arn = module.roles.ssm_profile_arn +# source = "./components/autoscaling_group" +# # security_group_id = module.security.internal_security_group_id +# instance_type = each.key +# name = "swarms-ami-${each.key}" +# launch_template_id = module.lt_dynamic_ami[each.key].launch_template_id +# target_group_arn = module.alb.alb_target_group_arn +# } diff --git a/environments/swarms-deploy/main.tf b/environments/swarms-deploy/main.tf new file mode 100644 index 0000000..8fa2136 --- /dev/null +++ b/environments/swarms-deploy/main.tf @@ -0,0 +1,2 @@ +# +