1
+ # Configure the AWS provider
1
2
provider "aws" {
2
3
region = " eu-west-1"
3
4
}
4
-
5
+
6
+ # Data source: query the list of availability zones
5
7
data "aws_availability_zones" "all" {}
6
8
9
+ # Data source: DB remote state
7
10
data "terraform_remote_state" "db" {
8
11
backend = " s3"
9
12
@@ -14,23 +17,25 @@ data "terraform_remote_state" "db" {
14
17
}
15
18
}
16
19
20
+ # Data source: Template file
17
21
data "template_file" "user_data" {
18
22
template = " ${ file (" user-data.sh" )} "
19
23
20
24
vars {
21
25
server_port = " ${ var . server_port } "
22
- db_address = " ${ data . terraform_remote_state . db . address } "
23
- db_port = " ${ data . terraform_remote_state . db . port } "
26
+ db_address = " ${ data . terraform_remote_state . db . address } "
27
+ db_port = " ${ data . terraform_remote_state . db . port } "
24
28
}
25
29
}
26
30
31
+ # Create a Security Group for an EC2 instance
27
32
resource "aws_security_group" "instance" {
28
33
name = " terraform-example-instance"
29
34
30
35
ingress {
31
- from_port = " ${ var . server_port } "
32
- to_port = " ${ var . server_port } "
33
- protocol = " tcp"
36
+ from_port = " ${ var . server_port } "
37
+ to_port = " ${ var . server_port } "
38
+ protocol = " tcp"
34
39
cidr_blocks = [" 0.0.0.0/0" ]
35
40
}
36
41
@@ -39,26 +44,28 @@ resource "aws_security_group" "instance" {
39
44
}
40
45
}
41
46
47
+ # Create a Security Group for an ELB
42
48
resource "aws_security_group" "elb" {
43
49
name = " terraform-example-elb"
44
50
45
51
ingress {
46
52
from_port = 80
47
- to_port = 80
53
+ to_port = 80
48
54
protocol = " tcp"
49
55
cidr_blocks = [" 0.0.0.0/0" ]
50
56
}
51
57
52
58
egress {
53
59
from_port = 0
54
- to_port = 0
60
+ to_port = 0
55
61
protocol = " -1"
56
62
cidr_blocks = [" 0.0.0.0/0" ]
57
63
}
58
64
}
59
65
66
+ # Create a Launch Configuration
60
67
resource "aws_launch_configuration" "example" {
61
- image_id = " ami-785db401"
68
+ image_id = " ami-785db401"
62
69
instance_type = " t2.micro"
63
70
security_groups = [" ${ aws_security_group . instance . id } " ]
64
71
user_data = " ${ data . template_file . user_data . rendered } "
@@ -68,6 +75,7 @@ resource "aws_launch_configuration" "example" {
68
75
}
69
76
}
70
77
78
+ # Create an Autoscaling Group
71
79
resource "aws_autoscaling_group" "example" {
72
80
launch_configuration = " ${ aws_launch_configuration . example . id } "
73
81
availability_zones = [" ${ data . aws_availability_zones . all . names } " ]
@@ -85,6 +93,7 @@ resource "aws_autoscaling_group" "example" {
85
93
}
86
94
}
87
95
96
+ # Create an ELB
88
97
resource "aws_elb" "example" {
89
98
name = " terraform-asg-example"
90
99
availability_zones = [" ${ data . aws_availability_zones . all . names } " ]
@@ -105,4 +114,3 @@ resource "aws_elb" "example" {
105
114
target = " HTTP:${ var . server_port } /"
106
115
}
107
116
}
108
-
0 commit comments