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
18 changed files
with
748 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: 0.2 | ||
|
||
phases: | ||
pre_build: | ||
commands: | ||
- echo Build started on `date` | ||
- echo Logging in to Amazon ECR... | ||
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com | ||
- echo Building dalmatian-sql-backup docker image ... | ||
- docker build -t dalmatian-sql-backup:latest . | ||
build: | ||
commands: | ||
- echo Adding ECR repo tag... | ||
- docker tag dalmatian-sql-backup:latest $REPOSITORY_URI:latest | ||
post_build: | ||
commands: | ||
- echo Pushing the Docker image... | ||
- docker push $REPOSITORY_URI:latest |
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 @@ | ||
DATE_STRING=$$(date +%Y%m%d%H%M) && mkdir -p /tmp/sqlbackups/${rds_identifier} && mysql -N -u $$DB_USERNAME -p$$DB_PASSWORD -h $$DB_HOST -e 'show databases' | grep -Ev 'Databases|information_schema|performance_schema|sys' while read DB_NAME; do mysqldump -u $$DB_USER -p$$DB_PASSWORD -h $$DB_HOST --set-gtid-purged=OFF --column-statistics=0 --single-transaction \"$$DB_NAME\" > /tmp/sqlbackups/$$DATE_STRING-$$DB_NAME.sql; done && cd /tmp/sqlbackups && aws s3 sync . s3://${s3_bucket_name} --storage-class STANDARD_IA && rm /tmp/sqlbackups/*.sql && echo 'SQL Backup Success!' |
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 @@ | ||
DATE_STRING=$(date +%Y%m%d%H%M) && mkdir -p /tmp/sqlbackups/${rds_identifier} && PGPASSWORD=\"$$DB_PASSWORD\" psql -U $$DB_USERNAME -h $$DB_HOST -t -c 'SELECT datname FROM pg_database WHERE NOT datistemplate' | while read DB_NAME; do if [[ -n \"$$DB_NAME\" && \"$$DB_NAME\" != \"rdsadmin\" ]]; then pg_dump --clean --if-exists postgres://$$DB_USERNAME:$$DB_PASSWORD@$$DB_HOST:5432/$$DB_NAME > /tmp/sqlbackups/${rds_identifier}/$$DATE_STRING-$$DB_NAME.sql; fi; done && cd /tmp/sqlbackups/${rds_identifier} && aws s3 sync . s3://${s3_bucket_name} --storage-class STANDARD_IA && rm *.sql && echo 'SQL Backup Success!' |
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,70 @@ | ||
#!/bin/bash | ||
|
||
# exit on failures | ||
set -e | ||
set -o pipefail | ||
|
||
usage() { | ||
echo "Usage: $(basename "$0") [OPTIONS]" 1>&2 | ||
echo " -h - help" | ||
echo " -n - CodeBuild project name" | ||
exit 1 | ||
} | ||
|
||
while getopts "n:h" opt; do | ||
case $opt in | ||
n) | ||
PROJECT_NAME=$OPTARG | ||
;; | ||
h) | ||
usage | ||
;; | ||
*) | ||
usage | ||
;; | ||
esac | ||
done | ||
|
||
if [ -z "$PROJECT_NAME" ] | ||
then | ||
usage | ||
fi | ||
|
||
BUILD_ID="$( | ||
aws codebuild start-build \ | ||
--project-name "$PROJECT_NAME" \ | ||
| jq -r '.build.id' | ||
)" | ||
|
||
echo "Triggered $PROJECT_NAME CodeBuild project ($BUILD_ID) ..." | ||
|
||
COMPLETED="" | ||
while [ -z "$COMPLETED" ] | ||
do | ||
sleep 10 | ||
echo "Checking progress of CodeBuild $BUILD_ID ..." | ||
BUILD_PROGRESS="$( | ||
aws codebuild batch-get-builds \ | ||
--ids "$BUILD_ID" \ | ||
)" | ||
COMPLETED="$( | ||
echo "$BUILD_PROGRESS" \ | ||
| jq -r \ | ||
'.builds[0].phases[] | select(.phaseType == "COMPLETED")' | ||
)" | ||
done | ||
echo "CodeBuild $BUILD_ID Completed, checking for failures ..." | ||
|
||
FAILURES="$( | ||
echo "$BUILD_PROGRESS" \ | ||
| jq -r \ | ||
'.builds[0].phases[] | select(.phaseStatus == "FAILED")' | ||
)" | ||
|
||
if [ -n "$FAILURES" ] | ||
then | ||
echo "$FAILURES" | ||
exit 1 | ||
fi | ||
|
||
echo "CodeBuild $BUILD_ID completed without failures" |
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,16 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"codebuild:StartBuild", | ||
"codebuild:StopBuild", | ||
"codebuild:BatchGet*", | ||
"codebuild:Get*", | ||
"codebuild:List*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} |
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,110 @@ | ||
resource "aws_iam_role" "infrastructure_rds_s3_backups_image_codebuild" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("rds-s3-backups-image-codebuild"), 0, 6)}" | ||
description = "${local.resource_prefix}-rds-s3-backups-image-codebuild" | ||
assume_role_policy = templatefile( | ||
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl", | ||
{ services = jsonencode(["codebuild.amazonaws.com", "events.amazonaws.com"]) } | ||
) | ||
} | ||
|
||
resource "aws_iam_policy" "infrastructure_rds_s3_backups_image_codebuild_cloudwatch_rw" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("rds-s3-backups-image-codebuild-cloudwatch-rw"), 0, 6)}" | ||
description = "${local.resource_prefix}-rds-s3-backups-image-codebuild-cloudwatch-rw" | ||
policy = templatefile("${path.root}/policies/cloudwatch-logs-rw.json.tpl", {}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "infrastructure_rds_s3_backups_image_codebuild_cloudwatch_rw" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
role = aws_iam_role.infrastructure_rds_s3_backups_image_codebuild[0].name | ||
policy_arn = aws_iam_policy.infrastructure_rds_s3_backups_image_codebuild_cloudwatch_rw[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "infrastructure_rds_s3_backups_image_codebuild_allow_builds" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("rds-s3-backups-image-codebuild-allow-builds"), 0, 6)}" | ||
description = "${local.resource_prefix}-rds-s3-backups-image-codebuild-allow-builds" | ||
policy = templatefile("${path.root}/policies/codebuild-allow-builds.json.tpl", {}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "infrastructure_rds_s3_backups_image_codebuild_allow_builds" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
role = aws_iam_role.infrastructure_rds_s3_backups_image_codebuild[0].name | ||
policy_arn = aws_iam_policy.infrastructure_rds_s3_backups_image_codebuild_allow_builds[0].arn | ||
} | ||
|
||
resource "aws_codebuild_project" "infrastructure_rds_s3_backups_image_build" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-rds-s3-backups-image-build" | ||
description = "${local.resource_prefix} RDS S3 Backups Image Build" | ||
build_timeout = "20" | ||
service_role = aws_iam_role.infrastructure_rds_s3_backups_image_codebuild[0].arn | ||
|
||
artifacts { | ||
type = "NO_ARTIFACTS" | ||
} | ||
|
||
environment { | ||
compute_type = "BUILD_GENERAL1_SMALL" | ||
image = "aws/codebuild/standard:7.0" | ||
type = "LINUX_CONTAINER" | ||
privileged_mode = true | ||
|
||
environment_variable { | ||
name = "AWS_ACCOUNT_ID" | ||
value = local.aws_account_id | ||
} | ||
|
||
environment_variable { | ||
name = "REPOSITORY_URI" | ||
value = aws_ecr_repository.infrastructure_rds_s3_backups[0].repository_url | ||
} | ||
} | ||
|
||
source { | ||
type = "GITHUB" | ||
location = "https://github.com/dxw/dalmatian-sql-backup" | ||
git_clone_depth = 1 | ||
buildspec = templatefile("${path.root}/buildspecs/dalmatian-sql-backup.yml", {}) | ||
} | ||
} | ||
|
||
resource "terraform_data" "infrastructure_rds_s3_backups_image_build_trigger_codebuild" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
triggers_replace = [ | ||
md5(templatefile("${path.root}/buildspecs/dalmatian-sql-backup.yml", {})), | ||
] | ||
|
||
provisioner "local-exec" { | ||
interpreter = ["/bin/bash", "-c"] | ||
command = <<EOF | ||
${path.root}/local-exec-scripts/trigger-codedeploy-project.sh \ | ||
-n "${aws_codebuild_project.infrastructure_rds_s3_backups_image_build[0].name}" | ||
EOF | ||
} | ||
} | ||
|
||
resource "aws_cloudwatch_event_rule" "infrastructure_rds_s3_backups_image_build_trigger_codebuild" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix_hash}-rds-s3-backups-image-build-trigger-codebuild" | ||
description = "${local.resource_prefix} RDS S3 Backups Image Build Trigger CodeBuild" | ||
schedule_expression = local.infrastructure_rds_backup_to_s3_cron_expression | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "infrastructure_rds_s3_backups_image_build_trigger_codebuild" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
target_id = "${local.resource_prefix_hash}-rds-s3-backups-image-build-trigger-codebuild" | ||
rule = aws_cloudwatch_event_rule.infrastructure_rds_s3_backups_image_build_trigger_codebuild[0].name | ||
arn = aws_codebuild_project.infrastructure_rds_s3_backups_image_build[0].id | ||
role_arn = aws_iam_role.infrastructure_rds_s3_backups_image_codebuild[0].arn | ||
} |
Oops, something went wrong.