Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added allowed_cidr_blocks variable to allow traffic from CIDR blocks #10

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Creates a RDS instance, security_group, subnet_group and parameter_group

### Available variables:
* [`vpc_id`]: String(required): ID of the VPC where to deploy in
* [`security_groups`]: List(required) Security groups that are allowed to access the RDS
* [`security_groups`]: List(optional) Security groups that are allowed to access the RDS
* [`allowed_cidr_blocks`]: List(optional) CIDR blocks that are allowed to access the RDS
* [`subnets`]: List(required) Subnets to deploy the RDS in
* [`storage`]: String(optional) How many GBs of space does your database need?
* [`size`]: String(optional) RDS instance size
Expand Down
26 changes: 19 additions & 7 deletions rds/security_group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ resource "aws_security_group" "sg_rds" {
description = "Security group that is needed for the RDS"
vpc_id = "${var.vpc_id}"

ingress {
from_port = "${lookup(var.ports, var.rds_type)}"
to_port = "${lookup(var.ports, var.rds_type)}"
protocol = "tcp"
security_groups = ["${var.security_groups}"]
}

tags {
Name = "${var.project}-${var.environment}${var.tag}-sg_rds"
Environment = "${var.environment}"
Project = "${var.project}"
}
}

resource "aws_security_group_rule" "rds_sg_in" {
count = "${length(var.security_groups)}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I overlooked this in my first review. Are you sure this works, did you test it? Tried to do the same a couple of TF versions ago and it was not possible in a count. That means you have to add an extra parameter to set the count fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, cool, than my positive review is still valid ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple cases work, but not yet everything. Here is some more info what is supported and what not:

hashicorp/terraform#11482 (comment)

security_group_id = "${aws_security_group.sg_rds.id}"
type = "ingress"
from_port = "${lookup(var.ports, var.rds_type)}"
to_port = "${lookup(var.ports, var.rds_type)}"
protocol = "tcp"
source_security_group_id = "${element(var.security_groups, count.index)}"
}

resource "aws_security_group_rule" "rds_cidr_in" {
security_group_id = "${aws_security_group.sg_rds.id}"
type = "ingress"
from_port = "${lookup(var.ports, var.rds_type)}"
to_port = "${lookup(var.ports, var.rds_type)}"
protocol = "tcp"
cidr_blocks = ["${var.allowed_cidr_blocks}"]
}
9 changes: 8 additions & 1 deletion rds/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ variable "vpc_id" {
}

variable "security_groups" {
description = "Security groups that are allowed to access the RDS on port 3306"
description = "Security groups that are allowed to access the RDS"
type = "list"
default = []
}

variable "allowed_cidr_blocks" {
description = "CIDR blocks that are allowed to access the RDS"
type = "list"
default = []
}

variable "subnets" {
Expand Down