generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from dxw/bastion-host
Conditionally launch Bastion host
- Loading branch information
Showing
7 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
resource "aws_security_group" "infrastructure_ec2_bastion_host" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-infrastructure-ec2-bastion-host" | ||
description = "Infrastructure EC2 Bastion Host" | ||
vpc_id = aws_vpc.infrastructure[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_egress_https_tcp" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
description = "Allow HTTPS tcp outbound" | ||
type = "egress" | ||
from_port = 443 | ||
to_port = 443 | ||
protocol = "tcp" | ||
# tfsec:ignore:aws-ec2-no-public-egress-sgr | ||
cidr_blocks = ["0.0.0.0/0"] | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_egress_https_udp" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
description = "Allow HTTPS udp outbound" | ||
type = "egress" | ||
from_port = 443 | ||
to_port = 443 | ||
protocol = "udp" | ||
# tfsec:ignore:aws-ec2-no-public-egress-sgr | ||
cidr_blocks = ["0.0.0.0/0"] | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_egress_dns_tcp" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
description = "Allow DNS tcp outbound to AWS" | ||
type = "egress" | ||
from_port = 53 | ||
to_port = 53 | ||
protocol = "tcp" | ||
cidr_blocks = local.infrastructure_ecs_cluster_publicly_avaialble ? [ | ||
for subnet in aws_subnet.infrastructure_public : subnet.cidr_block | ||
] : [ | ||
for subnet in aws_subnet.infrastructure_private : subnet.cidr_block | ||
] | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_egress_dns_udp" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
description = "Allow DNS udp outbound to AWS" | ||
type = "egress" | ||
from_port = 53 | ||
to_port = 53 | ||
protocol = "udp" | ||
cidr_blocks = local.infrastructure_ecs_cluster_publicly_avaialble ? [ | ||
for subnet in aws_subnet.infrastructure_public : subnet.cidr_block | ||
] : [ | ||
for subnet in aws_subnet.infrastructure_private : subnet.cidr_block | ||
] | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_egress_rds" { | ||
for_each = local.enable_infrastructure_bastion_host ? local.infrastructure_rds : {} | ||
|
||
description = "Allow ${each.value["engine"]} tcp outbound to RDS security group" | ||
type = "egress" | ||
from_port = local.rds_ports[each.value["engine"]] | ||
to_port = local.rds_ports[each.value["engine"]] | ||
protocol = "tcp" | ||
source_security_group_id = aws_security_group.infrastructure_rds[each.key].id | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_ec2_bastion_host_custom" { | ||
for_each = local.enable_infrastructure_bastion_host ? local.infrastructure_bastion_host_custom_security_group_rules : {} | ||
|
||
description = each.value["description"] | ||
type = each.value["type"] | ||
from_port = each.value["from_port"] | ||
to_port = each.value["to_port"] | ||
protocol = each.value["protocol"] | ||
source_security_group_id = each.value["source_security_group_id"] != "" ? each.value["source_security_group_id"] : null | ||
cidr_blocks = length(each.value["cidr_blocks"]) > 0 ? each.value["cidr_blocks"] : null | ||
security_group_id = aws_security_group.infrastructure_ec2_bastion_host[0].id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
resource "aws_instance" "infrastructure_bastion" { | ||
count = local.enable_infrastructure_bastion_host ? 1 : 0 | ||
|
||
ami = data.aws_ami.bastion_ami[0].id | ||
instance_type = "t3.micro" | ||
subnet_id = local.infrastructure_vpc_network_enable_private ? [for subnet in aws_subnet.infrastructure_private : subnet.id][0] : local.infrastructure_vpc_network_enable_public ? [for subnet in aws_subnet.infrastructure_public : subnet.id][0] : null | ||
user_data_base64 = base64encode(templatefile("${path.root}/ec2-userdata/bastion.tpl", {})) | ||
vpc_security_group_ids = [aws_security_group.infrastructure_ec2_bastion_host[0].id] | ||
|
||
root_block_device { | ||
encrypted = true | ||
} | ||
|
||
metadata_options { | ||
http_endpoint = "enabled" | ||
http_tokens = "required" | ||
} | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-bastion" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
# Install useful packages | ||
sudo yum update --security -y | ||
|
||
if ! command -v aws &> /dev/null | ||
then | ||
sudo yum install -y aws-cli | ||
fi | ||
|
||
sudo yum install -y \ | ||
jq \ | ||
rsync \ | ||
vim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters