s3.tf 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. resource "aws_s3_bucket" "bucket" {
  2. bucket = "xdr-threatq-lambda-${var.environment}"
  3. force_destroy = true
  4. acl = "private"
  5. server_side_encryption_configuration {
  6. rule {
  7. apply_server_side_encryption_by_default {
  8. kms_master_key_id = aws_kms_key.key.arn
  9. sse_algorithm = "aws:kms"
  10. }
  11. }
  12. }
  13. }
  14. resource "aws_s3_bucket_public_access_block" "public_access_block" {
  15. bucket = aws_s3_bucket.bucket.id
  16. block_public_acls = true
  17. block_public_policy = true
  18. ignore_public_acls = true
  19. restrict_public_buckets = true
  20. # Not technically dependent, but prevents a "Conflicting conditional operation" conflict.
  21. # See https://github.com/hashicorp/terraform-provider-aws/issues/7628
  22. depends_on = [aws_s3_bucket_policy.policy]
  23. }
  24. data "aws_iam_policy_document" "s3_policy_document" {
  25. statement {
  26. sid = "AllowS3Access"
  27. actions = [ "s3:GetObject", "s3:GetObjectVersion" ]
  28. effect = "Allow"
  29. resources = [
  30. "${aws_s3_bucket.bucket.arn}",
  31. "${aws_s3_bucket.bucket.arn}/*"
  32. ]
  33. principals {
  34. type = "AWS"
  35. identifiers = [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:root" ]
  36. }
  37. }
  38. }
  39. resource "aws_s3_bucket_policy" "policy" {
  40. bucket = aws_s3_bucket.bucket.id
  41. policy = data.aws_iam_policy_document.s3_policy_document.json
  42. }
  43. resource "aws_kms_key" "key" {
  44. description = "Encryption of lambda code in S3"
  45. policy = data.aws_iam_policy_document.kms_policy_document.json
  46. enable_key_rotation = true
  47. tags = merge(var.standard_tags, var.tags)
  48. }
  49. data "aws_iam_policy_document" "kms_policy_document" {
  50. statement {
  51. sid = "AllowServices"
  52. effect = "Allow"
  53. principals {
  54. type = "AWS"
  55. identifiers = [
  56. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  57. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin"
  58. ]
  59. }
  60. actions = [ "kms:*" ]
  61. resources = [ "*" ]
  62. }
  63. # allow account to modify/manage key
  64. statement {
  65. sid = "AllowThisAccount"
  66. effect = "Allow"
  67. principals {
  68. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  69. type = "AWS"
  70. }
  71. actions = [
  72. "kms:*"
  73. ]
  74. resources = ["*"]
  75. }
  76. }
  77. resource "aws_kms_alias" "key_alias" {
  78. name = "alias/threatq-lambda-s3-key"
  79. target_key_id = aws_kms_key.key.key_id
  80. }
  81. #upload the initial code as a placeholder
  82. resource "aws_s3_bucket_object" "object" {
  83. bucket = aws_s3_bucket.bucket.id
  84. key = "code.zip"
  85. source = "code.zip"
  86. etag = filemd5("code.zip")
  87. }