|
| 1 | +resource "aws_security_group" "this" { |
| 2 | + name_prefix = var.name |
| 3 | + vpc_id = var.vpc_id |
| 4 | +} |
| 5 | + |
| 6 | +resource "aws_security_group_rule" "this_egress" { |
| 7 | + security_group_id = aws_security_group.this.id |
| 8 | + type = "egress" |
| 9 | + cidr_blocks = ["0.0.0.0/0"] |
| 10 | + from_port = 0 |
| 11 | + to_port = 65535 |
| 12 | + protocol = "tcp" |
| 13 | +} |
| 14 | + |
| 15 | +resource "aws_security_group_rule" "this_ingress" { |
| 16 | + security_group_id = aws_security_group.this.id |
| 17 | + type = "ingress" |
| 18 | + cidr_blocks = var.private_subnets_cidr_blocks |
| 19 | + from_port = 0 |
| 20 | + to_port = 65535 |
| 21 | + protocol = "tcp" |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_launch_template" "this" { |
| 25 | + name_prefix = var.name |
| 26 | + image_id = var.image_id |
| 27 | + iam_instance_profile { |
| 28 | + arn = aws_iam_instance_profile.this.arn |
| 29 | + } |
| 30 | + network_interfaces { |
| 31 | + associate_public_ip_address = true |
| 32 | + security_groups = [aws_security_group.this.id] |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_autoscaling_group" "this" { |
| 37 | + name_prefix = var.name |
| 38 | + desired_capacity = 1 |
| 39 | + min_size = 1 |
| 40 | + max_size = 1 |
| 41 | + vpc_zone_identifier = var.public_subnets |
| 42 | + |
| 43 | + mixed_instances_policy { |
| 44 | + instances_distribution { |
| 45 | + on_demand_percentage_above_base_capacity = 0 |
| 46 | + } |
| 47 | + launch_template { |
| 48 | + launch_template_specification { |
| 49 | + launch_template_id = aws_launch_template.this.id |
| 50 | + version = "$Latest" |
| 51 | + } |
| 52 | + dynamic "override" { |
| 53 | + for_each = var.instance_types |
| 54 | + content { |
| 55 | + instance_type = override.value |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + lifecycle { |
| 62 | + create_before_destroy = true |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +resource "aws_iam_instance_profile" "this" { |
| 67 | + name_prefix = var.name |
| 68 | + role = aws_iam_role.this.name |
| 69 | +} |
| 70 | + |
| 71 | +resource "aws_iam_role" "this" { |
| 72 | + name_prefix = var.name |
| 73 | + assume_role_policy = <<EOF |
| 74 | +{ |
| 75 | + "Version": "2012-10-17", |
| 76 | + "Statement": [ |
| 77 | + { |
| 78 | + "Effect": "Allow", |
| 79 | + "Principal": { |
| 80 | + "Service": "ec2.amazonaws.com" |
| 81 | + }, |
| 82 | + "Action": "sts:AssumeRole" |
| 83 | + } |
| 84 | + ] |
| 85 | +} |
| 86 | +EOF |
| 87 | +} |
| 88 | + |
| 89 | +resource "aws_iam_role_policy_attachment" "this_ssm" { |
| 90 | + policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" |
| 91 | + role = aws_iam_role.this.name |
| 92 | +} |
0 commit comments