Skip to content

Commit 70cba1c

Browse files
committed
Variables for resources to satify security requirements
1 parent a0300e2 commit 70cba1c

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ variable "secret_key" {
1010
description = "The AWS secret key used to provision resources"
1111
}
1212

13+
variable "security_group_id" {
14+
description = "The security group with ingress and egress rules that EC2 instances will be created within."
15+
}
16+
1317
variable "region" {
1418
description = "The AWS region in which to provision resources"
1519
default = "us-west-2"
@@ -19,6 +23,10 @@ variable "identity" {
1923
description = "A unique name for your resources"
2024
}
2125

26+
variable "ami" {
27+
description = "The Amazon Machine Image for new instances."
28+
}
29+
2230
variable "num_webs" {
2331
description = "The number of servers to run"
2432
default = "1"
@@ -34,8 +42,10 @@ provider "aws" {
3442
module "server" {
3543
source = "./server"
3644

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}"
3949
}
4050

4151
output "public_ip" {

server/main.tf

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,16 @@ variable "identity" {
1212
description = "A unique name for this server"
1313
}
1414

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."
4717
}
4818

4919
resource "aws_instance" "web" {
5020
ami = "${var.ami}"
5121
instance_type = "t2.medium"
5222
count = "${var.num_webs}"
5323

54-
vpc_security_group_ids = ["${aws_security_group.web.id}"]
24+
vpc_security_group_ids = ["${var.security_group_id}"]
5525

5626
tags {
5727
"Name" = "${var.identity} web ${count.index+1}/${var.num_webs}"

0 commit comments

Comments
 (0)