Advertisement
Guest User

Encryption root block device with explicit customer-managed KMS CMK using launch template

a guest
Dec 22nd, 2020
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. variable "unique_example_string" {
  2. type = string
  3. default = "mytest123"
  4. description = "Unique string to avoid resource naming conflicts in order to allow the example to function"
  5. }
  6.  
  7. resource "aws_vpc" "vpc" {
  8. cidr_block = "192.168.0.0/22"
  9. }
  10.  
  11. data "aws_availability_zones" "azs" {
  12. state = "available"
  13. }
  14.  
  15. resource "aws_subnet" "subnet_az1" {
  16. availability_zone = data.aws_availability_zones.azs.names[0]
  17. cidr_block = "192.168.0.0/24"
  18. vpc_id = aws_vpc.vpc.id
  19. }
  20.  
  21. resource "aws_security_group" "sg" {
  22. name_prefix = var.unique_example_string
  23. vpc_id = aws_vpc.vpc.id
  24. }
  25.  
  26. data "aws_ami" "ami" {
  27. most_recent = true
  28. owners = ["amazon"]
  29. filter {
  30. name = "name"
  31. values = ["amzn2-ami-ecs-hvm-2.0.*"]
  32. }
  33. filter {
  34. name = "root-device-type"
  35. values = ["ebs"]
  36. }
  37. filter {
  38. name = "virtualization-type"
  39. values = ["hvm"]
  40. }
  41. filter {
  42. name = "architecture"
  43. values = ["x86_64"]
  44. }
  45. }
  46.  
  47. data "aws_region" "current" {}
  48.  
  49. data "aws_caller_identity" "current" {}
  50.  
  51. # Policy copied and adapted from alias/aws/ebs KMS key.
  52. data "aws_iam_policy_document" "kms_policy" {
  53. statement {
  54. sid = "Allow access through EBS for all principals in the account that are authorized to use EBS"
  55. effect = "Allow"
  56. principals {
  57. type = "AWS"
  58. identifiers = ["*"]
  59. }
  60. actions = [
  61. # Tried both with and without kms:* action (no change in result).
  62. "kms:Encrypt",
  63. "kms:Decrypt",
  64. "kms:ReEncrypt*",
  65. "kms:GenerateDataKey*",
  66. "kms:CreateGrant",
  67. "kms:DescribeKey"
  68. ]
  69. # Tried both with and without conditions (no change in result).
  70. condition {
  71. test = "StringEquals"
  72. variable = "kms:CallerAccount"
  73. values = [data.aws_caller_identity.current.account_id]
  74. }
  75. condition {
  76. test = "StringEquals"
  77. variable = "kms:ViaService"
  78. values = ["ec2.${data.aws_region.current.name}.amazonaws.com"]
  79. }
  80. }
  81. statement {
  82. sid = "Enable IAM User Permissions"
  83. effect = "Allow"
  84. principals {
  85. type = "AWS"
  86. identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
  87. }
  88. actions = ["kms:*"]
  89. resources = ["*"]
  90. }
  91. }
  92.  
  93. resource "aws_kms_key" "kms" {
  94. description = var.unique_example_string
  95. # Tried both with and without an explicit policy (no change in result).
  96. policy = data.aws_iam_policy_document.kms_policy.json
  97. }
  98.  
  99. resource "aws_launch_template" "example" {
  100. name_prefix = var.unique_example_string
  101. ebs_optimized = true
  102. image_id = data.aws_ami.ami.image_id
  103. instance_type = "t3.nano"
  104. vpc_security_group_ids = [aws_security_group.sg.id]
  105.  
  106. block_device_mappings {
  107. device_name = data.aws_ami.ami.root_device_name
  108. ebs {
  109. delete_on_termination = true
  110. encrypted = true
  111. volume_type = "gp2"
  112. volume_size = 30
  113. # Tried both with and without kms_key_id (only works WITHOUT kms_key_id or when pointing to alias/aws/ebs).
  114. #kms_key_id = aws_kms_key.kms.arn
  115. }
  116. }
  117. }
  118.  
  119. resource "aws_autoscaling_group" "example" {
  120. name_prefix = var.unique_example_string
  121. vpc_zone_identifier = [aws_subnet.subnet_az1.id]
  122. min_size = 1
  123. max_size = 1
  124.  
  125. launch_template {
  126. id = aws_launch_template.example.id
  127. version = "$Latest"
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement