generated from NHSDigital/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
added event bridge lambda and new vault to facilitate new cross account copy job method #42
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
Closed
aidanharrisnhs
wants to merge
7
commits into
main
from
feature/TEX-9678-event-bridge-lambda-intermediary-vault-implementation
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77d5cf0
added event bridge lambda and new vault to facilitate new cross accou…
aidanharrisnhs 3da6460
removed texas tags
aidanharrisnhs d7109ce
added new backup plans for rds and a variable for immutable backup re…
aidanharrisnhs f9e5927
made the intermediary vault and lambda based off of if rds in enabled
aidanharrisnhs 4ca611b
Merge branch 'main' into feature/TEX-9678-event-bridge-lambda-interme…
aidanharrisnhs e17781e
Merge branch 'main' into feature/TEX-9678-event-bridge-lambda-interme…
aidanharrisnhs b99f041
Update lambda_copy_job.tf to allow lambda to write cloudwatch logs
aidanharrisnhs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| module "eventbridge" { | ||
| source = "terraform-aws-modules/eventbridge/aws" | ||
| version = "3.14.3" | ||
|
|
||
| create_bus = false | ||
| create_role = false | ||
|
|
||
| rules = { | ||
| "Cross-Account-Copy-Job" = { | ||
| description = "Identify when a new recovery point is created in the intermediary vault" | ||
| event_pattern = jsonencode( | ||
| { | ||
| "source" : ["aws.backup"], | ||
| "account" : ["${data.aws_caller_identity.current.account_id}"], | ||
| "region" : ["eu-west-2"], | ||
| "detail" : { | ||
| "eventName" : ["RecoveryPointCreated"], | ||
| "serviceEventDetails" : { | ||
| "backupVaultName" : [{ "wildcard" : "*-intermediary-vault" }] | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| enabled = true | ||
| } | ||
| } | ||
|
|
||
| targets = { | ||
| "Cross-Account-Copy-Job" = [ | ||
| { | ||
| name = "start_cross_account_copy_job" | ||
| arn = "arn:aws:lambda:eu-west-2:${data.aws_caller_identity.current.account_id}:function:start_cross_account_copy_job" | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,86 @@ | ||
| data "aws_iam_policy_document" "lambda_assume_role" { | ||
| statement { | ||
| effect = "Allow" | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["lambda.amazonaws.com"] | ||
| } | ||
| actions = ["sts:AssumeRole"] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_role" "iam_for_lambda_copy_job" { | ||
| count = var.backup_plan_config_rds.enable ? 1 : 0 | ||
| name = "iam_for_cross_account_copy_job_lambda" | ||
| assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "lambda_copy_job_permissions" { | ||
| version = "2012-10-17" | ||
| statement { | ||
| effect = "Allow" | ||
| actions = ["kms:Decrypt", "kms:GenerateDataKey"] | ||
| resources = [aws_kms_key.aws_backup_key.arn] | ||
| } | ||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "backup:StartCopyJob", | ||
| "backup:DescribeRecoveryPoint", | ||
| "backup:ListRecoveryPointsByBackupVault" | ||
| ] | ||
| resources = ["*"] | ||
| } | ||
| statement { | ||
| effect = "Allow" | ||
| actions = ["iam:PassRole"] | ||
| resources = [aws_iam_role.backup.arn] | ||
| } | ||
| } | ||
|
|
||
pennytextures marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resource "aws_iam_role_policy_attachment" "lambda_role_policy_attachment" { | ||
| count = var.backup_plan_config_rds.enable ? 1 : 0 | ||
| role = aws_iam_role.iam_for_lambda_copy_job[0].name | ||
| policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy" "cross_account_iam_permissions" { | ||
| count = var.backup_plan_config_rds.enable ? 1 : 0 | ||
| name = "cross_account_iam_permissions_policy" | ||
| role = aws_iam_role.iam_for_lambda_copy_job[0].id | ||
| policy = data.aws_iam_policy_document.lambda_copy_job_permissions.json | ||
| } | ||
|
|
||
| data "archive_file" "start_cross_account_copy_job_lambda_zip" { | ||
| type = "zip" | ||
| source_dir = "${path.module}/resources" | ||
| output_path = "${path.module}/.terraform/archive_files/start_cross_account_copy_job_lambda.zip" | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "start_cross_account_copy_job_lambda" { | ||
| count = var.backup_plan_config_rds.enable ? 1 : 0 | ||
| filename = data.archive_file.start_cross_account_copy_job_lambda_zip.output_path | ||
| source_code_hash = data.archive_file.start_cross_account_copy_job_lambda_zip.output_base64sha256 | ||
| function_name = "start_cross_account_copy_job" | ||
| role = aws_iam_role.iam_for_lambda_copy_job[0].arn | ||
| handler = "start_cross_account_copy_job.lambda_handler" | ||
| runtime = "python3.12" | ||
| environment { | ||
| variables = { | ||
| aws_account_id = data.aws_caller_identity.current.account_id, | ||
| backup_account_id = var.backup_copy_vault_account_id, | ||
| backup_copy_vault_arn = var.backup_copy_vault_arn, | ||
| backup_role_arn = aws_iam_role.backup.arn, | ||
| destination_vault_retention_period = var.destination_vault_retention_period | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "aws_lambda_permission" "allow_eventbridge" { | ||
| count = var.backup_plan_config_rds.enable ? 1 : 0 | ||
| statement_id = "AllowExecutionFromEventbridge" | ||
| action = "lambda:InvokeFunction" | ||
| function_name = aws_lambda_function.start_cross_account_copy_job_lambda[0].function_name | ||
| principal = "events.amazonaws.com" | ||
| source_arn = "arn:aws:events:eu-west-2:${data.aws_caller_identity.current.account_id}:rule/Cross-Account-Copy-Job-rule" | ||
| } | ||
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
modules/aws-backup-source/resources/start_cross_account_copy_job.py
This file contains hidden or 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,43 @@ | ||
| import json | ||
| import boto3 | ||
| import logging | ||
| import os | ||
|
|
||
| # Initialize AWS Backup client and logger | ||
| backup_client = boto3.client('backup') | ||
| logger = logging.getLogger() | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
| # Create a Secrets Manager client | ||
| region_name = os.environ.get('AWS_REGION') | ||
|
|
||
| aws_account_id = os.environ.get('aws_account_id') | ||
| backup_account_id = os.environ.get('backup_account_id') | ||
| backup_copy_vault_arn = os.environ.get('backup_copy_vault_arn') | ||
| backup_role_arn = os.environ.get('backup_role_arn') | ||
| destination_vault_retention_period = int(os.environ.get('destination_vault_retention_period')) | ||
|
|
||
| def lambda_handler(event, context): | ||
| # Log the incoming event for debugging purposes | ||
| logger.info(f"Received Event: {json.dumps(event)}") | ||
|
|
||
| # Extract the recovery point ARN from the event | ||
| recovery_point_arn = event['detail']['serviceEventDetails']['recoveryPointArn'] | ||
| source_vault_name = event['detail']['serviceEventDetails']['backupVaultName'] | ||
| logger.info(f"Detected new recovery point in vault {source_vault_name}: {recovery_point_arn}") | ||
|
|
||
| # Start the copy job to the destination vault in another AWS account | ||
| backup_client.start_copy_job( | ||
| RecoveryPointArn=recovery_point_arn, | ||
| SourceBackupVaultName=source_vault_name, | ||
| DestinationBackupVaultArn=backup_copy_vault_arn, | ||
| IamRoleArn=backup_role_arn, | ||
| Lifecycle={ | ||
| 'DeleteAfterDays': destination_vault_retention_period | ||
| } | ||
| ) | ||
|
|
||
| return { | ||
| 'statusCode': 200, | ||
| 'body': json.dumps('Copy job started successfully.') | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.