متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 168Cloud · Terraform

How to create a safe S3 bucket with Terraform

An S3 bucket made with only aws_s3_bucket works — and misses everything security reviews ask for. In the AWS provider (v4+), each setting is a separate resource: public access block, versioning, encryption, ownership (ACLs off), a policy that refuses plain HTTP, and lifecycle rules. This how-to writes a bucket that passes typical scanners (Checkov, Trivy) and explains each block.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

168.1

The quick answer

s3.tf
resource "aws_s3_bucket" "data" {
  bucket = "acme-shop-data-prod"
  tags   = { env = "prod", team = "shop" }
}
 
resource "aws_s3_bucket_public_access_block" "data" {
  bucket                  = aws_s3_bucket.data.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
 
resource "aws_s3_bucket_ownership_controls" "data" {
  bucket = aws_s3_bucket.data.id
  rule { object_ownership = "BucketOwnerEnforced" }
}
 
resource "aws_s3_bucket_versioning" "data" {
  bucket = aws_s3_bucket.data.id
  versioning_configuration { status = "Enabled" }
}
 
resource "aws_s3_bucket_server_side_encryption_configuration" "data" {
  bucket = aws_s3_bucket.data.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm     = "aws:kms"
      kms_master_key_id = aws_kms_key.s3.arn
    }
    bucket_key_enabled = true
  }
}

168.2

Step by step

  1. What each block does:
ResourceWhy
public_access_blockThe bucket can never become public (policy or ACL).
ownership_controls (BucketOwnerEnforced)ACLs are off — access only by IAM/bucket policy.
versioningDeleted/overwritten objects can be restored.
server_side_encryption_configurationEncrypted at rest with your KMS key (bucket_key_enabled lowers KMS cost).
  1. Refuse plain HTTP with a bucket policy:
s3-policy.tf
data "aws_iam_policy_document" "tls_only" {
  statement {
    sid     = "DenyInsecureTransport"
    effect  = "Deny"
    actions = ["s3:*"]
    resources = [
      aws_s3_bucket.data.arn,
      "${aws_s3_bucket.data.arn}/*",
    ]
    principals {
      type        = "*"
      identifiers = ["*"]
    }
    condition {
      test     = "Bool"
      variable = "aws:SecureTransport"
      values   = ["false"]
    }
  }
}
 
resource "aws_s3_bucket_policy" "data" {
  bucket = aws_s3_bucket.data.id
  policy = data.aws_iam_policy_document.tls_only.json
}
  1. The KMS key with rotation:
kms.tf
resource "aws_kms_key" "s3" {
  description         = "S3 data bucket key"
  enable_key_rotation = true
}
  1. Lifecycle — old versions and unfinished uploads cost money:
s3-lifecycle.tf
resource "aws_s3_bucket_lifecycle_configuration" "data" {
  bucket = aws_s3_bucket.data.id
  rule {
    id     = "cleanup"
    status = "Enabled"
    filter {}
    noncurrent_version_expiration { noncurrent_days = 90 }
    abort_incomplete_multipart_upload { days_after_initiation = 7 }
  }
}
  1. Scan before apply:
trivy config .
checkov -d .
  1. Many buckets? Put all of this into one module (see the module how-to) — every bucket gets the same safe defaults.

الأوامر في هذا الدرس

168.3

Check that it worked

terraform plan -out=tfplan && terraform apply tfplan
aws s3api get-public-access-block --bucket acme-shop-data-prod
aws s3api get-bucket-encryption --bucket acme-shop-data-prod --query 'ServerSideEncryptionConfiguration.Rules[0]'
aws s3api get-bucket-versioning --bucket acme-shop-data-prod

All four public-access settings are true, encryption shows aws:kms, versioning Enabled, and the scanners report no high findings.

168.4

Common problems

ProblemCauseFix
BucketAlreadyExistsBucket names are global across all AWS accounts.Choose a unique name (account/region prefix).
Unsupported argument "versioning" in aws_s3_bucketOld (v3) syntax.Use the separate resources (provider v4+).
AccessDenied writing objects with KMSThe writer has no rights on the KMS key.Grant kms:GenerateDataKey/kms:Encrypt (and Decrypt to read).
ACL errors from an old toolACLs are disabled (BucketOwnerEnforced).Update the tool to not send ACLs.
terraform destroy fails (bucket not empty)Versioned objects remain.Empty it first, or force_destroy = true only for test buckets.
Scanner flags missing access loggingNo server access logs.Add aws_s3_bucket_logging to a log bucket if required.

الأوامر في هذا الدرس

168.5

Go further

1Why are versioning and encryption separate resources in the AWS provider?

Since provider v4 each bucket setting is its own resource (aws_s3_bucket_versioning, …_server_side_encryption_configuration, …).

2How do you make S3 refuse plain HTTP requests?

A bucket policy that denies all actions when aws:SecureTransport is false.

كل الأوامر في هذا الفصل