-
Notifications
You must be signed in to change notification settings - Fork 82
/
asg.tf
147 lines (129 loc) · 4.52 KB
/
asg.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
################################################################################
# Local variables
################################################################################
locals {
user_data = <<-EOT
#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
echo '<?php phpinfo(); ?>' > /var/www/html/phpinfo.php
sudo yum install php-mbstring php-xml -y
sudo systemctl restart httpd
sudo systemctl restart php-fpm
cd /var/www/html
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
mkdir phpMyAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C phpMyAdmin --strip-components 1
rm phpMyAdmin-latest-all-languages.tar.gz
echo '<?php phpinfo(); ?>' > /var/www/html/phpinfo.php
cd phpMyAdmin
mv config.sample.inc.php config.inc.php
sed -i 's/localhost/${module.rds.db_instance_address}/g' config.inc.php
EOT
}
################################################################################
# Supporting Resources
################################################################################
module "asg_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"
name = var.asg_sg_name
description = var.asg_sg_description
vpc_id = module.vpc.vpc_id
computed_ingress_with_source_security_group_id = [
{
rule = "http-80-tcp"
source_security_group_id = module.alb_http_sg.security_group_id
}
]
number_of_computed_ingress_with_source_security_group_id = 1
egress_rules = ["all-all"]
tags = var.asg_sg_tags
}
################################################################################
# Autoscaling scaling group (ASG)
################################################################################
module "asg" {
source = "terraform-aws-modules/autoscaling/aws"
# Autoscaling group
name = var.asg_name
min_size = var.asg_min_size
max_size = var.asg_max_size
desired_capacity = var.asg_desired_capacity
wait_for_capacity_timeout = var.asg_wait_for_capacity_timeout
health_check_type = var.asg_health_check_type
vpc_zone_identifier = module.vpc.private_subnets
target_group_arns = module.alb.target_group_arns
user_data = base64encode(local.user_data)
# Launch template
launch_template_name = var.asg_launch_template_name
launch_template_description = var.asg_launch_template_description
update_default_version = var.asg_update_default_version
image_id = var.asg_image_id
instance_type = var.asg_instance_type
ebs_optimized = var.asg_ebs_optimized
enable_monitoring = var.asg_enable_monitoring
# IAM role & instance profile
create_iam_instance_profile = var.asg_create_iam_instance_profile
iam_role_name = var.asg_iam_role_name
iam_role_path = var.asg_iam_role_path
iam_role_description = var.asg_iam_role_description
iam_role_tags = var.asg_iam_role_tags
iam_role_policies = {
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
block_device_mappings = [
{
# Root volume
device_name = "/dev/xvda"
no_device = 0
ebs = {
delete_on_termination = true
encrypted = true
volume_size = var.asg_block_device_mappings_volume_size_0
volume_type = "gp2"
}
}, {
device_name = "/dev/sda1"
no_device = 1
ebs = {
delete_on_termination = true
encrypted = true
volume_size = var.asg_block_device_mappings_volume_size_1
volume_type = "gp2"
}
}
]
network_interfaces = [
{
delete_on_termination = true
description = "eth0"
device_index = 0
security_groups = [module.asg_sg.security_group_id]
},
{
delete_on_termination = true
description = "eth1"
device_index = 1
security_groups = [module.asg_sg.security_group_id]
}
]
tag_specifications = [
{
resource_type = "instance"
tags = var.asg_instance_tags
},
{
resource_type = "volume"
tags = var.asg_volume_tags
}
]
tags = var.asg_tags
}