main.tf 2.8 KB

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