1+ # Description: This file defines the EC2 instances for the web
2+ # and app tiers using Auto Scaling Groups for high availability.
3+ # -------------------------------------------------------------
4+
5+ # ## Data source ###
6+ data "aws_ami" "amazon_linux_2" {
7+ most_recent = true
8+ owners = [" amazon" ]
9+
10+ filter {
11+ name = " owner-alias"
12+ values = [" amazon" ]
13+ }
14+
15+ filter {
16+ name = " name"
17+ values = [" amzn2-ami-hvm*" ]
18+ }
19+ }
20+
21+ resource "aws_key_pair" "terraform_key" {
22+ key_name = " ec2-new-key"
23+ public_key = tls_private_key. rsa . public_key_openssh
24+ }
25+
26+ resource "tls_private_key" "rsa" {
27+ algorithm = " RSA"
28+ rsa_bits = 4096
29+ }
30+
31+ resource "aws_secretsmanager_secret" "ec2_private_key" {
32+ name = " ec2-private-key"
33+ description = " EC2 private key for SSH access"
34+ }
35+
36+ resource "aws_secretsmanager_secret_version" "ec2_private_key_version" {
37+ secret_id = aws_secretsmanager_secret. ec2_private_key . id
38+ secret_string = tls_private_key. rsa . private_key_pem
39+ }
40+
41+ # ## User Data Script for Web Server ###
42+ data "template_file" "web_user_data" {
43+ template = file (" ${ path . module } /user_data/web_user_data.sh" )
44+ }
45+
46+ # ## User Data Script for App Server ###
47+ data "template_file" "app_user_data" {
48+ template = file (" ${ path . module } /user_data/app_user_data.sh" )
49+ }
50+
51+ # ## Launch Template for Web Tier ###
52+ resource "aws_launch_template" "web_tier_template" {
53+ name_prefix = " web-tier-"
54+ image_id = data. aws_ami . amazon_linux_2 . id
55+ instance_type = " t2.micro"
56+ key_name = aws_key_pair. terraform_key . key_name
57+ user_data = base64encode (data. template_file . web_user_data . rendered )
58+
59+ block_device_mappings {
60+ device_name = " /dev/xvda"
61+ ebs {
62+ volume_size = 8
63+ encrypted = true
64+ }
65+ }
66+
67+ iam_instance_profile {
68+ name = aws_iam_instance_profile. web_profile . name
69+ }
70+
71+ vpc_security_group_ids = [aws_security_group . webserver_security_group . id ]
72+
73+ tag_specifications {
74+ resource_type = " instance"
75+ tags = {
76+ Name = " Web-Tier-Instance"
77+ }
78+ }
79+ }
80+
81+ # ## Launch Template for App Tier ###
82+ resource "aws_launch_template" "app_tier_template" {
83+ name_prefix = " app-tier-"
84+ image_id = data. aws_ami . amazon_linux_2 . id
85+ instance_type = " t2.micro"
86+ key_name = aws_key_pair. terraform_key . key_name
87+ user_data = base64encode (data. template_file . app_user_data . rendered )
88+
89+ block_device_mappings {
90+ device_name = " /dev/xvda"
91+ ebs {
92+ volume_size = 8
93+ encrypted = true
94+ }
95+ }
96+
97+ iam_instance_profile {
98+ name = aws_iam_instance_profile. app_profile . name
99+ }
100+
101+ vpc_security_group_ids = [aws_security_group . appserver_security_group . id ]
102+
103+ tag_specifications {
104+ resource_type = " instance"
105+ tags = {
106+ Name = " App-Tier-Instance"
107+ }
108+ }
109+ }
110+
111+ # ## Auto Scaling Group for Web Tier ###
112+ resource "aws_autoscaling_group" "web_asg" {
113+ name = " web-tier-asg"
114+ vpc_zone_identifier = [aws_subnet . public-web-subnet-1 . id , aws_subnet . public-web-subnet-2 . id ]
115+ desired_capacity = 2
116+ max_size = 2
117+ min_size = 2
118+ health_check_type = " ELB"
119+ health_check_grace_period = 300
120+ target_group_arns = [aws_lb_target_group . web_target_group . arn ]
121+
122+ launch_template {
123+ id = aws_launch_template. web_tier_template . id
124+ version = " $Latest"
125+ }
126+
127+ tag {
128+ key = " Name"
129+ value = " Web-Tier-ASG-Instance"
130+ propagate_at_launch = true
131+ }
132+ }
133+
134+ # ## Auto Scaling Group for App Tier ###
135+ resource "aws_autoscaling_group" "app_asg" {
136+ name = " app-tier-asg"
137+ vpc_zone_identifier = [aws_subnet . private-app-subnet-1 . id , aws_subnet . private-app-subnet-2 . id ]
138+ desired_capacity = 2
139+ max_size = 2
140+ min_size = 2
141+ health_check_type = " EC2"
142+ health_check_grace_period = 300
143+
144+ launch_template {
145+ id = aws_launch_template. app_tier_template . id
146+ version = " $Latest"
147+ }
148+
149+ tag {
150+ key = " Name"
151+ value = " App-Tier-ASG-Instance"
152+ propagate_at_launch = true
153+ }
154+ }
155+
156+ # Instance profiles to attach the IAM roles to the EC2 instances
157+ resource "aws_iam_instance_profile" "web_profile" {
158+ name = " web-tier-profile"
159+ role = aws_iam_role. web_tier_role . name
160+ }
161+
162+ resource "aws_iam_instance_profile" "app_profile" {
163+ name = " app-tier-profile"
164+ role = aws_iam_role. app_tier_role . name
165+ }
0 commit comments