-
Notifications
You must be signed in to change notification settings - Fork 14
/
spotfleet.tf
131 lines (114 loc) · 3.42 KB
/
spotfleet.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
resource "aws_iam_instance_profile" "ecs" {
name = "${var.app_name}-ecs-instance"
roles = ["${aws_iam_role.ecs_instance.name}"]
}
resource "aws_iam_policy_attachment" "ecs_instance" {
name = "${var.app_name}-ecs-instance"
roles = ["${aws_iam_role.ecs_instance.name}"]
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_role" "ecs_instance" {
name = "${var.app_name}-ecs-instance"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_security_group" "ecs_instance" {
name = "${var.app_name}-ecs-instance"
description = "container security group for ${var.app_name}"
vpc_id = "${var.vpc}"
ingress {
from_port = 0
to_port = 65535
protocol = "TCP"
security_groups = ["${aws_security_group.ecs_alb.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_iam_policy_attachment" "fleet" {
name = "${var.app_name}-fleet"
roles = ["${aws_iam_role.fleet.name}"]
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
}
resource "aws_iam_role" "fleet" {
name = "${var.app_name}-fleet"
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"spotfleet.amazonaws.com",
"ec2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_spot_fleet_request" "main" {
iam_fleet_role = "${aws_iam_role.fleet.arn}"
spot_price = "${var.spot_prices[0]}"
allocation_strategy = "${var.strategy}"
target_capacity = "${var.instance_count}"
terminate_instances_with_expiration = true
valid_until = "${var.valid_until}"
launch_specification {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
spot_price = "${var.spot_prices[0]}"
subnet_id = "${var.subnets[0]}"
vpc_security_group_ids = ["${aws_security_group.ecs_instance.id}"]
iam_instance_profile = "${aws_iam_instance_profile.ecs.name}"
key_name = "${var.key_name}"
root_block_device = {
volume_type = "gp2"
volume_size = "${var.volume_size}"
}
user_data = <<USER_DATA
#!/bin/bash
echo ECS_CLUSTER=${aws_ecs_cluster.main.name} >> /etc/ecs/ecs.config
USER_DATA
}
launch_specification {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
spot_price = "${var.spot_prices[1]}"
subnet_id = "${var.subnets[1]}"
vpc_security_group_ids = ["${aws_security_group.ecs_instance.id}"]
iam_instance_profile = "${aws_iam_instance_profile.ecs.name}"
key_name = "${var.key_name}"
root_block_device = {
volume_type = "gp2"
volume_size = "${var.volume_size}"
}
user_data = <<USER_DATA
#!/bin/bash
echo ECS_CLUSTER=${aws_ecs_cluster.main.name} >> /etc/ecs/ecs.config
USER_DATA
}
depends_on = ["aws_iam_policy_attachment.fleet"]
}