You are correct. The code snippet you provided uses deprecated features for configuring an S3 bucket. Let's break down why and how to fix it:
1. ACL (Access Control Lists) - Deprecated (sort of)
Why: While not completely removed, using ACLs for granting access to S3 buckets is now strongly discouraged by AWS. They are overly complex and often lead to misconfigurations. Bucket Policies and IAM roles provide more granular and manageable access control.
How to Fix:Remove the acl = "private" line and manage access using Bucket Policies and/or IAM Roles.
2. versioning Block:
Why: The versioning block itself is not deprecated, but using it without also specifying mfa_delete will trigger the deprecation notice, this is because to properly secure versioning, you need to protect it from malicious or accidental deletion via MFA Delete.
How to Fix: If you're using versioning, which you should, decide if you need MFA delete. If not, just keep versioning as you have it.
3. server_side_encryption_configuration Block:
Why: The structure of this block is deprecated in Terraform. The correct way to configure server-side encryption is using the bucket_encryption block.
How to Fix: Replace the server_side_encryption_configuration block with bucket_encryption.
Here's the corrected Terraform code:
terraform
resource "aws_s3_bucket" "cloudtrail_logs" {
bucket = "${trimprefix(var.account_name, "ct-")}-cloudtrail-logs"
# Removed acl = "private" - will manage with a bucket policy
versioning {
enabled = true
}
bucket_encryption {
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
# Example Bucket Policy (Replace with your actual policy)
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowCloudTrailReadAccess"
Effect = "Allow"
Principal = {
Service = "cloudtrail.amazonaws.com"
}
Action = [
"s3:GetBucketAcl",
"s3:PutObject"
]
Resource = [
"arn:aws:s3:::${trimprefix(var.account_name, "ct-")}-cloudtrail-logs",
"arn:aws:s3:::${trimprefix(var.account_name, "ct-")}-cloudtrail-logs/*"
]
Condition = {
StringEquals = {
"s3:x-amz-acl" = "bucket-owner-full-control"
},
ArnLike = {
"aws:SourceArn" = "arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/*" # Replace with your trail ARN if needed
}
}
},
{
Sid = "AllowCloudTrailKMSKey"
Effect = "Allow"
Principal = {
Service = "cloudtrail.amazonaws.com"
}
Action = "kms:GenerateDataKey"
Resource = "arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/${var.kms_key_id}" #replace with KMS key
Condition = {
ArnLike = {
"aws:SourceArn" = "arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/*" # Replace with your trail ARN if needed
}
}
},
{
Sid = "AllowBucketOwnerFullControl"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root" #Replace with your Account ARN
}
Action = "s3:*"
Resource = [
"arn:aws:s3:::${trimprefix(var.account_name, "ct-")}-cloudtrail-logs",
"arn:aws:s3:::${trimprefix(var.account_name, "ct-")}-cloudtrail-logs/*"
]
}
]
})
}
#Required Data Sources
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
Key Improvements & Explanations:
Removed acl: The acl line is gone. We now rely on a bucket policy for access control. This is best practice.
bucket_encryption: The deprecated server_side_encryption_configuration block is replaced with the bucket_encryption block, This ensures the S3 bucket is properly configured for server-side encryption using AES-256. AES-256 is a strong, commonly used encryption algorithm. You can also specify KMS if you prefer to manage your encryption keys with KMS.
Bucket Policy Example: The provided example bucket policy includes:
AllowCloudTrailReadAccess: This allows CloudTrail to write logs to the bucket. Crucially, it includes a Condition that requires CloudTrail to set the bucket-owner-full-control ACL. This is important to ensure the account owning the bucket has full control over the objects written by CloudTrail. Also ensures the the account is the source account.
AllowCloudTrailKMSKey: This policy statement enables CloudTrail to utilize KMS to encrypt logs. It allows cloudtrail to use the provided KMS key to encrypt logs on write to S3.
AllowBucketOwnerFullControl: This provides the bucket owner full permissions over the bucket and its contents.
Important Considerations:
Bucket Policy Complexity: Bucket policies can become complex quickly. Carefully design your policy to grant only the necessary permissions and use the principle of least privilege. Test your policies thoroughly.
IAM Roles: In addition to bucket policies, consider using IAM roles for applications or users that need to access the S3 bucket. This allows you to grant permissions to specific identities.
KMS Encryption: If you want to use AWS KMS for server-side encryption (SSE-KMS), you'll need to:
Create a KMS key.
Update the bucket_encryption block to specify the kms_key_id.
Modify the bucket policy to allow CloudTrail to use the KMS key.
mfa_delete: Consider using mfa_delete within the versioning block for increased security if your requirements dictate it.
By making these changes, you'll have a more secure and maintainable S3 bucket configuration that aligns with current AWS best practices and Terraform conventions. Remember to replace the placeholder values (like account IDs and CloudTrail trail ARN) in the bucket policy with your actual values.