main.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. acl = "private"
  43. tags = merge(var.standard_tags, var.tags)
  44. server_side_encryption_configuration {
  45. rule {
  46. apply_server_side_encryption_by_default {
  47. kms_master_key_id = module.shared_ami_key.key_arn
  48. sse_algorithm = "aws:kms"
  49. }
  50. }
  51. }
  52. }
  53. resource "aws_iam_role" "vmimport" {
  54. name = "vmimport"
  55. description = "Required role for importing AMIs from S3"
  56. assume_role_policy = <<EOF
  57. {
  58. "Version": "2012-10-17",
  59. "Statement": [
  60. {
  61. "Effect": "Allow",
  62. "Principal": { "Service": "vmie.amazonaws.com" },
  63. "Action": "sts:AssumeRole",
  64. "Condition": {
  65. "StringEquals":{
  66. "sts:Externalid": "vmimport"
  67. }
  68. }
  69. }
  70. ]
  71. }
  72. EOF
  73. }
  74. resource "aws_iam_role_policy" "vmimport" {
  75. name = "vmimport"
  76. role = aws_iam_role.vmimport.id
  77. policy = <<EOF
  78. {
  79. "Version":"2012-10-17",
  80. "Statement": [
  81. {
  82. "Sid": "AllowAccesstoImportsBucket",
  83. "Effect": "Allow",
  84. "Action": [
  85. "s3:GetBucketLocation",
  86. "s3:GetObject",
  87. "s3:GetBucketAcl",
  88. "s3:ListBucket",
  89. "s3:PutObject"
  90. ],
  91. "Resource": ${jsonencode(local.bucket_resources)}
  92. },
  93. {
  94. "Sid": "AllowAccesstodoImportExportActions",
  95. "Effect": "Allow",
  96. "Action": [
  97. "ec2:ModifySnapshotAttribute",
  98. "ec2:CopySnapshot",
  99. "ec2:RegisterImage",
  100. "ec2:Describe*"
  101. ],
  102. "Resource": "*"
  103. },
  104. {
  105. "Sid": "AllowAccesstotheKMSkey",
  106. "Effect": "Allow",
  107. "Action": [
  108. "kms:CreateGrant",
  109. "kms:Decrypt",
  110. "kms:DescribeKey",
  111. "kms:Encrypt",
  112. "kms:GenerateDataKey*",
  113. "kms:ReEncrypt*"
  114. ],
  115. "Resource": ${jsonencode(local.all_keys)}
  116. }
  117. ]
  118. }
  119. EOF
  120. }