forked from voyagegroup/tf_aws_ecs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
97 lines (78 loc) · 2.5 KB
/
main.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
resource "aws_ecs_cluster" "main" {
name = var.name
}
resource "aws_cloudwatch_log_group" "ecs_agent" {
name = var.log_group
retention_in_days = var.log_groups_expiration_days
tags = var.log_groups_tags
}
resource "aws_autoscaling_group" "app" {
name = aws_ecs_cluster.main.name
enabled_metrics = var.asg_enabled_metrics
termination_policies = var.asg_termination_policies
launch_template {
id = aws_launch_template.app.id
version = "$Latest"
}
# NOTE: this module no handled desired capacity
#desired_capacity = "${var.asg_desired}"
min_size = var.asg_min_size
max_size = var.asg_max_size
vpc_zone_identifier = var.vpc_zone_identifier
default_cooldown = var.asg_default_cooldown
tags = [var.asg_extra_tags]
lifecycle {
create_before_destroy = true
# NOTE: changed automacally by autoscale policy
ignore_changes = [desired_capacity]
}
}
#Launch template for main cluster
data "template_file" "user_data" {
template = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${var.name} >> /etc/ecs/ecs.config
EOF
}
resource "aws_launch_template" "app" {
name_prefix = "${aws_ecs_cluster.main.name}-launch-template-"
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = var.root_volume_size
delete_on_termination = var.delete_on_termination
encrypted = var.encrypted
volume_type = var.ebs_volume_type
}
}
capacity_reservation_specification {
capacity_reservation_preference = "open"
}
iam_instance_profile {
name = aws_iam_instance_profile.ecs_instance.name
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = var.associate_public_ip_address
security_groups = var.security_groups
}
disable_api_termination = var.disable_api_termination
ebs_optimized = var.ebs_optimized
image_id = var.ami_id
instance_initiated_shutdown_behavior = "terminate"
update_default_version = true
instance_type = var.instance_type
key_name = var.key_name
user_data = "${base64encode(data.template_file.user_data.rendered)}"
tag_specifications {
resource_type = "instance"
tags = {
Name = "${aws_ecs_cluster.main.name}-launch-template"
}
}
lifecycle {
create_before_destroy = true
}
}