s3.tf 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. resource "aws_s3_bucket" "bucket" {
  2. bucket = local.fullname
  3. tags = merge(var.standard_tags, var.tags)
  4. }
  5. resource "aws_s3_bucket_versioning" "s3_version_bucket" {
  6. bucket = aws_s3_bucket.bucket.id
  7. versioning_configuration {
  8. status = "Enabled"
  9. }
  10. }
  11. resource "aws_s3_bucket_acl" "s3_acl_bucket" {
  12. bucket = aws_s3_bucket.bucket.id
  13. acl = "private"
  14. }
  15. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_bucket" {
  16. bucket = aws_s3_bucket.bucket.id
  17. rule {
  18. apply_server_side_encryption_by_default {
  19. kms_master_key_id = aws_kms_key.bucketkey.arn
  20. sse_algorithm = "aws:kms"
  21. }
  22. }
  23. }
  24. resource "aws_s3_bucket_lifecycle_configuration" "s3_lifecyle_bucket" {
  25. bucket = aws_s3_bucket.bucket.id
  26. rule {
  27. id = "INTELLIGENT_TIERING"
  28. status = "Enabled"
  29. filter {} # Required for noncurrent_version_expiration to work
  30. abort_incomplete_multipart_upload {
  31. days_after_initiation = 2
  32. }
  33. transition {
  34. days = 30
  35. storage_class = "INTELLIGENT_TIERING"
  36. }
  37. noncurrent_version_expiration {
  38. # We always keep the current version and the previous version, and delete any other versions after 90 days
  39. newer_noncurrent_versions = 2
  40. noncurrent_days = 90
  41. }
  42. }
  43. }
  44. resource "aws_s3_bucket_public_access_block" "public_access_block" {
  45. bucket = aws_s3_bucket.bucket.id
  46. block_public_acls = true
  47. block_public_policy = true
  48. ignore_public_acls = true
  49. restrict_public_buckets = true
  50. # Not technically dependent, but prevents a "Conflicting conditional operation" conflict.
  51. # See https://github.com/hashicorp/terraform-provider-aws/issues/7628
  52. depends_on = [aws_s3_bucket_policy.s3_bucket_policy]
  53. }
  54. data "aws_iam_policy_document" "s3_bucket_policy" {
  55. statement {
  56. sid = "AccountAllow"
  57. effect = "Allow"
  58. resources = [
  59. aws_s3_bucket.bucket.arn,
  60. "${aws_s3_bucket.bucket.arn}/*",
  61. ]
  62. actions = [
  63. "s3:GetObject",
  64. "s3:ListBucket",
  65. ]
  66. principals {
  67. type = "AWS"
  68. identifiers = local.principals
  69. }
  70. }
  71. }
  72. resource "aws_s3_bucket_policy" "s3_bucket_policy" {
  73. depends_on = [ aws_iam_role.role ] # 2022-04-22: FTD: Copied this across, but not sure why this dependency.
  74. bucket = aws_s3_bucket.bucket.id
  75. policy = data.aws_iam_policy_document.s3_bucket_policy.json
  76. }