-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling.tf
159 lines (135 loc) · 3.75 KB
/
autoscaling.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
148
149
150
151
152
153
154
155
156
157
158
159
provider "aws" {
region = "us-west-2"
}
# Create VPC
resource "aws_vpc" "main_vpc" {
cidr_block = "10.0.0.0/16"
}
# Create Subnet
resource "aws_subnet" "main_subnet" {
vpc_id = aws_vpc.main_vpc.id
cidr_block = "10.0.1.0/24"
}
# Security Group
resource "aws_security_group" "allow_ssh" {
vpc_id = aws_vpc.main_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# IAM Role for EC2 Instances
resource "aws_iam_role" "ec2_role" {
name = "ec2_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "attach_policy" {
role = aws_iam_role.ec2_role.name
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
resource "aws_iam_instance_profile" "ec2_instance_profile" {
name = "ec2_instance_profile"
role = aws_iam_role.ec2_role.name
}
# Launch Template
resource "aws_launch_template" "ec2_template" {
name_prefix = "example-template"
image_id = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
key_name = "your-key-name" # replace with your key pair
iam_instance_profile {
name = aws_iam_instance_profile.ec2_instance_profile.name
}
network_interfaces {
security_groups = [aws_security_group.allow_ssh.id]
associate_public_ip_address = true
subnet_id = aws_subnet.main_subnet.id
}
user_data = <<-EOF
#!/bin/bash
yum install -y aws-cli
EOF
}
# Auto Scaling Group
resource "aws_autoscaling_group" "asg" {
desired_capacity = 1
max_size = 3
min_size = 1
vpc_zone_identifier = [aws_subnet.main_subnet.id]
launch_template {
id = aws_launch_template.ec2_template.id
version = "$Latest"
}
tag {
key = "Name"
value = "autoscaled-vm"
propagate_at_launch = true
}
}
# CloudWatch CPU Utilization Alarm - Scale Up
resource "aws_cloudwatch_metric_alarm" "scale_up" {
alarm_name = "scale_up_on_high_cpu"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 80
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
alarm_actions = [aws_autoscaling_policy.scale_up_policy.arn]
}
# CloudWatch CPU Utilization Alarm - Scale Down
resource "aws_cloudwatch_metric_alarm" "scale_down" {
alarm_name = "scale_down_on_low_cpu"
comparison_operator = "LessThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 30
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
alarm_actions = [aws_autoscaling_policy.scale_down_policy.arn]
}
# Scaling Policies
resource "aws_autoscaling_policy" "scale_up_policy" {
name = "scale_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.asg.name
}
resource "aws_autoscaling_policy" "scale_down_policy" {
name = "scale_down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.asg.name
}