Skip to content

Commit

Permalink
feat: replaced launch config with launch template
Browse files Browse the repository at this point in the history
  • Loading branch information
zahornyak committed Aug 19, 2024
1 parent 010da1e commit bc244eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
61 changes: 33 additions & 28 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,66 +84,71 @@ 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
}
}


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
Expand Down

0 comments on commit bc244eb

Please sign in to comment.