|
| 1 | +# Cross-account cross-region replication for FSx for OpenZFS volumes with AWS Lambda |
| 2 | +Amazon EventBridge triggers an AWS Lambda function to replicate FSx for OpenZFS volumes across file systems located in the same account and region, or across different accounts and regions. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +The [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) template deploys an Amazon EventBridge Scheduler to trigger an AWS Lambda function based on a user-defined schedule. This function copies the snapshot of a volume and transfers it to the target FSx system, which can be located in the same or a different AWS account and/or region. |
| 7 | + |
| 8 | +For FSx for OpenZFS periodic volume replication in same account and same region, please refer to the Serverless Land Pattern <https://serverlessland.com/patterns/eventbridge-lambda-fsx-openzfs-periodic-replication> |
| 9 | + |
| 10 | +The template contains a sample Lambda function that creates a snapshot of the source FSx Volume ID. Once the snapshot becomes available, it invokes another Lambda function in the destination AWS account and/or region, which initiates the replication by calling the copy_snapshot_and_update_volume API. This solution also notifies users via an Amazon SNS topic of any errors and snapshot creation details. |
| 11 | + |
| 12 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +* [Create two AWS accounts for cross account setup](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have, create them and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 17 | + |
| 18 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configure two profiles with credentials for the individual accounts as below: |
| 19 | + |
| 20 | + ``` |
| 21 | + [default] |
| 22 | + [crossaccount] |
| 23 | + ``` |
| 24 | +
|
| 25 | +
|
| 26 | +- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 27 | +- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 28 | +- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 29 | +- Make sure that you have the ID of the source and destination volumes that you would like to initiate the replication between. For more information on these resources, see [Creating FSx for OpenZFS file systems](https://docs.aws.amazon.com/fsx/latest/OpenZFSGuide/creating-file-systems.html), [Creating a volume](https://docs.aws.amazon.com/fsx/latest/OpenZFSGuide/creating-volumes.html), [Creating a snapshot](https://docs.aws.amazon.com/fsx/latest/OpenZFSGuide/snapshots-openzfs.html#creating-snapshots), and [Using on-demand data replication](https://docs.aws.amazon.com/fsx/latest/OpenZFSGuide/on-demand-replication.html#how-to-use-data-replication). |
| 30 | +
|
| 31 | +## Deployment Instructions |
| 32 | +
|
| 33 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 34 | + ``` |
| 35 | + git clone https://github.com/aws-samples/serverless-patterns |
| 36 | + ``` |
| 37 | +2. Change directory to the pattern directory: |
| 38 | + ``` |
| 39 | + cd eventbridge-lambda-fsx-openzfs-cross-account-region-periodic-replication |
| 40 | + ``` |
| 41 | +3. From the command line, use the AWS SAM command listed below to deploy the AWS resources in the destination AWS account as specified in the destination-template.yaml file. Note that an AWS CLI profile named crossaccount must be configured with AWS credentials for the destination/target AWS account. |
| 42 | + ``` |
| 43 | + sam deploy --guided --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM CAPABILITY_NAMED_IAM -t destination-template.yaml --profile crossaccount |
| 44 | + ``` |
| 45 | +4. During the prompts: |
| 46 | + - Enter a target stack name |
| 47 | + - Enter the desired AWS Region |
| 48 | + - Enter a TargetVolumeID |
| 49 | + - Enter a CopySnapshotAndUpdateVolume - "Options" parameter. Comma (,) separated values |
| 50 | + - Enter a CopySnapshotAndUpdateVolume - "CopyStrategy" parameter (Default = INCREMENTAL_COPY) |
| 51 | + - Enter source AWS account Id |
| 52 | + - Allow SAM CLI to create IAM roles with the required permissions. |
| 53 | + - Save arguments to configuration file [Y/n]: N |
| 54 | +5. Once the above stack is deployed in the target account/region, use the AWS SAM command listed below to deploy the resources in the source AWS account using the source-template.yaml file. |
| 55 | + ``` |
| 56 | + sam deploy --guided --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM CAPABILITY_NAMED_IAM -t source-template.yaml |
| 57 | + ``` |
| 58 | +6. During the prompts: |
| 59 | + - Enter a source stack name |
| 60 | + - Enter the desired AWS Region |
| 61 | + - Enter a SourceVolumeID |
| 62 | + - Enter a CRON schedule for snapshots (Default = [0 0/6 * * ? *] every six hours) |
| 63 | + - Enter a value of snapshot Name (Default = fsx_scheduled_snapshot) |
| 64 | + - Enter an Email for notifications |
| 65 | + - Allow Success Notification (Default = Yes) |
| 66 | + - Enter number of days to retain custom-scheduled snapshots (Default = 7 days) |
| 67 | + - Enter target AWS account Id |
| 68 | + - Enter target region |
| 69 | + - Enter target stack name used previously |
| 70 | + - Save arguments to configuration file [Y/n]: N |
| 71 | +7. Note the outputs from the previous SAM deploy commands. These contain the resource names and/or ARNs which will be used for later review. |
| 72 | +
|
| 73 | +## How it works |
| 74 | +
|
| 75 | +This pattern sets up the following resources: |
| 76 | +
|
| 77 | +- An Amazon EventBridge Scheduler that triggers a Lambda function based on the schedule defined by you to create snapshots of the provided FSx Source Volume ID. |
| 78 | +- A sample [Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function that creates snapshots of the source FSx Volume ID and invokes another Lambda function in the destination AWS account and/or region, which will initiate the replication by calling the [CopySnapshotAndUpdateVolume](https://docs.aws.amazon.com/fsx/latest/APIReference/API_CopySnapshotAndUpdateVolume.html) API. |
| 79 | +- The function also deletes the older snapshots in the source and target AWS account/region based on the configured retention period. |
| 80 | +- An SNS topic that sends notifications for any success or failure events while creating or replicating snapshots. |
| 81 | +
|
| 82 | +## Testing |
| 83 | +1. Based on the provided schedule, monitor the CloudWatch logs and the FSx snapshots that are created. |
| 84 | +2. The Lambda function will send various success and failure notifications to the configured email address via an SNS topic. |
| 85 | +
|
| 86 | +## Cleanup |
| 87 | +1. Change directory to the pattern directory: |
| 88 | + ``` |
| 89 | + cd serverless-patterns/eventbridge-lambda-fsx-openzfs-cross-account-region-periodic-replication |
| 90 | + ``` |
| 91 | +2. Delete resources from the source account. |
| 92 | + ``` |
| 93 | + sam delete --stack-name <source-stack-name> |
| 94 | + ``` |
| 95 | + |
| 96 | +3. Delete resources from the destination/target account. |
| 97 | + ``` |
| 98 | + sam delete --stack-name <target-stack stackname> --profile crossaccount --config-env crossacct |
| 99 | + `````` |
| 100 | +3. During the prompts: |
| 101 | + * Enter all details as per requirement. |
| 102 | +
|
| 103 | +--- |
| 104 | +
|
| 105 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 106 | +
|
| 107 | +SPDX-License-Identifier: MIT-0 |
| 108 | +
|
0 commit comments