Skip to content

Commit

Permalink
fix private ip issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zahornyak committed Oct 16, 2023
1 parent f60aad2 commit fa4e5cd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 53 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module "ec2" {
| [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.lt](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
55 changes: 3 additions & 52 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module "ec2_instance" {

name = var.server_name

private_ip = var.private_ip

ami = var.ami != null ? var.ami : data.aws_ami.ami.id
instance_type = var.instance_type
monitoring = var.monitoring
Expand Down Expand Up @@ -99,49 +101,6 @@ resource "aws_eip" "this" {
}
}

resource "aws_launch_template" "lt" {
count = var.create_autoscaling_group && var.private_ip != null ? 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)

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 "block_device_mappings" {
for_each = var.root_block_device != null ? [var.root_block_device] : []
content {
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)
}
}
}

dynamic "network_interfaces" {
for_each = var.private_ip != null ? [var.private_ip] : []
content {
associate_public_ip_address = true
private_ip_address = var.private_ip
}
}

lifecycle {
create_before_destroy = true
}
}


resource "aws_launch_configuration" "as_conf" {
count = var.create_autoscaling_group && var.private_ip == null ? 1 : 0
Expand Down Expand Up @@ -177,15 +136,7 @@ resource "aws_autoscaling_group" "this" {
count = var.create_autoscaling_group ? 1 : 0

name = "${var.server_name}-asg"
launch_configuration = var.private_ip != null ? null : aws_launch_configuration.as_conf[0].id

dynamic "launch_template" {
for_each = var.private_ip != null ? [var.private_ip] : []
content {
id = aws_launch_template.lt[0].id
version = "$Latest"
}
}
launch_configuration = aws_launch_configuration.as_conf[0].id

min_size = var.min_size
max_size = var.max_size
Expand Down

0 comments on commit fa4e5cd

Please sign in to comment.