Skip to content

Commit 1a9f6be

Browse files
author
Kamlesh
committed
terraform 0.11.0
0 parents  commit 1a9f6be

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

example/example.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module "security_group" {
2+
source = "../"
3+
name = "security-group"
4+
application = "clouddrove"
5+
environment = "test"
6+
vpc_id = "vpc-3242342342432"
7+
cidr_blocks = ["10.0.0.0/16"]
8+
allowed_ports = [22, 80]
9+
}

main.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
locals {
2+
security_group_count = "${var.create_default_security_group == "true" ? 1 : 0}"
3+
}
4+
5+
module "labels" {
6+
source = "git::https://github.com/clouddrove/terraform-lables.git?ref=tags/0.11.0"
7+
application = "${var.application}"
8+
name = "${var.name}"
9+
environment = "${var.environment}"
10+
}
11+
12+
resource "aws_security_group" "default" {
13+
count = "${local.security_group_count}"
14+
name = "${module.labels.id}"
15+
vpc_id = "${var.vpc_id}"
16+
description = "Instance default security group (only egress access is allowed)"
17+
tags = "${module.labels.tags}"
18+
19+
lifecycle {
20+
create_before_destroy = true
21+
}
22+
}
23+
24+
resource "aws_security_group_rule" "egress" {
25+
count = "${var.create_default_security_group == "true" ? 1 : 0}"
26+
type = "egress"
27+
from_port = 0
28+
to_port = 65535
29+
protocol = "-1"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
security_group_id = "${aws_security_group.default.id}"
32+
}
33+
34+
resource "aws_security_group_rule" "ingress" {
35+
count = "${var.create_default_security_group == "true" ? length(compact(var.allowed_ports)) : 0}"
36+
type = "ingress"
37+
from_port = "${element(var.allowed_ports, count.index)}"
38+
to_port = "${element(var.allowed_ports, count.index)}"
39+
protocol = "tcp"
40+
cidr_blocks = ["${var.cidr_blocks}"]
41+
security_group_id = "${aws_security_group.default.id}"
42+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "security_group_ids" {
2+
description = "IDs on the AWS Security Groups associated with the instance"
3+
value = "${compact(concat(list(var.create_default_security_group == "true" ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}"
4+
}

variables.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
variable "create_default_security_group" {
2+
description = "Create default Security Group with only Egress traffic allowed"
3+
default = "true"
4+
}
5+
6+
variable "vpc_id" {
7+
description = "The ID of the VPC that the instance security group belongs to"
8+
default = ""
9+
}
10+
11+
variable "allowed_ports" {
12+
type = "list"
13+
description = "List of allowed ingress ports"
14+
default = []
15+
}
16+
17+
variable "application" {
18+
type = "string"
19+
description = "application (e.g. `cp` or `clouddrove`)"
20+
}
21+
22+
variable "environment" {
23+
type = "string"
24+
description = "Environment (e.g. `prod`, `dev`, `staging`)"
25+
}
26+
27+
variable "name" {
28+
description = "Name (e.g. `bastion` or `db`)"
29+
}
30+
31+
variable "delimiter" {
32+
default = "-"
33+
description = "Delimiter to be used between `name`, `namespace`, `stage`, etc."
34+
}
35+
36+
variable "attributes" {
37+
description = "Additional attributes (e.g. `1`)"
38+
type = "list"
39+
default = []
40+
}
41+
42+
variable "tags" {
43+
description = "Additional tags"
44+
type = "map"
45+
default = {}
46+
}
47+
48+
variable "cidr_blocks" {
49+
description = "List of CIDR blocks"
50+
type = "list"
51+
default = []
52+
}
53+
54+
variable "security_groups" {
55+
description = "List of Security Group IDs allowed to connect to the instance"
56+
type = "list"
57+
default = []
58+
}

0 commit comments

Comments
 (0)