-
Notifications
You must be signed in to change notification settings - Fork 13
/
ecs-cluster-ec2.tf
59 lines (47 loc) · 1.46 KB
/
ecs-cluster-ec2.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
data "aws_ami" "ecs_optimized_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-ecs-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_iam_instance_profile" "ecs-instance-profile_ec2" {
name = "${var.environment}-${var.service}-instance-profile"
path = "/"
role = "${aws_iam_role.ecs-service-ec2[count.index].id}"
provisioner "local-exec" {
command = "sleep 60"
}
count = "${var.launch_type == "FARGATE" ? 0 : 1}"
}
resource "aws_launch_configuration" "launch-configuration_ec2" {
name_prefix = "${var.environment}-${var.service}-launch-configuration-"
image_id = "${data.aws_ami.ecs_optimized_ami.id}"
instance_type = "${var.instance_type}"
iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile_ec2[count.index].id}"
key_name = "${var.key-pair-name}"
root_block_device {
volume_type = "${var.volume_type}"
volume_size = "${var.volume_size}"
delete_on_termination = true
}
lifecycle {
create_before_destroy = true
}
associate_public_ip_address = "false"
user_data = <<EOF
#!/bin/bash -xe
echo "ECS_CLUSTER=${local.ecs_cluster_name}" >> /etc/ecs/ecs.config
start ecs
EOF
count = "${var.launch_type == "FARGATE" ? 0 : 1}"
}