Skip to content

Instantly share code, notes, and snippets.

@marier-nico
Created August 23, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marier-nico/5c7c99c7670162175f2c1a4c03703626 to your computer and use it in GitHub Desktop.
Save marier-nico/5c7c99c7670162175f2c1a4c03703626 to your computer and use it in GitHub Desktop.
Terraform module to create backup buckets
resource "aws_s3_bucket" "main_bucket" {
provider = aws.main_region
bucket = var.bucket_name
acl = "private"
versioning {
enabled = true
}
lifecycle_rule {
id = "infrequent_access"
enabled = true
transition {
days = 30
storage_class = "STANDARD_IA"
}
noncurrent_version_expiration {
days = 15
}
}
replication_configuration {
role = aws_iam_role.s3_replication_role.arn
rules {
id = "ReplicateAll"
status = "Enabled"
destination {
bucket = aws_s3_bucket.replication_bucket.arn
storage_class = "STANDARD"
}
}
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
resource "aws_s3_bucket_public_access_block" "block_main_bucket_public_access" {
bucket = aws_s3_bucket.main_bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket" "replication_bucket" {
provider = aws.replication_region
bucket = "${var.bucket_name}-replicated"
acl = "private"
versioning {
enabled = true
}
lifecycle_rule {
id = "infrequent_access"
enabled = true
transition {
days = 30
storage_class = "STANDARD_IA"
}
noncurrent_version_expiration {
days = 15
}
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
resource "aws_s3_bucket_public_access_block" "block_replication_bucket_public_access" {
provider = aws.replication_region
bucket = aws_s3_bucket.replication_bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
provider "aws" {
alias = "main_region"
}
provider "aws" {
alias = "replication_region"
}
output "main_bucket_arn" {
value = aws_s3_bucket.main_bucket.arn
}
output "replication_bucket_arn" {
value = aws_s3_bucket.replication_bucket.arn
}
resource "aws_iam_role" "s3_replication_role" {
name = "${var.bucket_name}-replication-role"
assume_role_policy = data.aws_iam_policy_document.s3_replication_role_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "s3_replication_role_policy_attachment" {
role = aws_iam_role.s3_replication_role.name
policy_arn = aws_iam_policy.s3_replication_role_policy.arn
}
data "aws_iam_policy_document" "s3_replication_role_assume_role_policy" {
statement {
sid = "AllowS3AssumeReplicationRole"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "s3_replication_role_policy" {
name = "${var.bucket_name}-replication-role-policy"
policy = data.aws_iam_policy_document.s3_replication_role_policy_document.json
}
data "aws_iam_policy_document" "s3_replication_role_policy_document" {
statement {
sid = "GetSourceBucketConfiguration"
effect = "Allow"
actions = [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
]
resources = [aws_s3_bucket.main_bucket.arn]
}
statement {
sid = "GetObjectVersion"
effect = "Allow"
actions = [
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl"
]
resources = ["${aws_s3_bucket.main_bucket.arn}/*"]
}
statement {
sid = "ReplicateObjects"
effect = "Allow"
actions = [
"s3:ReplicateObject",
"s3:ReplicateDelete"
]
resources = ["${aws_s3_bucket.replication_bucket.arn}/*"]
}
}
variable "bucket_name" {
type = string
description = "Name of the bucket to create"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment