· 7 years ago · Jan 08, 2019, 02:40 PM
1provider "aws" {
2 region = "us-east-1"
3 access_key = "AKIAJHWMUNVJPHZK6NQQ"
4 secret_key = "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"
5}
6resource "aws_launch_configuration" "test_launch" {
7 name = "UbuntuScale"
8 image_id = "ami-0ac019f4fcb7cb7e6"
9 instance_type = "t2.micro"
10 security_groups = ["web"]
11 lifecycle {
12 create_before_destroy = true
13 }
14 user_data = <<-EOF
15 #!/bin/bash
16 apt-get update
17 apt-get install nginx -y
18 echo "This is Hello World from Server $(shuf -i1-10 -n1)" > /var/www/html/index.html
19 service nginx start
20 EOF
21 root_block_device = {
22 volume_type = "gp2"
23 volume_size = 20
24 delete_on_termination = true
25 }
26 key_name = "n.virginia"
27}
28resource "aws_autoscaling_group" "asg" {
29 name = "MyWebServers"
30 max_size = 5
31 min_size = 1
32 health_check_grace_period = 300
33 availability_zones = ["us-east-1a"]
34 health_check_type = "EC2"
35 desired_capacity = 2
36 force_delete = true
37 launch_configuration = "${aws_launch_configuration.test_launch.name}"
38 lifecycle {
39 create_before_destroy = true
40 }
41 tag {
42 key = "Name"
43 value = "Hello-World"
44 propagate_at_launch = "true"
45 }
46
47}
48resource "aws_autoscaling_policy" "asp_up" {
49 name = "MyEC2Scaling_up"
50 adjustment_type = "ChangeInCapacity"
51 autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
52 policy_type = "SimpleScaling"
53 scaling_adjustment = "+2"
54 cooldown = 300
55}
56resource "aws_autoscaling_policy" "asp_down" {
57 name = "MyEC2Scaling_down"
58 adjustment_type = "ChangeInCapacity"
59 autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
60 policy_type = "SimpleScaling"
61 scaling_adjustment = "-1"
62 cooldown = 300
63}
64resource "aws_cloudwatch_metric_alarm" "cma_up" {
65 alarm_name = "Alarm for MyEC2Scaling_up"
66 comparison_operator = "GreaterThanOrEqualToThreshold"
67 evaluation_periods = "1"
68 metric_name = "CPUUtilization"
69 namespace = "AWS/EC2"
70 period = "60"
71 statistic = "Average"
72 threshold = "80"
73
74 dimensions {
75 AutoScalingGroupName = "${aws_autoscaling_group.asg.name}"
76 }
77 alarm_actions = ["${aws_autoscaling_policy.asp_up.arn}"]
78}
79resource "aws_cloudwatch_metric_alarm" "cma_down" {
80 alarm_name = "Alarm for MyEC2Scaling_down"
81 comparison_operator = "LessThanOrEqualToThreshold"
82 evaluation_periods = "1"
83 metric_name = "CPUUtilization"
84 namespace = "AWS/EC2"
85 period = "60"
86 statistic = "Average"
87 threshold = "60"
88
89 dimensions {
90 AutoScalingGroupName = "${aws_autoscaling_group.asg.name}"
91 }
92 alarm_actions = ["${aws_autoscaling_policy.asp_down.arn}"]
93}
94resource "aws_elb" "myelb" {
95 name = "MyELB"
96 availability_zones = ["us-east-1a"]
97 listener {
98 instance_port = 80
99 instance_protocol = "http"
100 lb_port = 80
101 lb_protocol = "http"
102 }
103
104 health_check {
105 healthy_threshold = 2
106 unhealthy_threshold = 2
107 timeout = 3
108 target = "HTTP:80/index.html"
109 interval = 30
110 }
111
112 cross_zone_load_balancing = true
113 idle_timeout = 300
114 connection_draining = true
115 connection_draining_timeout = 300
116
117 tags {
118 Name = "MyELB"
119 }
120}
121resource "aws_autoscaling_attachment" "asg_attachment" {
122 autoscaling_group_name = "${aws_autoscaling_group.asg.id}"
123 elb = "${aws_elb.myelb.id}"
124}
125resource "aws_route53_zone" "main" {
126 name = "test-aws.ml"
127 }
128
129data "aws_elb_hosted_zone_id" "main" {}
130resource "aws_route53_record" "elb_alias" {
131 zone_id = "${aws_route53_zone.main.zone_id}"
132 name = "test-aws.ml"
133 type = "A"
134 alias {
135 name = "${aws_elb.myelb.dns_name}"
136 zone_id = "${data.aws_elb_hosted_zone_id.main.id}"
137 evaluate_target_health = true
138 }
139}