Skip to content

Commit a10aa10

Browse files
committed
added aws auto scaling example
1 parent 0a28599 commit a10aa10

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Diff for: terraform-aws-autoscaling/main.tf

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
resource "aws_launch_configuration" "launch_config" {
6+
name = "web_config"
7+
image_id = lookup(var.ami_id, var.region)
8+
instance_type = "t2.micro"
9+
key_name = var.key_name
10+
security_groups = [ var.security_grpup_id]
11+
}
12+
13+
resource "aws_autoscaling_group" "example_autoscaling" {
14+
name = "autoscaling-terraform-test"
15+
max_size = 2
16+
min_size = 1
17+
health_check_grace_period = 300
18+
health_check_type = "EC2"
19+
desired_capacity = 1
20+
force_delete = true
21+
launch_configuration = aws_launch_configuration.launch_config.name
22+
availability_zones = ["us-east-1a","us-east-1b"]
23+
# vpc_zone_identifier = [aws_subnet.example1.id, aws_subnet.example2.id]
24+
25+
}
26+
27+
resource "aws_autoscaling_policy" "asp" {
28+
name = "asp-terraform-test"
29+
scaling_adjustment = 1
30+
adjustment_type = "ChangeInCapacity"
31+
cooldown = 300
32+
policy_type = "SimpleScaling"
33+
autoscaling_group_name = aws_autoscaling_group.example_autoscaling.name
34+
}
35+
36+
resource "aws_cloudwatch_metric_alarm" "aws_cloudwatch_metric_alarm" {
37+
alarm_name = "terraform-test-cloudwatch"
38+
comparison_operator = "GreaterThanOrEqualToThreshold"
39+
evaluation_periods = "2"
40+
metric_name = "CPUUtilization"
41+
namespace = "AWS/EC2"
42+
period = "120"
43+
statistic = "Average"
44+
threshold = "30"
45+
alarm_description = "This metric monitors ec2 cpu utilization"
46+
47+
dimensions = {
48+
AutoScalingGroupName = aws_autoscaling_group.example_autoscaling.name
49+
}
50+
51+
actions_enabled = true
52+
alarm_actions = [aws_autoscaling_policy.asp.arn]
53+
54+
}
55+
56+
resource "aws_sns_topic" "user_updates" {
57+
name = "user-updates-topic"
58+
display_name = "example auto scaling"
59+
}
60+
61+
resource "aws_autoscaling_notification" "example_notifications" {
62+
group_names = [aws_autoscaling_group.example_autoscaling.name]
63+
64+
notifications = [
65+
"autoscaling:EC2_INSTANCE_LAUNCH",
66+
"autoscaling:EC2_INSTANCE_TERMINATE",
67+
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
68+
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
69+
]
70+
71+
topic_arn = aws_sns_topic.user_updates.arn
72+
}

Diff for: terraform-aws-autoscaling/variables.tf

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
variable "region" {
2+
default = "us-east-1"
3+
}
4+
5+
variable "ami_id" {
6+
type = "map"
7+
default = {
8+
us-east-1 = "ami-04d29b6f966df1537"
9+
eu-west-2 = "ami-132b3c7efe6sdfdsfd"
10+
eu-central-1 = "ami-9787h5h6nsn75gd33"
11+
}
12+
}
13+
14+
variable "key_name" {
15+
type = "string"
16+
default = "ec2-demo"
17+
}
18+
19+
variable "instance_type" {
20+
type = "string"
21+
default = "t2.micro"
22+
}
23+
24+
variable "subnets" {
25+
type = list(string)
26+
default = ["subnet-59b98303","subnet-0d7cb232"]
27+
}
28+
29+
variable "azs" {
30+
type = list(string)
31+
default = ["us-east-1a","us-east-1b"]
32+
}
33+
34+
variable "security_grpup_id" {
35+
type = "string"
36+
default = "sg-53623a20"
37+
}

Diff for: terraform-aws-iam/iam_role_with_instance/variables.tf

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ variable "key_name" {
1616
default = "ec2-demo"
1717
}
1818

19+
1920
variable "instance_type" {
2021
type = "string"
2122
default = "t2.micro"

0 commit comments

Comments
 (0)