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
+ }
0 commit comments