Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
65 changes: 65 additions & 0 deletions examples/lifecycle-n-replication/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
resource "aws_iam_role" "replication" {
name = "s3-bucket-replication-${random_pet.this.id}"

assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}

resource "aws_iam_policy" "replication" {
name = "s3-bucket-replication-${random_pet.this.id}"

policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::${local.bucket_name}"
]
},
{
"Action": [
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::${local.bucket_name}/*"
]
},
{
"Action": [
"s3:ReplicateObject",
"s3:ReplicateDelete"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::${local.replica_bucket_name}/*"
}
]
}
POLICY
}

resource "aws_iam_policy_attachment" "replication" {
name = "s3-bucket-replication-${random_pet.this.id}"
roles = [aws_iam_role.replication.name]
policy_arn = aws_iam_policy.replication.arn
}
112 changes: 112 additions & 0 deletions examples/lifecycle-n-replication/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
locals {
bucket_name = "origin-s3-bucket-${random_pet.this.id}"
replica_bucket_name = "replica-s3-bucket-${random_pet.this.id}"

origin_region = "eu-west-1"
replica_region = "eu-central-1"
}

provider "aws" {
region = local.origin_region
alias = "source"
}

provider "aws" {
region = local.replica_region
alias = "replica"
}

resource "random_pet" "this" {
length = 2
}

module "s3_bucket" {
source = "../../"

providers = {
aws = aws.source
}

bucket = local.bucket_name

force_destroy = true

# Versioning
versioning = {
enabled = true
}

# Replication
replication_configuration = {
role = aws_iam_role.replication.arn

rules = [
{
id = "custom-additional-rule",
priority = 15,
delete_marker_replication = true

destination = {
bucket = "arn:aws:s3:::${local.replica_bucket_name}"
replica_kms_key_id = aws_kms_key.replica.arn
storage_class = "STANDARD_IA"
}

filter = {
prefix = ""
}

source_selection_criteria = {
sse_kms_encrypted_objects = {
enabled = true
}
}

},
]
}

# Lifecycle
lifecycle_rule = [
{
id = "abort-incomplete-multipart-upload"
status = "Enabled"

abort_incomplete_multipart_upload = {
days_after_initiation = 35
}

filter = {
}

noncurrent_version_expiration = {
noncurrent_days = 35
}
},
]

depends_on = [module.bucket_replica]
}

resource "aws_kms_key" "replica" {
provider = aws.replica

description = "S3 bucket replication KMS key"
deletion_window_in_days = 7
}

module "bucket_replica" {
source = "../../"

providers = {
aws = aws.replica
}

versioning = {
enabled = true
}

bucket = local.replica_bucket_name

force_destroy = true
}
39 changes: 39 additions & 0 deletions examples/lifecycle-n-replication/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
output "s3_bucket_id" {
description = "The name of the bucket."
value = module.s3_bucket.s3_bucket_id
}

output "s3_bucket_arn" {
description = "The ARN of the bucket. Will be of format arn:aws:s3:::bucketname."
value = module.s3_bucket.s3_bucket_arn
}

output "s3_bucket_bucket_domain_name" {
description = "The bucket domain name. Will be of format bucketname.s3.amazonaws.com."
value = module.s3_bucket.s3_bucket_bucket_domain_name
}

output "s3_bucket_bucket_regional_domain_name" {
description = "The bucket region-specific domain name. The bucket domain name including the region name, please refer here for format. Note: The AWS CloudFront allows specifying S3 region-specific endpoint when creating S3 origin, it will prevent redirect issues from CloudFront to S3 Origin URL."
value = module.s3_bucket.s3_bucket_bucket_regional_domain_name
}

output "s3_bucket_hosted_zone_id" {
description = "The Route 53 Hosted Zone ID for this bucket's region."
value = module.s3_bucket.s3_bucket_hosted_zone_id
}

output "s3_bucket_region" {
description = "The AWS region this bucket resides in."
value = module.s3_bucket.s3_bucket_region
}

output "s3_bucket_website_endpoint" {
description = "The website endpoint, if the bucket is configured with a website. If not, this will be an empty string."
value = module.s3_bucket.s3_bucket_website_endpoint
}

output "s3_bucket_website_domain" {
description = "The domain of the website endpoint, if the bucket is configured with a website. If not, this will be an empty string. This is used to create Route 53 alias records. "
value = module.s3_bucket.s3_bucket_website_domain
}
Empty file.
14 changes: 14 additions & 0 deletions examples/lifecycle-n-replication/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.70"
}
random = {
source = "hashicorp/random"
version = ">= 2.0"
}
}
}
8 changes: 6 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,12 @@ resource "aws_s3_bucket_lifecycle_configuration" "this" {
}
}

# Must have bucket versioning enabled first
depends_on = [aws_s3_bucket_versioning.this]
depends_on = [
# Must have bucket versioning enabled first
aws_s3_bucket_versioning.this,
# Must wait for replication configuration to propagate
aws_s3_bucket_replication_configuration.this
]
}

resource "aws_s3_bucket_object_lock_configuration" "this" {
Expand Down
Loading