Skip to content

Commit

Permalink
Merge pull request #333 from ComputeCanada/aws_spot
Browse files Browse the repository at this point in the history
Update AWS provider version
  • Loading branch information
cmd-ntrf authored Nov 17, 2024
2 parents 8dca8bd + b405c63 commit 11294d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 44 deletions.
55 changes: 13 additions & 42 deletions aws/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ data "aws_ec2_instance_type" "instance_type" {
}

resource "aws_instance" "instances" {
for_each = {for key, values in module.design.instances_to_build: key => values if !contains(values["tags"], "spot")}
for_each = module.design.instances_to_build
instance_type = each.value.type
ami = lookup(each.value, "image", var.image)
user_data = base64gzip(module.configuration.user_data[each.key])
Expand Down Expand Up @@ -113,49 +113,20 @@ resource "aws_instance" "instances" {
]
}

depends_on = [aws_internet_gateway.gw]
}

resource "aws_spot_instance_request" "spot_instances" {
for_each = {for key, values in module.design.instances_to_build: key => values if contains(values["tags"], "spot")}
instance_type = each.value.type
ami = lookup(each.value, "image", var.image)
user_data = base64gzip(module.configuration.user_data[each.key])
availability_zone = local.availability_zone
placement_group = contains(each.value.tags, "efa") ? aws_placement_group.efa_group.id : null

key_name = aws_key_pair.key.key_name

network_interface {
network_interface_id = aws_network_interface.nic[each.key].id
device_index = 0
}

ebs_optimized = true
root_block_device {
volume_type = lookup(each.value, "disk_type", "gp2")
volume_size = lookup(each.value, "disk_size", 20)
}

tags = {
Name = format("%s-%s", var.cluster_name, each.key)
}

lifecycle {
ignore_changes = [
ami
]
dynamic "instance_market_options" {
for_each = contains(each.value.tags, "spot") ? [each.value] : []
iterator = spot
content {
market_type = "spot"
spot_options {
spot_instance_type = lookup(spot, "spot_instance_type", "persistent")
instance_interruption_behavior = lookup(spot, "instance_interruption_behavior", "stop")
max_price = lookup(spot, "max_price", null)
}
}
}

depends_on = [aws_internet_gateway.gw]

# spot specific variables
wait_for_fulfillment = lookup(each.value, "wait_for_fulfillment", true)
spot_type = lookup(each.value, "spot_type", "persistent")
instance_interruption_behavior = lookup(each.value, "instance_interruption_behavior", "stop")
spot_price = lookup(each.value, "spot_price", null)
block_duration_minutes = lookup(each.value, "block_duration_minutes", null)

}

resource "aws_ebs_volume" "volumes" {
Expand Down Expand Up @@ -213,7 +184,7 @@ locals {
}

public_instances = { for host in keys(module.design.instances_to_build):
host => merge(module.configuration.inventory[host], {id=try(!contains(module.configuration.inventory[host].tags, "spot") ? aws_instance.instances[host].id : aws_spot_instance_request.spot_instances[host].spot_instance_id, "")})
host => merge(module.configuration.inventory[host], {id=try(aws_instance.instances[host].id, "")})
if contains(module.configuration.inventory[host].tags, "public")
}
}
2 changes: 1 addition & 1 deletion aws/network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ resource "aws_eip" "public_ip" {
for_each = {
for x, values in module.design.instances : x => true if contains(values.tags, "public")
}
vpc = true
domain = "vpc"
network_interface = aws_network_interface.nic[each.key].id
depends_on = [aws_internet_gateway.gw]
tags = {
Expand Down
2 changes: 1 addition & 1 deletion aws/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.10.0"
version = "~> 5.76.0"
}
}
}

0 comments on commit 11294d6

Please sign in to comment.