Skip to content
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

S3 to S3 Replication in SAM #2062

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ec9210e
S3 to S3 Replication in SAM
jorgetovar Jan 22, 2024
acf7064
S3 to S3 Replication in SAM
jorgetovar Jan 22, 2024
8c8196b
Update s3-s3-replication-sam/README.md
jorgetovar Feb 29, 2024
22d44c6
Update s3-s3-replication-sam/README.md
jorgetovar Feb 29, 2024
e74c8f4
Update s3-s3-replication-sam/example-pattern.json
jorgetovar Feb 29, 2024
73fbd9b
Update s3-s3-replication-sam/example-pattern.json
jorgetovar Feb 29, 2024
304033e
S3 replication
jorgetovar Feb 29, 2024
5717faa
Merge branch 'jorgetovar-feature-s3-replication' of https://github.co…
jorgetovar Feb 29, 2024
08c7e79
Update s3-s3-replication-sam/example-pattern.json
jorgetovar Feb 29, 2024
f360089
Update s3-s3-replication-sam/template.yaml
jorgetovar Feb 29, 2024
7fe5575
S3 replication
jorgetovar Feb 29, 2024
79684c6
Merge branch 'jorgetovar-feature-s3-replication' of https://github.co…
jorgetovar Feb 29, 2024
c695e1d
Update s3-s3-replication-sam/example-pattern.json
jorgetovar Mar 26, 2024
7321c19
Update links and improve how it works
jorgetovar Mar 26, 2024
b5c9cc9
Update example-pattern.json
jorgetovar Apr 17, 2024
6e85a36
Add youtube cross region replication
jorgetovar May 10, 2024
9fbc567
Links to relevant documentation
jorgetovar May 10, 2024
88b3228
Links to relevant documentation
jorgetovar May 10, 2024
ef87e0d
Add final pattern file
bfreiberg May 23, 2024
16de673
Fix service name
bfreiberg May 23, 2024
4c1d55d
Amazon S3 replication name
jorgetovar May 28, 2024
f19d209
Merge branch 'jorgetovar-feature-s3-replication' of https://github.co…
jorgetovar May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions s3-s3-replication-sam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Amazon S3 Bucket Replication

This pattern demonstrates how to replicate S3 bucket objects to multiple S3 buckets. Implemented with SAM.

**Key Benefits**:
jorgetovar marked this conversation as resolved.
Show resolved Hide resolved

- **Data Redundancy and Durability**:
S3 replication ensures that your data is replicated across multiple S3 buckets, offering redundancy and safeguarding against data loss.
- **Disaster Recovery**:
By replicating your data to a different region or account, you establish a robust disaster recovery strategy. In the event of an outage or data corruption, you can quickly restore operations using the replicated data.
- **Global Content Distribution**:
Replicate frequently accessed data to different geographical locations, reducing latency and enhancing the user experience for global audiences.
- **Compliance and Data Retention**:
Address compliance requirements by replicating data to a separate account or region. This ensures data integrity and facilitates adherence to regulatory standards.
- **Operational Efficiency**:
S3 replication operates asynchronously, allowing you to focus on other tasks while data is efficiently copied in the background.


Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/s3-s3-replication-sam](https://serverlessland.com/patterns/s3-s3-replication-sam)

**Important**: This application utilizes various AWS services, and there are associated costs after Free Tier usage. Please refer to the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any incurred AWS costs. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. Ensure that the IAM user used has sufficient permissions to make necessary AWS service calls and manage resources.
* [Install and configure AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)
* [Install Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [Install AWS Serverless Application Model (AWS SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal, and clone the GitHub repository:
```bash
git clone https://github.com/aws-samples/serverless-patterns
```
1. Change the directory to the pattern directory:
```bash
cd s3-s3-replication-sam
```
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```bash
sam deploy --guided
```
1. During the prompts:
* Enter a stack name.
* Enter the desired AWS Region.
* Allow SAM CLI to create IAM roles with the required permissions.

Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in the future to use these defaults.

1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs used for testing.

## How it Works

S3 replication in the same region involves copying objects from one S3 bucket to another within the same AWS region. When replication is configured, S3 automatically copies objects from the source bucket to the destination bucket, ensuring redundancy and data durability. This process helps in scenarios such as data backup, compliance requirements, and minimizing latency for accessing data.


## Testing

Upload some images to the source bucket, and then check the replication bucket. You should be able to see the same data replicated there.

## Cleanup

1. Empty buckets
```bash
aws s3 rm s3://BUCKET_NAME_SOURCE --recursive
aws s3 rm s3://BUCKET_NAME_REPLICA --recursive

```
2. Confirm the stack has been deleted:
```bash
sam delete --stack-name STACK_NAME

```

----

Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
53 changes: 53 additions & 0 deletions s3-s3-replication-sam/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"title": "Amazon S3 bucket replication",
"description": "This pattern sets up replication between S3 buckets.",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
jorgetovar marked this conversation as resolved.
Show resolved Hide resolved
"This pattern sets up S3 replication between two buckets. The replication configuration is set up in the source bucket, and the destination bucket is set up to receive the replicated objects. The replication configuration is set up to replicate all objects in the source bucket to the destination bucket."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/s3-s3-replication-sam",
"templateURL": "serverless-patterns/s3-s3-replication-sam",
"projectFolder": "s3-s3-replication-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Same Region Replication",
"link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/replication.html#srr-scenario"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>sam delete</code>"
]
},
"authors": [
{
"name": "Jorge Tovar",
"image": "https://avatars.githubusercontent.com/u/21094604?v=4",
"bio": "Software Architect, passionate about serverless technologies and cloud computing.",
"linkedin": "jorgetovar-sa",
"twitter": "https://twitter.com/jorgetovar621"
}
]
}
72 changes: 72 additions & 0 deletions s3-s3-replication-sam/s3-s3-replication-sam.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"title": "Amazon S3 bucket replication",
"description": "This pattern sets up replication between S3 buckets.",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This pattern sets up S3 replication between two buckets. The replication configuration is set up in the source bucket, and the destination bucket is set up to receive the replicated objects. The replication configuration is set up to replicate all objects in the source bucket to the destination bucket."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/s3-s3-replication-sam",
"templateURL": "serverless-patterns/s3-s3-replication-sam",
"projectFolder": "s3-s3-replication-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Same Region Replication",
"link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/replication.html#srr-scenario"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>sam delete</code>"
]
},
"authors": [
{
"name": "Jorge Tovar",
"image": "https://avatars.githubusercontent.com/u/21094604?v=4",
"bio": "Software Architect, passionate about serverless technologies and cloud computing.",
"linkedin": "jorgetovar-sa",
"twitter": "https://twitter.com/jorgetovar621"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "s3",
"label": "Source S3 bucket"
},
"icon2": {
"x": 80,
"y": 50,
"service": "s3",
"label": "Destination S3 bucket"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": "Replication"
}
}
}
101 changes: 101 additions & 0 deletions s3-s3-replication-sam/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'S3 bucket replication'

Parameters:

BucketSourceName:
Description: "Bucket Source Name (must be unique)"
Type: String

BucketReplicaName:
Description: "Bucket Replica Name (must be unique)"
Type: String

Resources:

ReplicationRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service: 's3.amazonaws.com'
Action: 'sts:AssumeRole'
Policies:
- PolicyName: 'ReplicationPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 's3:GetBucketVersioning'
- 's3:ListBucket'
- s3:GetReplicationConfiguration
- s3:GetObjectVersionForReplication
- s3:GetObjectVersionAcl
- s3:GetObjectVersionTagging
- s3:GetObjectRetention
- s3:GetObjectLegalHold
Resource:
- !Sub "arn:aws:s3:::${BucketSource}"
- !Sub "arn:aws:s3:::${BucketReplica}"
- Effect: 'Allow'
Action:
- 's3:ReplicateObject'
- 's3:ReplicateDelete'
- 's3:ReplicateTags'
- 's3:GetObjectVersionTagging'
- 's3:ObjectOwnerOverrideToBucketOwner'
Resource:
- !Sub "arn:aws:s3:::${BucketSource}"
- !Sub "arn:aws:s3:::${BucketReplica}"


BucketSource:
Type: 'AWS::S3::Bucket'
Properties:
VersioningConfiguration:
Status: 'Enabled'
ReplicationConfiguration:
Role: !GetAtt ReplicationRole.Arn
Rules:
- Destination:
Bucket: !GetAtt BucketReplica.Arn
Prefix: ''
Status: 'Enabled'
BucketName: !Ref BucketSourceName

BucketReplica:
Type: 'AWS::S3::Bucket'
Properties:
VersioningConfiguration:
Status: 'Enabled'
BucketName: !Ref BucketReplicaName

BucketReplicaPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref BucketReplica
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: "Object Level Permissions"
Effect: "Allow"
Principal:
AWS: !GetAtt ReplicationRole.Arn
Action:
- "s3:ReplicateObject"
- "s3:ReplicateDelete"
Resource: !Sub "arn:aws:s3:::${BucketReplica}/*"
- Sid: "Bucket Level Permissions"
Effect: "Allow"
Principal:
AWS: !GetAtt ReplicationRole.Arn
Action:
- "s3:List*"
- "s3:GetBucketVersioning"
- "s3:PutBucketVersioning"
Resource: !Sub "arn:aws:s3:::${BucketReplica}"