Last active
March 2, 2020 16:51
-
-
Save wreulicke/4c1abb1405839b14bc7679bfe955bd1e to your computer and use it in GitHub Desktop.
いい感じでauto_scaling_groupでec2を立ち上げつつ、ssm agentで接続できるようにするやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
locals { | |
key_name = "saito-load-test" | |
user_data = "" | |
} | |
data "aws_availability_zones" "available" { | |
state = "available" | |
} | |
resource "aws_default_subnet" "default_subnet" { | |
for_each = toset(data.aws_availability_zones.available.names) | |
availability_zone = each.value | |
} | |
resource "aws_iam_role" "ec2_instance_role" { | |
name = "Ec2InstanceRole" | |
assume_role_policy = file("ec2-assume-role.json") | |
} | |
resource "aws_iam_instance_profile" "default" { | |
name = "ecs-instance-profile" | |
path = "/" | |
role = aws_iam_role.ec2_instance_role.name | |
} | |
resource "aws_iam_policy_attachment" "ecs_instance_role_attach" { | |
name = "ec2-instance-role-attach" | |
roles = [aws_iam_role.ec2_instance_role.name] | |
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" | |
} | |
data "aws_ssm_parameter" "amazon_linux_2_ami_id" { | |
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" | |
} | |
resource "aws_launch_template" "launch_template" { | |
name = "load-test" | |
image_id = data.aws_ssm_parameter.amazon_linux_2_ami_id.value | |
instance_type = "c5.large" | |
key_name = local.key_name | |
iam_instance_profile { | |
name = aws_iam_instance_profile.default.name // TODO | |
} | |
user_data = base64encode(local.user_data) | |
block_device_mappings { | |
device_name = "/dev/xvda" # root device name of amazon linux2 | |
ebs { | |
volume_size = "8" | |
volume_type = "gp2" | |
delete_on_termination = true | |
} | |
} | |
} | |
resource "aws_placement_group" "load_test" { | |
name = "load-test" | |
strategy = "cluster" | |
} | |
resource "aws_autoscaling_group" "load-test-group" { | |
name = "load-test-group" | |
min_size = 0 | |
max_size = 1 | |
desired_capacity = 1 | |
placement_group = aws_placement_group.load_test.id | |
vpc_zone_identifier = [for x in aws_default_subnet.default_subnet: x.id] | |
mixed_instances_policy { | |
launch_template { | |
launch_template_specification { | |
launch_template_id = aws_launch_template.launch_template.id | |
version = aws_launch_template.launch_template.latest_version | |
} | |
} | |
instances_distribution { | |
on_demand_percentage_above_base_capacity = 0 | |
spot_allocation_strategy = "lowest-price" | |
spot_instance_pools = "2" | |
} | |
} | |
tag { | |
key = "Name" | |
value = "load-test" | |
propagate_at_launch = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment