main.tf 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. locals {
  2. bucket_name = "xdr-github-enterprise-${var.environment}-github-actions"
  3. accounts = [var.aws_account_id]
  4. account_arns = [for a in local.accounts : "arn:${var.aws_partition}:iam::${a}:root"]
  5. }
  6. resource "aws_s3_bucket" "bucket" {
  7. bucket = local.bucket_name
  8. acl = "private"
  9. versioning {
  10. enabled = true
  11. }
  12. tags = merge(var.standard_tags, var.tags)
  13. lifecycle_rule {
  14. id = "INTELLIGENT_TIERING"
  15. enabled = true
  16. abort_incomplete_multipart_upload_days = 2
  17. transition {
  18. days = 30
  19. storage_class = "INTELLIGENT_TIERING"
  20. }
  21. expiration {
  22. days = 60
  23. }
  24. }
  25. server_side_encryption_configuration {
  26. rule {
  27. apply_server_side_encryption_by_default {
  28. kms_master_key_id = aws_kms_key.bucketkey.arn
  29. sse_algorithm = "aws:kms"
  30. }
  31. }
  32. }
  33. }
  34. resource "aws_s3_bucket_public_access_block" "public_access_block" {
  35. bucket = aws_s3_bucket.bucket.id
  36. block_public_acls = true
  37. block_public_policy = true
  38. ignore_public_acls = true
  39. restrict_public_buckets = true
  40. # Not technically dependent, but prevents a "Conflicting conditional operation" conflict.
  41. # See https://github.com/hashicorp/terraform-provider-aws/issues/7628
  42. depends_on = [aws_s3_bucket_policy.policy]
  43. }
  44. resource "aws_s3_bucket_policy" "policy" {
  45. bucket = aws_s3_bucket.bucket.id
  46. policy = <<POLICY
  47. {
  48. "Version": "2012-10-17",
  49. "Id": "AllowThisAccount",
  50. "Statement": [
  51. {
  52. "Sid": "AccountAllow",
  53. "Effect": "Allow",
  54. "Principal": {
  55. "AWS": ${jsonencode(local.account_arns)}
  56. },
  57. "Action": [
  58. "s3:GetObject",
  59. "s3:ListBucket"
  60. ],
  61. "Resource": [
  62. "${aws_s3_bucket.bucket.arn}",
  63. "${aws_s3_bucket.bucket.arn}/*"
  64. ]
  65. }
  66. ]
  67. }
  68. POLICY
  69. }