Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarmdeploy #9

Open
wants to merge 3 commits into
base: feature/swarms.ai
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions accounts/swarms/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Loading