main.tf 2.9 KB

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