|
| 1 | + |
| 2 | +module "iam_instance_profile" { |
| 3 | + source = "scottwinkler/iip/aws" |
| 4 | + actions = ["logs:*", "ec2:DescribeInstances"] |
| 5 | +} |
| 6 | + |
| 7 | +data "aws_region" "current" {} |
| 8 | + |
| 9 | +locals { |
| 10 | + consul_config = var.consul.mode != "disabled" ? templatefile("${path.module}/templates/consul_${var.consul.mode}.json", { |
| 11 | + instance_count = var.instance_count, |
| 12 | + namespace = var.namespace, |
| 13 | + datacenter = var.datacenter, |
| 14 | + join_wan = join(",",[for s in var.join_wan: join("",["\"",s,"\""])]), |
| 15 | + }) : "" |
| 16 | + nomad_config = var.nomad.mode != "disabled" ? templatefile("${path.module}/templates/nomad_${var.nomad.mode}.hcl", { |
| 17 | + instance_count = var.instance_count |
| 18 | + datacenter = var.datacenter |
| 19 | + region = data.aws_region.current.name |
| 20 | + }) : "" |
| 21 | + startup = templatefile("${path.module}/templates/startup.sh", { |
| 22 | + consul_version = var.consul.version, |
| 23 | + consul_config = local.consul_config, |
| 24 | + consul_mode = var.consul.mode |
| 25 | + nomad_version = var.nomad.version, |
| 26 | + nomad_config = local.nomad_config, |
| 27 | + nomad_mode = var.nomad.mode, |
| 28 | + }) |
| 29 | + namespace = "${var.namespace}_N${var.nomad.mode}_C${var.consul.mode}" |
| 30 | +} |
| 31 | + |
| 32 | +data "aws_ami" "ubuntu" { |
| 33 | + most_recent = true |
| 34 | + filter { |
| 35 | + name = "name" |
| 36 | + values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"] |
| 37 | + } |
| 38 | + owners = ["099720109477"] |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_launch_template" "server" { |
| 42 | + name_prefix = local.namespace |
| 43 | + image_id = data.aws_ami.ubuntu.id |
| 44 | + instance_type = var.instance_type |
| 45 | + user_data = base64encode(local.startup) |
| 46 | + key_name = var.ssh_keypair |
| 47 | + iam_instance_profile { |
| 48 | + name = module.iam_instance_profile.name |
| 49 | + } |
| 50 | + network_interfaces { |
| 51 | + associate_public_ip_address = var.associate_public_ips |
| 52 | + security_groups = [var.security_group_id] |
| 53 | + delete_on_termination = true |
| 54 | + } |
| 55 | + |
| 56 | + tags = { |
| 57 | + ResourceGroup = var.namespace |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_autoscaling_group" "server" { |
| 62 | + name = local.namespace |
| 63 | + health_check_grace_period = 300 |
| 64 | + health_check_type = "ELB" |
| 65 | + target_group_arns = var.target_group_arns |
| 66 | + default_cooldown = 3600 |
| 67 | + min_size = var.instance_count |
| 68 | + max_size = var.instance_count |
| 69 | + vpc_zone_identifier = var.associate_public_ips ? var.vpc.public_subnets : var.vpc.private_subnets |
| 70 | + launch_template { |
| 71 | + id = aws_launch_template.server.id |
| 72 | + version = aws_launch_template.server.latest_version |
| 73 | + } |
| 74 | + tags = [ |
| 75 | + { |
| 76 | + key = "ResourceGroup" |
| 77 | + value = var.namespace |
| 78 | + propagate_at_launch = true |
| 79 | + }, |
| 80 | + { |
| 81 | + key = "Name" |
| 82 | + value = local.namespace |
| 83 | + propagate_at_launch = true |
| 84 | + } |
| 85 | + ] |
| 86 | +} |
| 87 | + |
| 88 | +data "aws_instances" "instances" { |
| 89 | + depends_on = [aws_autoscaling_group.server] |
| 90 | + count = var.associate_public_ips ? 1 : 0 |
| 91 | + instance_tags = { |
| 92 | + ResourceGroup = var.namespace |
| 93 | + Name = local.namespace |
| 94 | + } |
| 95 | + |
| 96 | + instance_state_names = ["running", "pending"] |
| 97 | +} |
0 commit comments