-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathlaunch-instance.tf
84 lines (70 loc) · 2.26 KB
/
launch-instance.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
data "aws_ami" "latest-ecs" {
most_recent = true
owners = ["591542846629"] # Amazon
filter {
name = "name"
values = ["amzn2-ami-ecs-hvm-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_launch_configuration" "this" {
name = "ECS-Instance-${var.service_name}"
image_id = "${data.aws_ami.latest-ecs.id}"
instance_type = "t3.micro"
iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}"
root_block_device {
volume_type = "gp2"
volume_size = 100
delete_on_termination = true
}
lifecycle {
create_before_destroy = true
}
security_groups = ["${aws_security_group.ecs.id}"]
associate_public_ip_address = "true"
key_name = "${var.ecs_key_pair_name}"
user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${var.service_name}_cluster >> /etc/ecs/ecs.config
EOF
}
resource "aws_autoscaling_group" "this" {
name = "${var.service_name}-ecs-autoscaling-group"
max_size = 2
min_size = 1
desired_capacity = 1
vpc_zone_identifier = "${aws_subnet.private.*.id}"
launch_configuration = "${aws_launch_configuration.this.name}"
health_check_type = "ELB"
tag {
key = "Name"
value = "ECS-Instance-${var.service_name}-service"
propagate_at_launch = true
}
}
resource "aws_iam_role" "ecs-instance-role" {
name = "${var.service_name}-ecs-instance-role"
path = "/"
assume_role_policy = "${data.aws_iam_policy_document.ecs-instance-policy.json}"
}
data "aws_iam_policy_document" "ecs-instance-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "ecs-instance-role-attachment" {
role = "${aws_iam_role.ecs-instance-role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_instance_profile" "ecs-instance-profile" {
name = "${var.service_name}-ecs-instance-profile"
path = "/"
role = "${aws_iam_role.ecs-instance-role.id}"
}