main.tf 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. locals {
  2. account_arns = [
  3. for account in var.account_list:
  4. "arn:${var.aws_partition}:iam::${account}:root"
  5. ]
  6. terraformer_arns = [
  7. for account in var.account_list:
  8. "arn:${var.aws_partition}:iam::${account}:role/user/mdr_terraformer"
  9. ]
  10. all_keys = concat([ module.shared_ami_key.key_arn ], var.vmimport_extra_keys)
  11. buckets = [
  12. for bucket in concat([ aws_s3_bucket.xdr-shared-amis.arn ], var.vmimport_extra_buckets):
  13. bucket
  14. ]
  15. bucket_contents = [
  16. for bucket in concat([ aws_s3_bucket.xdr-shared-amis.arn ], var.vmimport_extra_buckets):
  17. "${bucket}/*"
  18. ]
  19. bucket_resources = concat(local.buckets, local.bucket_contents)
  20. }
  21. output other {
  22. value = local.account_arns
  23. }
  24. module "shared_ami_key" {
  25. source = "../../submodules/kms/ami-key"
  26. name = "shared_ami_key"
  27. alias = "alias/shared_ami_key"
  28. description = "Key for encrypting the AMIs to be shared with other accounts."
  29. tags = merge(var.standard_tags, var.tags)
  30. key_admin_arns = [ ]
  31. key_user_arns = [ ]
  32. #key_attacher_arns = local.account_arns
  33. key_attacher_arns = local.terraformer_arns
  34. #key_attacher_arns = [ ]
  35. standard_tags = var.standard_tags
  36. aws_account_id = var.aws_account_id
  37. aws_partition = var.aws_partition
  38. remote_account_arns = local.account_arns
  39. }
  40. resource "aws_s3_bucket" "xdr-shared-amis" {
  41. bucket = var.ami_bucket_name
  42. tags = merge(var.standard_tags, var.tags)
  43. }
  44. resource "aws_s3_bucket_acl" "s3_acl_xdr-shared-amis" {
  45. bucket = aws_s3_bucket.xdr-shared-amis.id
  46. acl = "private"
  47. }
  48. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_xdr-shared-amis" {
  49. bucket = aws_s3_bucket.xdr-shared-amis.id
  50. rule {
  51. apply_server_side_encryption_by_default {
  52. kms_master_key_id = module.shared_ami_key.key_arn
  53. sse_algorithm = "aws:kms"
  54. }
  55. }
  56. }
  57. resource "aws_iam_role" "vmimport" {
  58. name = "vmimport"
  59. description = "Required role for importing AMIs from S3"
  60. assume_role_policy = <<EOF
  61. {
  62. "Version": "2012-10-17",
  63. "Statement": [
  64. {
  65. "Effect": "Allow",
  66. "Principal": { "Service": "vmie.amazonaws.com" },
  67. "Action": "sts:AssumeRole",
  68. "Condition": {
  69. "StringEquals":{
  70. "sts:Externalid": "vmimport"
  71. }
  72. }
  73. }
  74. ]
  75. }
  76. EOF
  77. }
  78. resource "aws_iam_role_policy" "vmimport" {
  79. name = "vmimport"
  80. role = aws_iam_role.vmimport.id
  81. policy = <<EOF
  82. {
  83. "Version":"2012-10-17",
  84. "Statement": [
  85. {
  86. "Sid": "AllowAccesstoImportsBucket",
  87. "Effect": "Allow",
  88. "Action": [
  89. "s3:GetBucketLocation",
  90. "s3:GetObject",
  91. "s3:GetBucketAcl",
  92. "s3:ListBucket",
  93. "s3:PutObject"
  94. ],
  95. "Resource": ${jsonencode(local.bucket_resources)}
  96. },
  97. {
  98. "Sid": "AllowAccesstodoImportExportActions",
  99. "Effect": "Allow",
  100. "Action": [
  101. "ec2:ModifySnapshotAttribute",
  102. "ec2:CopySnapshot",
  103. "ec2:RegisterImage",
  104. "ec2:Describe*"
  105. ],
  106. "Resource": "*"
  107. },
  108. {
  109. "Sid": "AllowAccesstotheKMSkey",
  110. "Effect": "Allow",
  111. "Action": [
  112. "kms:CreateGrant",
  113. "kms:Decrypt",
  114. "kms:DescribeKey",
  115. "kms:Encrypt",
  116. "kms:GenerateDataKey*",
  117. "kms:ReEncrypt*"
  118. ],
  119. "Resource": ${jsonencode(local.all_keys)}
  120. }
  121. ]
  122. }
  123. EOF
  124. }
  125. //AWS Provider outdated arguments <4.4.0
  126. /*resource "aws_s3_bucket" "xdr-shared-amis" {
  127. bucket = var.ami_bucket_name
  128. acl = "private"
  129. tags = merge(var.standard_tags, var.tags)
  130. server_side_encryption_configuration {
  131. rule {
  132. apply_server_side_encryption_by_default {
  133. kms_master_key_id = module.shared_ami_key.key_arn
  134. sse_algorithm = "aws:kms"
  135. }
  136. }
  137. }
  138. }
  139. */