-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_server.tf
112 lines (92 loc) · 2.88 KB
/
app_server.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
# ---------------------------------------------
# key pair
# ---------------------------------------------
resource "aws_key_pair" "keypair" {
key_name = "${var.project}-${var.environment}-keypair"
public_key = file("${var.key_pair_path}")
tags = {
Name = "${var.project}-${var.environment}-keypair"
Project = var.project
Env = var.environment
}
}
# ---------------------------------------------
# EC2 instance
# ---------------------------------------------
# resource "aws_instance" "app_server" {
# ami = data.aws_ami.app.id
# instance_type = "t2.micro"
# subnet_id = aws_subnet.public_subnet_1a.id
# associate_public_ip_address = true
# vpc_security_group_ids = [
# aws_security_group.app_sg.id,
# aws_security_group.opmng_sg.id
# ]
# iam_instance_profile = aws_iam_instance_profile.app_ec2_profile.name
# key_name = aws_key_pair.keypair.key_name
# tags = {
# Name = "${var.project}-${var.environment}-app-ec2"
# Project = var.project
# Env = var.environment
# Type = "app"
# }
# }
# ---------------------------------------------
# launch template
# ---------------------------------------------
resource "aws_launch_template" "app_lt" {
update_default_version = true
name = "${var.project}-${var.environment}-app-lt"
image_id = data.aws_ami.app.id
key_name = aws_key_pair.keypair.key_name
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.project}-${var.environment}-app-ec2"
Project = var.project
Env = var.environment
Type = "app"
}
}
network_interfaces {
associate_public_ip_address = true
security_groups = [
aws_security_group.app_sg.id,
aws_security_group.opmng_sg.id
]
delete_on_termination = true
}
iam_instance_profile {
name = aws_iam_instance_profile.app_ec2_profile.name
}
user_data = filebase64("./scripts/initialize.sh")
}
# ---------------------------------------------
# auto scaling group
# ---------------------------------------------
resource "aws_autoscaling_group" "app_asg" {
name = "${var.project}-${var.environment}-app-asg"
max_size = 2
min_size = 2
desired_capacity = 2
health_check_grace_period = 300
health_check_type = "ELB"
vpc_zone_identifier = [
aws_subnet.public_subnet_1a.id,
aws_subnet.public_subnet_1c.id
]
target_group_arns = [
aws_lb_target_group.alb_target_group.arn
]
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.app_lt.id
version = "$Latest"
}
override {
instance_type = var.ec2_instance_type
}
}
}
}