Skip to content

Commit

Permalink
update the AWS terraform root module (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaMandal0rian authored Jan 23, 2025
1 parent 213378e commit b1822ab
Show file tree
Hide file tree
Showing 5 changed files with 882 additions and 673 deletions.
20 changes: 0 additions & 20 deletions templates/terraform/aws/ec2/ami.tf

This file was deleted.

228 changes: 214 additions & 14 deletions templates/terraform/aws/ec2/main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
data "aws_partition" "current" {}

locals {
create = var.create
create = var.create && var.putin_khuylo

is_t_instance_type = can(regex("t[0-9]+\\.[a-z0-9]+", var.instance_type))
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false

ami = try(coalesce(data.aws_ami.ubuntu_amd64.image_id, try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
ami = try(coalesce(var.ami, try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
}

data "aws_ssm_parameter" "this" {
Expand All @@ -14,15 +14,18 @@ data "aws_ssm_parameter" "this" {
name = var.ami_ssm_parameter
}

######################################
################################################################################
# Instance
######################################
################################################################################

resource "aws_instance" "this" {
count = local.create && var.ignore_ami_changes && !var.create_spot_instance ? 1 : 0
count = local.create && !var.ignore_ami_changes && !var.create_spot_instance ? 1 : 0

ami = local.ami
instance_type = var.instance_type
cpu_core_count = var.cpu_core_count
cpu_threads_per_core = var.cpu_threads_per_core
hibernation = var.hibernation

user_data = var.user_data
user_data_base64 = var.user_data_base64
Expand Down Expand Up @@ -187,24 +190,204 @@ resource "aws_instance" "this" {

tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
}

lifecycle {
################################################################################
# Instance - Ignore AMI Changes
################################################################################

resource "aws_instance" "ignore_ami" {
count = local.create && var.ignore_ami_changes && !var.create_spot_instance ? 1 : 0

ami = local.ami
instance_type = var.instance_type
cpu_core_count = var.cpu_core_count
cpu_threads_per_core = var.cpu_threads_per_core
hibernation = var.hibernation

ignore_changes = [ami, ipv6_address_count]
user_data = var.user_data
user_data_base64 = var.user_data_base64
user_data_replace_on_change = var.user_data_replace_on_change

availability_zone = var.availability_zone
subnet_id = var.subnet_id
vpc_security_group_ids = var.vpc_security_group_ids

key_name = var.key_name
monitoring = var.monitoring
get_password_data = var.get_password_data
iam_instance_profile = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].name : var.iam_instance_profile

associate_public_ip_address = var.associate_public_ip_address
private_ip = var.private_ip
secondary_private_ips = var.secondary_private_ips
ipv6_address_count = var.ipv6_address_count
ipv6_addresses = var.ipv6_addresses

ebs_optimized = var.ebs_optimized

dynamic "cpu_options" {
for_each = length(var.cpu_options) > 0 ? [var.cpu_options] : []

content {
core_count = try(cpu_options.value.core_count, null)
threads_per_core = try(cpu_options.value.threads_per_core, null)
amd_sev_snp = try(cpu_options.value.amd_sev_snp, null)
}
}
}

dynamic "capacity_reservation_specification" {
for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []

content {
capacity_reservation_preference = try(capacity_reservation_specification.value.capacity_reservation_preference, null)

dynamic "capacity_reservation_target" {
for_each = try([capacity_reservation_specification.value.capacity_reservation_target], [])

content {
capacity_reservation_id = try(capacity_reservation_target.value.capacity_reservation_id, null)
capacity_reservation_resource_group_arn = try(capacity_reservation_target.value.capacity_reservation_resource_group_arn, null)
}
}
}
}

dynamic "root_block_device" {
for_each = var.root_block_device

content {
delete_on_termination = try(root_block_device.value.delete_on_termination, null)
encrypted = try(root_block_device.value.encrypted, null)
iops = try(root_block_device.value.iops, null)
kms_key_id = lookup(root_block_device.value, "kms_key_id", null)
volume_size = try(root_block_device.value.volume_size, null)
volume_type = try(root_block_device.value.volume_type, null)
throughput = try(root_block_device.value.throughput, null)
tags = try(root_block_device.value.tags, null)
}
}

dynamic "ebs_block_device" {
for_each = var.ebs_block_device

content {
delete_on_termination = try(ebs_block_device.value.delete_on_termination, null)
device_name = ebs_block_device.value.device_name
encrypted = try(ebs_block_device.value.encrypted, null)
iops = try(ebs_block_device.value.iops, null)
kms_key_id = lookup(ebs_block_device.value, "kms_key_id", null)
snapshot_id = lookup(ebs_block_device.value, "snapshot_id", null)
volume_size = try(ebs_block_device.value.volume_size, null)
volume_type = try(ebs_block_device.value.volume_type, null)
throughput = try(ebs_block_device.value.throughput, null)
tags = try(ebs_block_device.value.tags, null)
}
}

dynamic "ephemeral_block_device" {
for_each = var.ephemeral_block_device

content {
device_name = ephemeral_block_device.value.device_name
no_device = try(ephemeral_block_device.value.no_device, null)
virtual_name = try(ephemeral_block_device.value.virtual_name, null)
}
}

dynamic "metadata_options" {
for_each = length(var.metadata_options) > 0 ? [var.metadata_options] : []

content {
http_endpoint = try(metadata_options.value.http_endpoint, "enabled")
http_tokens = try(metadata_options.value.http_tokens, "optional")
http_put_response_hop_limit = try(metadata_options.value.http_put_response_hop_limit, 1)
instance_metadata_tags = try(metadata_options.value.instance_metadata_tags, null)
}
}

dynamic "network_interface" {
for_each = var.network_interface

content {
device_index = network_interface.value.device_index
network_interface_id = lookup(network_interface.value, "network_interface_id", null)
delete_on_termination = try(network_interface.value.delete_on_termination, false)
}
}

dynamic "private_dns_name_options" {
for_each = length(var.private_dns_name_options) > 0 ? [var.private_dns_name_options] : []

content {
hostname_type = try(private_dns_name_options.value.hostname_type, null)
enable_resource_name_dns_a_record = try(private_dns_name_options.value.enable_resource_name_dns_a_record, null)
enable_resource_name_dns_aaaa_record = try(private_dns_name_options.value.enable_resource_name_dns_aaaa_record, null)
}
}

dynamic "launch_template" {
for_each = length(var.launch_template) > 0 ? [var.launch_template] : []

content {
id = lookup(var.launch_template, "id", null)
name = lookup(var.launch_template, "name", null)
version = lookup(var.launch_template, "version", null)
}
}

dynamic "maintenance_options" {
for_each = length(var.maintenance_options) > 0 ? [var.maintenance_options] : []

content {
auto_recovery = try(maintenance_options.value.auto_recovery, null)
}
}

######################################
enclave_options {
enabled = var.enclave_options_enabled
}

source_dest_check = length(var.network_interface) > 0 ? null : var.source_dest_check
disable_api_termination = var.disable_api_termination
disable_api_stop = var.disable_api_stop
instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
placement_group = var.placement_group
tenancy = var.tenancy
host_id = var.host_id

credit_specification {
cpu_credits = local.is_t_instance_type ? var.cpu_credits : null
}

timeouts {
create = try(var.timeouts.create, null)
update = try(var.timeouts.update, null)
delete = try(var.timeouts.delete, null)
}

tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null

lifecycle {
ignore_changes = [
ami
]
}
}

################################################################################
# Spot Instance
######################################
################################################################################

resource "aws_spot_instance_request" "this" {
count = local.create && var.create_spot_instance ? 1 : 0

ami = data.aws_ami.ubuntu_amd64.image_id
ami = local.ami
instance_type = var.instance_type
cpu_core_count = var.cpu_core_count
cpu_threads_per_core = var.cpu_threads_per_core
hibernation = var.hibernation

user_data = var.user_data
user_data_base64 = var.user_data_base64
Expand Down Expand Up @@ -361,9 +544,9 @@ resource "aws_spot_instance_request" "this" {
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
}

######################################
################################################################################
# IAM Role / Instance Profile
######################################
################################################################################

locals {
iam_role_name = try(coalesce(var.iam_role_name, var.name), "")
Expand Down Expand Up @@ -420,3 +603,20 @@ resource "aws_iam_instance_profile" "this" {
create_before_destroy = true
}
}

################################################################################
# Elastic IP
################################################################################

resource "aws_eip" "this" {
count = local.create && var.create_eip && !var.create_spot_instance ? 1 : 0

instance = try(
aws_instance.this[0].id,
aws_instance.ignore_ami[0].id,
)

domain = var.eip_domain

tags = merge(var.tags, var.eip_tags)
}
Loading

0 comments on commit b1822ab

Please sign in to comment.