

# Setting up ongoing periodic data replication
<a name="ongoing-periodic-data-replication"></a>

With ongoing periodic data replication, you can set up a schedule that automatically takes a snapshot of a source volume and performs an incremental replication of that snapshot on a destination volume at a certain interval, for example every 15 minutes. You can schedule ongoing periodic data replication between two volumes on FSx for OpenZFS file systems within or across AWS Regions and accounts by using the solution provided in this section.

**Topics**
+ [Architecture overview](#architecture-overview-periodic-replication)
+ [Required permissions](#required-permissions-periodic-replication)
+ [Step 1: Initializing and deploying the application](#step-1-periodic-replication)
+ [Step 2: Monitoring periodic replication](#step2-periodic-replication)

## Architecture overview
<a name="architecture-overview-periodic-replication"></a>

Deploying this solution builds the following resources in the AWS Cloud.

![Architecture of the periodic data replication solution.](https://docs.aws.amazon.com/fsx/latest/OpenZFSGuide/images/openzfs-periodic-data-replication-architecture.PNG)


The diagram illustrates the following periodic replication workflow.

1. AWS Serverless Application Model (SAM) automates the deployment of the FSx for OpenZFS periodic replication solution. For more information about AWS SAM, see [What is the AWS Serverless Application Model (AWS SAM)?](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) in the *AWS Serverless Application Model* User Guide.

1. The SAM template deploys an Amazon EventBridge scheduler, an AWS Lambda function, an Amazon SNS queue, and an IAM role. The IAM role gives the Lambda function permission to call the necessary Amazon FSx API operations.

1. The EventBridge scheduler runs on a schedule you specify as a cron pattern during the initial deployment. For more information about cron patterns, see [Creating an Amazon EventBridge rule that runs on a schedule](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html) in the *Amazon EventBridge* User Guide. The scheduler invokes a Lambda function that calls the Amazon FSx `CreateSnapshot` API operation to create a snapshot of the source volume.

1. Once the snapshot is available, the Lambda function calls the Amazon FSx `CopySnapshotAndUpdateVolume` API operation to start replicating the source snapshot data to the destination volume.

1. The Lambda function sends a notification message to the Amazon SNS queue when replication starts, if you choose to be notified during the initial deployment. A notification is always sent when a snapshot cannot be created or the replication cannot be initiated.

## Required permissions
<a name="required-permissions-periodic-replication"></a>

The following permissions are required to use the custom snapshot schedule CloudFormation template.
+ `AmazonS3FullAccess`
+ `AWSCloudFormationFullAccess`
+ `AmazonEventBridgeFullAccess`
+ `IAMFullAccess`
+ `AmazonSNSFullAccess`
+ `AWSKeyManagementServicePowerUser`
+ `AWSLambda_FullAccess`

For more information about using IAM to set up permissions, see [How Amazon FSx for OpenZFS works with IAM](security_iam_service-with-iam.md).

## Step 1: Initializing and deploying the application
<a name="step-1-periodic-replication"></a>

The following procedure configures and deploys the periodic replication solution. It takes about five minutes to deploy. Before you begin this step, 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 an Amazon FSx for OpenZFS volume](creating-volumes.md), [Creating a snapshot](snapshots-openzfs.md#creating-snapshots), and [Using on-demand data replication](on-demand-replication.md#how-to-use-data-replication).

**Note**  
Implementing this solution incurs billing for the associated AWS services. For more information, see the pricing details pages for those services.

**To launch the periodic replication solution stack**

1. Follow the instructions on the [Replicate FSx-OpenZFS volumes across file systems](https://serverlessland.com/patterns/eventbridge-lambda-fsx-openzfs-periodic-replication) page to download the serverless pattern.

1. For **Parameters**, review the following parameters for the template and modify them for the needs of your periodic replication. This solution uses the following default values.


<table>
<thead>
  <tr><th><b>Parameter</b></th><th><b>Default</b></th><th><b>Description</b></th></tr>
</thead>
<tbody>
  <tr><td>Source volume ID</td><td>No default value</td><td>The ID of the source volume from which data will be periodically replicated.</td></tr>
  <tr><td>Destination volume ID</td><td>No default value</td><td>The ID of the destination volume that will become a replica of the source volume.</td></tr>
  <tr><td>CronSchedule</td><td>[0 0/6 **?*] (every six hours)</td><td>The schedule to replicate data from the source volume to the destination volume.</td></tr>
  <tr><td>SnapshotName</td><td>fsx-scheduled-snapshot</td><td>The name for the scheduled snapshots that will be taken of the source volume. Appears in the <b>Snapshot Name</b> column of the Amazon FSx Console.</td></tr>
  <tr><td>Snapshot retention (days)</td><td>7</td><td>The number of days to keep user-initiated snapshots. The Lambda function deletes user-initiatted snapshots that are kept after this number of days.</td></tr>
  <tr><td>SuccessNotification</td><td>Yes</td><td>Choose whether to be notified when the replication is successfully initiated. A notification is always sent when a snapshot fails to create or the replication fails to start.</td></tr>
  <tr><td>Email</td><td>No default value</td><td>The email address that you would like notifications to be sent to.</td></tr>
  <tr><td>CopyStrategy</td><td>INCREMENTAL_COPY</td><td>The <b>CopyStrategy</b> parameter for the <code>CopySnapshotAndUpdateVolume</code> API operation. For more information, see <a href="https://docs.aws.amazon.com/fsx/latest/APIReference/API_CopySnapshotAndUpdateVolume.html">CopySnapshotAndUpdateVolume</a> in the Amazon FSx API.</td></tr>
  <tr><td>Options</td><td>None</td><td>The <b>Options</b> parameter for the <code>CopySnapshotAndUpdateVolume</code> API operation. For more information, see <a href="https://docs.aws.amazon.com/fsx/latest/APIReference/API_CopySnapshotAndUpdateVolume.html">CopySnapshotAndUpdateVolume</a> in the Amazon FSx API.</td></tr>
</tbody>
</table>


1. In the AWS SAM CLI, run the following command to deploy the resources specified in the SAM template.

   ```
   sam deploy --guided \
   --stack-name fsxz-periodic-replication \
   --template-file fsx-openzfs-periodic-replication.yaml \
   --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM CAPABILITY_NAMED_IAM
   ```

   You will be asked if you would like to update any parameters.

1. Choose **Enter** to deploy the template.

## Step 2: Monitoring periodic replication
<a name="step2-periodic-replication"></a>

You can monitor the status of the periodic replication workflow using the Amazon FSx Console, AWS CLI, and API. For more information on how to monitor periodic replication using the Amazon FSx Console, see [Monitoring progress of on-demand data replication](on-demand-replication.md#how-to-monitor-data-replication).

To use the AWS CLI or API to track the progress of your replication, call the [describe-volumes](https://docs.aws.amazon.com/cli/latest/reference/fsx/describe-volumes.html) CLI command or the [DescribeVolumes](https://docs.aws.amazon.com/fsx/latest/APIReference/API_DescribeVolumes.html) API operation to view the `AdministrativeActions` array for the destination volume. The following example shows the response for an incremental copy on-demand data replication task.

```
"AdministrativeActions": [
   {
    "AdministrativeActionType": "VOLUME_UPDATE_WITH_SNAPSHOT",
    "ProgressPercent": 100,
    "RequestTime": 1699997847.438,
    "Status": "COMPLETED",
    "TargetVolumeValues": {
    "OpenZFSConfiguration": {
        "RecordSizeKiB": 128,
        "DataCompressionType": "ZSTD",
        "DeleteIntermediateSnaphots": true,
        "DeleteClonedVolumes": false,
        "DeleteIntermediateData": true,
        "SourceSnapshotARN": "arn:aws:fsx:us-east-1:609492434915:snapshot/fsvol-0e1ab09de954a352f/fsvolsnap-01dda47dcbb24ddd0",
        "DestinationSnapshot": "fsvolsnap-0afef62088c7c9060"
        }
    },
    "TotalTransferBytes": 44144,
    "RemainingTransferBytes": 0
   },
```