File tree Expand file tree Collapse file tree 2 files changed +15
-35
lines changed Expand file tree Collapse file tree 2 files changed +15
-35
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ variable "secret_key" {
10
10
description = " The AWS secret key used to provision resources"
11
11
}
12
12
13
+ variable "security_group_id" {
14
+ description = " The security group with ingress and egress rules that EC2 instances will be created within."
15
+ }
16
+
13
17
variable "region" {
14
18
description = " The AWS region in which to provision resources"
15
19
default = " us-west-2"
@@ -19,6 +23,10 @@ variable "identity" {
19
23
description = " A unique name for your resources"
20
24
}
21
25
26
+ variable "ami" {
27
+ description = " The Amazon Machine Image for new instances."
28
+ }
29
+
22
30
variable "num_webs" {
23
31
description = " The number of servers to run"
24
32
default = " 1"
@@ -34,8 +42,10 @@ provider "aws" {
34
42
module "server" {
35
43
source = " ./server"
36
44
37
- num_webs = " ${ var . num_webs } "
38
- identity = " ${ var . identity } "
45
+ num_webs = " ${ var . num_webs } "
46
+ identity = " ${ var . identity } "
47
+ security_group_id = " ${ var . security_group_id } "
48
+ ami = " ${ var . ami } "
39
49
}
40
50
41
51
output "public_ip" {
Original file line number Diff line number Diff line change @@ -12,46 +12,16 @@ variable "identity" {
12
12
description = " A unique name for this server"
13
13
}
14
14
15
- resource "aws_security_group" "web" {
16
- name = " ${ var . identity } -sg"
17
-
18
- ingress {
19
- from_port = 80
20
- to_port = 80
21
- protocol = " tcp"
22
- cidr_blocks = [" 0.0.0.0/0" ]
23
- }
24
-
25
- ingress {
26
- from_port = " 22"
27
- to_port = " 22"
28
- protocol = " TCP"
29
- cidr_blocks = [" 0.0.0.0/0" ]
30
- }
31
-
32
- egress {
33
- from_port = 0
34
- to_port = 0
35
- protocol = " -1"
36
- cidr_blocks = [" 0.0.0.0/0" ]
37
- }
38
-
39
- tags {
40
- " Identity" = " ${ var . identity } "
41
- " Created-by" = " Terraform"
42
- }
43
-
44
- lifecycle {
45
- create_before_destroy = true
46
- }
15
+ variable "security_group_id" {
16
+ description = " The AWS security group with ingress and egress rules for this instance."
47
17
}
48
18
49
19
resource "aws_instance" "web" {
50
20
ami = " ${ var . ami } "
51
21
instance_type = " t2.medium"
52
22
count = " ${ var . num_webs } "
53
23
54
- vpc_security_group_ids = [" ${ aws_security_group . web . id } " ]
24
+ vpc_security_group_ids = [" ${ var . security_group_id } " ]
55
25
56
26
tags {
57
27
" Name" = " ${ var . identity } web ${ count . index + 1 } /${ var . num_webs } "
You can’t perform that action at this time.
0 commit comments