From bc244eb9cc0fa8ac7b4869bc5548f03c435f2525 Mon Sep 17 00:00:00 2001 From: Andriy Zahornyak Date: Mon, 19 Aug 2024 15:18:59 +0300 Subject: [PATCH] feat: replaced launch config with launch template --- README.md | 2 +- main.tf | 61 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index e2670d9..4e6ef15 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ module "ec2" { | [aws_eip.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | | [aws_iam_instance_profile.ec2_instance_profile](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource | | [aws_iam_role.instance_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | -| [aws_launch_configuration.as_conf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_configuration) | resource | +| [aws_launch_template.as_template](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource | | [aws_ami.ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source | | [template_file.user_data](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source | diff --git a/main.tf b/main.tf index c1d7bef..af935a9 100644 --- a/main.tf +++ b/main.tf @@ -84,19 +84,11 @@ EOF ], var.managed_policy_arns) } - -#resource "aws_iam_policy_attachment" "ecs_instance_policy_attachment_ssm" { -# name = "${var.server_name}_ecs_policy_attachemnt_ssm" -# policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" -# roles = [aws_iam_role.instance_role.name] -#} - resource "aws_eip" "this" { count = var.create_eip ? 1 : 0 instance = var.create_eip && var.create_autoscaling_group == false ? module.ec2_instance[0].id : null associate_with_private_ip = var.create_eip && var.create_autoscaling_group == false ? module.ec2_instance[0].private_ip : var.private_ip - vpc = true tags = { Server_name = var.server_name @@ -104,46 +96,59 @@ resource "aws_eip" "this" { } -resource "aws_launch_configuration" "as_conf" { - count = var.create_autoscaling_group && var.private_ip == null ? 1 : 0 +resource "aws_launch_template" "as_template" { + count = var.create_autoscaling_group ? 1 : 0 + + name_prefix = var.server_name + image_id = var.ami != null ? var.ami : data.aws_ami.ami.id + instance_type = var.instance_type + user_data = base64encode(data.template_file.user_data.rendered) - name_prefix = var.server_name - image_id = var.ami != null ? var.ami : data.aws_ami.ami.id - instance_type = var.instance_type - user_data = base64encode(data.template_file.user_data.rendered) - security_groups = var.security_group_ids - iam_instance_profile = var.instance_profile != null ? var.instance_profile : aws_iam_instance_profile.ec2_instance_profile[0].name + iam_instance_profile { + name = var.instance_profile != null ? var.instance_profile : aws_iam_instance_profile.ec2_instance_profile[0].name + } + vpc_security_group_ids = var.security_group_ids - dynamic "root_block_device" { - for_each = var.root_block_device != null ? [1] : [0] + dynamic "block_device_mappings" { + 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) - throughput = try(root_block_device.value.throughput, null) - volume_size = try(root_block_device.value.volume_size, null) - volume_type = try(root_block_device.value.volume_type, null) + device_name = "/dev/xvda" + + ebs { + delete_on_termination = try(block_device_mappings.value.delete_on_termination, null) + encrypted = try(block_device_mappings.value.encrypted, null) + iops = try(block_device_mappings.value.iops, null) + throughput = try(block_device_mappings.value.throughput, null) + volume_size = try(block_device_mappings.value.volume_size, null) + volume_type = try(block_device_mappings.value.volume_type, null) + } } } + network_interfaces { + private_ip_address = var.private_ip + } + lifecycle { create_before_destroy = true } } - - resource "aws_autoscaling_group" "this" { count = var.create_autoscaling_group ? 1 : 0 - name = "${var.server_name}-asg" - launch_configuration = aws_launch_configuration.as_conf[0].id + name = "${var.server_name}-asg" + launch_template { + id = aws_launch_template.as_template[0].id + version = "$Latest" + } min_size = var.min_size max_size = var.max_size vpc_zone_identifier = [var.subnet_id] + tag { key = "Name" propagate_at_launch = true