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.
* Conditonally create a scheduled Fargate task to backup RDS databases to S3
- Loading branch information
Showing
12 changed files
with
516 additions
and
2 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,12 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"secretsmanager:GetSecretValue" | ||
], | ||
"Resource": ${secret_name_arns} | ||
} | ||
] | ||
} |
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,17 @@ | ||
resource "aws_ecr_repository" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-rds-s3-backups" | ||
|
||
#tfsec:ignore:aws-ecr-enforce-immutable-repository | ||
image_tag_mutability = "MUTABLE" | ||
|
||
encryption_configuration { | ||
encryption_type = local.infrastructure_kms_encryption ? "KMS" : "AES256" | ||
kms_key = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
} | ||
|
||
image_scanning_configuration { | ||
scan_on_push = true | ||
} | ||
} |
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,87 @@ | ||
resource "aws_s3_bucket" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = "${local.resource_prefix_hash}-infrastructure-rds-s3-backups" | ||
} | ||
|
||
resource "aws_s3_bucket_policy" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
policy = templatefile( | ||
"${path.module}/policies/s3-bucket-policy.json.tpl", | ||
{ | ||
statement = <<EOT | ||
[ | ||
${templatefile("${path.root}/policies/s3-bucket-policy-statements/enforce-tls.json.tpl", | ||
{ | ||
bucket_arn = aws_s3_bucket.infrastructure_rds_s3_backups[0].arn | ||
} | ||
)} | ||
] | ||
EOT | ||
} | ||
) | ||
} | ||
|
||
resource "aws_s3_bucket_public_access_block" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
block_public_acls = true | ||
block_public_policy = true | ||
ignore_public_acls = true | ||
restrict_public_buckets = true | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
|
||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_logging" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
|
||
target_bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
target_prefix = "s3/infrastructure-rds-s3-backups" | ||
} | ||
|
||
resource "aws_s3_bucket_server_side_encryption_configuration" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
|
||
rule { | ||
apply_server_side_encryption_by_default { | ||
kms_master_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
sse_algorithm = local.infrastructure_kms_encryption ? "aws:kms" : "AES256" | ||
} | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_lifecycle_configuration" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_logs_bucket && local.infrastructure_rds_backup_to_s3_retention != 0 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_rds_s3_backups[0].id | ||
|
||
rule { | ||
id = "all_expire" | ||
|
||
filter { | ||
prefix = "" | ||
} | ||
|
||
expiration { | ||
days = local.infrastructure_rds_backup_to_s3_retention | ||
} | ||
|
||
status = "Enabled" | ||
} | ||
} |
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,77 @@ | ||
resource "aws_security_group" "infrastructure_rds_s3_backups_scheduled_task" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? local.infrastructure_rds : {} | ||
|
||
name = "${local.resource_prefix}-infrastructure-rds-s3-backups-scheduled-task" | ||
description = "Infrastructure RDS S3 backups scheduled task" | ||
vpc_id = aws_vpc.infrastructure[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_rds_s3_backups_scheduled_task_egress_https_tcp" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? local.infrastructure_rds : {} | ||
|
||
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_rds_s3_backups_scheduled_task[each.key].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_rds_s3_backups_scheduled_task_egress_https_udp" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? local.infrastructure_rds : {} | ||
|
||
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_rds_s3_backups_scheduled_task[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_rds_s3_backups_scheduled_task_egress_dns_tcp" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? local.infrastructure_rds : {} | ||
|
||
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_rds_s3_backups_scheduled_task[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_rds_s3_backups_scheduled_task_egress_dns_udp" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? local.infrastructure_rds : {} | ||
|
||
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_rds_s3_backups_scheduled_task[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_rds_s3_backups_scheduled_task_egress_rds" { | ||
for_each = local.enable_infrastructure_rds_backup_to_s3 ? 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_rds_s3_backups_scheduled_task[0].id | ||
} |
Oops, something went wrong.