1
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2
+ # CREATE ALL THE RESOURCES TO DEPLOY AN APP IN AN AUTO SCALING GROUP WITH AN ELB
3
+ # This template runs a simple "Hello, World" web server in Auto Scaling Group (ASG) with an Elastic Load Balancer
4
+ # (ELB) in front of it to distribute traffic across the EC2 Instances in the ASG.
5
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
+
7
+ # ------------------------------------------------------------------------------
8
+ # CONFIGURE OUR AWS CONNECTION
9
+ # ------------------------------------------------------------------------------
10
+
11
+ provider "aws" {
12
+ region = " us-east-1"
13
+ }
14
+
15
+ # ---------------------------------------------------------------------------------------------------------------------
16
+ # GET THE LIST OF AVAILABILITY ZONES IN THE CURRENT REGION
17
+ # Every AWS accout has slightly different availability zones in each region. For example, one account might have
18
+ # us-east-1a, us-east-1b, and us-east-1c, while another will have us-east-1a, us-east-1b, and us-east-1d. This resource
19
+ # queries AWS to fetch the list for the current account and region.
20
+ # ---------------------------------------------------------------------------------------------------------------------
21
+
22
+ data "aws_availability_zones" "all" {}
23
+
24
+ # ---------------------------------------------------------------------------------------------------------------------
25
+ # CREATE THE AUTO SCALING GROUP
26
+ # ---------------------------------------------------------------------------------------------------------------------
27
+
28
+ resource "aws_autoscaling_group" "example" {
29
+ launch_configuration = " ${ aws_launch_configuration . example . id } "
30
+ availability_zones = [" ${ data . aws_availability_zones . all . names } " ]
31
+
32
+ min_size = 2
33
+ max_size = 10
34
+
35
+ load_balancers = [" ${ aws_elb . example . name } " ]
36
+ health_check_type = " ELB"
37
+
38
+ tag {
39
+ key = " Name"
40
+ value = " terraform-asg-example"
41
+ propagate_at_launch = true
42
+ }
43
+ }
44
+
45
+ # ---------------------------------------------------------------------------------------------------------------------
46
+ # CREATE A LAUNCH CONFIGURATION THAT DEFINES EACH EC2 INSTANCE IN THE ASG
47
+ # ---------------------------------------------------------------------------------------------------------------------
48
+
49
+ resource "aws_launch_configuration" "example" {
50
+ # Ubuntu Server 14.04 LTS (HVM), SSD Volume Type in us-east-1
51
+ image_id = " ami-2d39803a"
52
+ instance_type = " t2.micro"
53
+ security_groups = [" ${ aws_security_group . instance . id } " ]
54
+
55
+ user_data = <<- EOF
56
+ #!/bin/bash
57
+ echo "Hello, World" > index.html
58
+ nohup busybox httpd -f -p "${ var . server_port } " &
59
+ EOF
60
+
61
+ # Important note: whenever using a launch configuration with an auto scaling group, you must set
62
+ # create_before_destroy = true. However, as soon as you set create_before_destroy = true in one resource, you must
63
+ # also set it in every resource that it depends on, or you'll get an error about cyclic dependencies (especially when
64
+ # removing resources). For more info, see:
65
+ #
66
+ # https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
67
+ # https://terraform.io/docs/configuration/resources.html
68
+ lifecycle {
69
+ create_before_destroy = true
70
+ }
71
+ }
72
+
73
+ # ---------------------------------------------------------------------------------------------------------------------
74
+ # CREATE THE SECURITY GROUP THAT'S APPLIED TO EACH EC2 INSTANCE IN THE ASG
75
+ # ---------------------------------------------------------------------------------------------------------------------
76
+
77
+ resource "aws_security_group" "instance" {
78
+ name = " terraform-example-instance"
79
+
80
+ # Inbound HTTP from anywhere
81
+ ingress {
82
+ from_port = " ${ var . server_port } "
83
+ to_port = " ${ var . server_port } "
84
+ protocol = " tcp"
85
+ cidr_blocks = [" 0.0.0.0/0" ]
86
+ }
87
+
88
+ # aws_launch_configuration.launch_configuration in this module sets create_before_destroy to true, which means
89
+ # everything it depends on, including this resource, must set it as well, or you'll get cyclic dependency errors
90
+ # when you try to do a terraform destroy.
91
+ lifecycle {
92
+ create_before_destroy = true
93
+ }
94
+ }
95
+
96
+ # ---------------------------------------------------------------------------------------------------------------------
97
+ # CREATE AN ELB TO ROUTE TRAFFIC ACROSS THE AUTO SCALING GROUP
98
+ # ---------------------------------------------------------------------------------------------------------------------
99
+
100
+ resource "aws_elb" "example" {
101
+ name = " terraform-asg-example"
102
+ security_groups = [" ${ aws_security_group . elb . id } " ]
103
+ availability_zones = [" ${ data . aws_availability_zones . all . names } " ]
104
+
105
+ health_check {
106
+ healthy_threshold = 2
107
+ unhealthy_threshold = 2
108
+ timeout = 3
109
+ interval = 30
110
+ target = " HTTP:${ var . server_port } /"
111
+ }
112
+
113
+ # This adds a listener for incoming HTTP requests.
114
+ listener {
115
+ lb_port = 80
116
+ lb_protocol = " http"
117
+ instance_port = " ${ var . server_port } "
118
+ instance_protocol = " http"
119
+ }
120
+ }
121
+
122
+ # ---------------------------------------------------------------------------------------------------------------------
123
+ # CREATE A SECURITY GROUP THAT CONTROLS WHAT TRAFFIC AN GO IN AND OUT OF THE ELB
124
+ # ---------------------------------------------------------------------------------------------------------------------
125
+
126
+ resource "aws_security_group" "elb" {
127
+ name = " terraform-example-elb"
128
+
129
+ # Inbound HTTP from anywhere
130
+ ingress {
131
+ from_port = 80
132
+ to_port = 80
133
+ protocol = " tcp"
134
+ cidr_blocks = [" 0.0.0.0/0" ]
135
+ }
136
+ }
0 commit comments