Created
June 5, 2020 10:20
-
-
Save paranoidd/8f45de6b777f235251ddb9c747f0ceb0 to your computer and use it in GitHub Desktop.
spotinst_custom_attributes_code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
locals { | |
cluster_name = "${var.owner}-${var.env}" | |
project = "ecs-${var.owner}" | |
default_tags = { | |
env = var.env | |
project = local.project | |
owner = var.owner | |
Name = local.cluster_name | |
vcs = var.vcs | |
Terraformed = "true" | |
Ansible = "false" | |
tf_module = "https://bitbucket.org/org/repo2" | |
} | |
spotinst_autoscaler_attributes = [ | |
{ | |
key = "env" | |
value = local.default_tags["env"] | |
}, | |
{ | |
key = "project" | |
value = local.default_tags["project"] | |
}, | |
{ | |
key = "owner" | |
value = local.default_tags["owner"] | |
}, | |
{ | |
key = "Name" | |
value = "ecs-node-spotinst-${local.cluster_name}" | |
}, | |
{ | |
key = "ecs-cluster" | |
value = "${local.cluster_name}" | |
} | |
] | |
} | |
# Using AWS ECS optimized ami <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html> | |
data "aws_ami" "amazon_linux_ecs" { | |
most_recent = true | |
filter { | |
name = "name" | |
values = ["amzn2-ami-ecs-hvm-*-x86_64-ebs"] | |
} | |
filter { | |
name = "owner-alias" | |
values = ["amazon"] | |
} | |
owners = ["591542846629"] # AWS Account id | |
} | |
data "template_file" "user_data" { | |
template = file("${path.module}/templates/user-data.sh") | |
vars = { | |
cluster_name = "${var.owner}-${var.env}" | |
user_data = var.user_data | |
persistent_disk_id = aws_efs_file_system.ecs_persistent_disk.id | |
} | |
} | |
resource "spotinst_elastigroup_aws" "default" { | |
count = var.cluster_spotinst_max_size == 0 ? 0 : 1 | |
lifecycle { | |
ignore_changes = [ | |
desired_capacity | |
] | |
} | |
name = "${var.owner}-ecs-spotinst-elastigroup" | |
product = "Linux/UNIX" | |
min_size = var.cluster_spotinst_min_size | |
max_size = var.cluster_spotinst_max_size | |
ondemand_count = var.cluster_spotinst_ondemand_count == 0 ? null : var.cluster_spotinst_ondemand_count | |
spot_percentage = var.cluster_spotinst_spot_percentage | |
capacity_unit = "instance" | |
region = "eu-west-1" | |
subnet_ids = module.aws_vpc_data_module.aws_vpc_base_subnets_ids["private"] | |
image_id = data.aws_ami.amazon_linux_ecs.id | |
iam_instance_profile = aws_iam_instance_profile.ecs_cluster.arn | |
key_name = "aws_initialization_default" | |
security_groups = data.aws_security_groups.ecs_cluster_node.ids | |
user_data = data.template_file.user_data.rendered | |
enable_monitoring = false | |
ebs_optimized = true | |
placement_tenancy = "default" | |
instance_types_ondemand = var.cluster_spotinst_instance_types_ondemand | |
instance_types_spot = var.cluster_spotinst_instance_types_spot | |
instance_types_preferred_spot = var.cluster_spotinst_instance_types_preferred_spot | |
orientation = var.env == "prod" ? "balanced" : "costOriented" | |
fallback_to_ondemand = true | |
cpu_credits = "unlimited" | |
revert_to_spot { | |
perform_at = "always" | |
} | |
wait_for_capacity = var.cluster_spotinst_min_size | |
wait_for_capacity_timeout = 300 | |
scaling_strategy { | |
terminate_at_end_of_billing_hour = true | |
termination_policy = "default" | |
} | |
integration_ecs { | |
cluster_name = aws_ecs_cluster.cluster.name | |
autoscale_is_enabled = true | |
autoscale_is_auto_config = var.cluster_spotinst_autoscale_headroom_automatic | |
autoscale_cooldown = 300 | |
autoscale_scale_down_non_service_tasks = false | |
autoscale_headroom { | |
cpu_per_unit = var.cluster_spotinst_autoscale_headroom_cpu | |
memory_per_unit = var.cluster_spotinst_autoscale_headroom_memory | |
num_of_units = var.cluster_spotinst_autoscale_headroom_units | |
} | |
dynamic "autoscale_attributes" { | |
for_each = local.spotinst_autoscaler_attributes | |
content { | |
key = lookup(autoscale_attributes.value, "key", null) | |
value = lookup(autoscale_attributes.value, "value", null) | |
} | |
} | |
} | |
ebs_block_device { | |
device_name = "/dev/xvda" | |
volume_type = "gp2" | |
volume_size = var.cluster_root_volume_size | |
delete_on_termination = true | |
} | |
dynamic "tags" { | |
for_each = local.tags_spotinst | |
content { | |
key = lookup(tags.value, "key", null) | |
value = lookup(tags.value, "value", null) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment