Skip to content

feat: added support for deploying instances into multiple instance types #52

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

Open
wants to merge 1 commit into
base: main
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
27 changes: 24 additions & 3 deletions asg.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,31 @@ resource "aws_autoscaling_group" "main" {
desired_capacity = 1
health_check_type = "EC2"
vpc_zone_identifier = [var.subnet_id]
capacity_rebalance = var.use_spot_instances && length(var.instance_types) > 1 ? true : false

launch_template {
id = aws_launch_template.main.id
version = "$Latest"
dynamic "mixed_instances_policy" {
for_each = length(var.instance_types) > 0 ? [1] : []
content {
instances_distribution {
on_demand_base_capacity = var.use_spot_instances ? 0 : 1
on_demand_percentage_above_base_capacity = var.use_spot_instances ? 0 : 100
spot_allocation_strategy = "price-capacity-optimized"
}

launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.main.id
version = "$Latest"
}

dynamic "override" {
for_each = var.instance_types
content {
instance_type = override.value
}
}
}
}
}

dynamic "tag" {
Expand Down
10 changes: 1 addition & 9 deletions ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "aws_launch_template" "main" {
#checkov:skip=CKV_AWS_88:NAT instances must have a public IP.
name = var.name
image_id = local.ami_id
instance_type = var.instance_type
instance_type = length(var.instance_types) > 0 ? var.instance_types[0] : null
key_name = var.ssh_key_name

block_device_mappings {
Expand All @@ -60,14 +60,6 @@ resource "aws_launch_template" "main" {
security_groups = local.security_groups
}

dynamic "instance_market_options" {
for_each = var.use_spot_instances ? ["x"] : []

content {
market_type = "spot"
}
}

dynamic "tag_specifications" {
for_each = ["instance", "network-interface", "volume"]

Expand Down
4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
is_arm = can(regex("[a-zA-Z]+\\d+g[a-z]*\\..+", var.instance_type))
is_arm = can(regex("[a-zA-Z]+\\d+g[a-z]*\\..+", var.instance_types[0]))
ami_id = var.ami_id != null ? var.ami_id : data.aws_ami.main[0].id
cwagent_param_arn = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? var.cloudwatch_agent_configuration_param_arn : aws_ssm_parameter.cloudwatch_agent_config[0].arn : null
cwagent_param_name = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? split("/", data.aws_arn.ssm_param[0].resource)[1] : aws_ssm_parameter.cloudwatch_agent_config[0].name : null
Expand Down Expand Up @@ -80,4 +80,4 @@ resource "aws_ssm_parameter" "cloudwatch_agent_config" {
METRICS_NAMESPACE = var.cloudwatch_agent_configuration.namespace
METRICS_ENDPOINT_OVERRIDE = var.cloudwatch_agent_configuration.endpoint_override
})
}
}
6 changes: 3 additions & 3 deletions output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ output "ha_mode" {
value = var.ha_mode
}

output "instance_type" {
description = "Instance type used for the fck-nat instance"
value = aws_launch_template.main.instance_type
output "instance_types" {
description = "Instance types used for the fck-nat instance (from the ASG mixed instances policy or launch template default)"
value = length(var.instance_types) > 0 ? var.instance_types : [aws_launch_template.main.instance_type]
}

output "ami_id" {
Expand Down
8 changes: 4 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ variable "ha_mode" {
default = true
}

variable "instance_type" {
description = "Instance type to use for the NAT instance"
type = string
default = "t4g.micro"
variable "instance_types" {
description = "List of instance types to use for the NAT instance (ASG mixed instances policy)"
type = list(string)
default = ["t4g.nano"]
}

variable "ami_id" {
Expand Down