|
| 1 | +locals { |
| 2 | + log_prefix_computed = ( |
| 3 | + var.log_prefix != null |
| 4 | + ? replace("/${var.log_prefix}/*", "//{2,}/", "/") |
| 5 | + : "/*" |
| 6 | + ) |
| 7 | +} |
| 8 | + |
| 9 | +data "aws_iam_policy_document" "log_write" { |
| 10 | + count = var.log_bucket != null ? 1 : 0 |
| 11 | + |
| 12 | + statement { |
| 13 | + actions = ["s3:PutObject"] |
| 14 | + resources = ["arn:aws:s3:::${var.log_bucket}${local.log_prefix_computed}"] |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +resource "aws_iam_policy" "log_write" { |
| 19 | + count = var.log_bucket != null ? 1 : 0 |
| 20 | + |
| 21 | + description = "IAM policy granting write access to the logging bucket for ${var.name}" |
| 22 | + name_prefix = "${var.name}-logging-policy-" |
| 23 | + policy = data.aws_iam_policy_document.log_write[count.index].json |
| 24 | +} |
| 25 | + |
| 26 | +locals { |
| 27 | + core_iam_policies = [ |
| 28 | + "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore", |
| 29 | + "arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder" |
| 30 | + ] |
| 31 | + iam_policies = concat( |
| 32 | + local.core_iam_policies, |
| 33 | + aws_iam_policy.log_write[*].arn, |
| 34 | + var.additional_iam_policy_arns |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +data "aws_iam_policy_document" "assume" { |
| 39 | + statement { |
| 40 | + actions = ["sts:AssumeRole"] |
| 41 | + |
| 42 | + principals { |
| 43 | + type = "Service" |
| 44 | + identifiers = ["ec2.amazonaws.com"] |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +resource "aws_iam_role" "this" { |
| 50 | + assume_role_policy = data.aws_iam_policy_document.assume.json |
| 51 | + name_prefix = "${var.name}-imagebuilder-role-" |
| 52 | + |
| 53 | + tags = merge( |
| 54 | + var.tags, |
| 55 | + { |
| 56 | + Name : "${var.name}-imagebuilder-role" |
| 57 | + } |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_iam_role_policy_attachment" "this" { |
| 62 | + count = ( |
| 63 | + var.log_bucket == null |
| 64 | + ? length(local.core_iam_policies) + length(var.additional_iam_policy_arns) |
| 65 | + : length(local.core_iam_policies) + length(var.additional_iam_policy_arns) + 1 |
| 66 | + ) |
| 67 | + |
| 68 | + policy_arn = local.iam_policies[count.index] |
| 69 | + role = aws_iam_role.this.name |
| 70 | +} |
| 71 | + |
| 72 | +resource "aws_iam_instance_profile" "this" { |
| 73 | + name_prefix = "${var.name}-imagebuilder-instance-profile-" |
| 74 | + role = aws_iam_role.this.name |
| 75 | +} |
| 76 | + |
| 77 | +resource "aws_cloudformation_stack" "this" { |
| 78 | + name = var.name |
| 79 | + on_failure = "ROLLBACK" |
| 80 | + timeout_in_minutes = var.cloudformation_timeout |
| 81 | + |
| 82 | + tags = merge( |
| 83 | + var.tags, |
| 84 | + { Name : "${var.name}-stack" } |
| 85 | + ) |
| 86 | + |
| 87 | + template_body = templatefile("${path.module}/cloudformation.yml.tpl", { |
| 88 | + description = var.description |
| 89 | + instance_profile = aws_iam_instance_profile.this.name |
| 90 | + instance_types = var.instance_types |
| 91 | + key_pair = var.key_pair |
| 92 | + license_config_arns = var.license_config_arns |
| 93 | + log_bucket = var.log_bucket |
| 94 | + log_prefix = var.log_prefix |
| 95 | + name = var.name |
| 96 | + recipe_arn = var.recipe_arn |
| 97 | + regions = var.regions |
| 98 | + schedule = var.schedule |
| 99 | + security_group_ids = var.security_group_ids |
| 100 | + shared_account_ids = var.shared_account_ids |
| 101 | + sns_topic_arn = var.sns_topic_arn |
| 102 | + status = var.enabled ? "ENABLED" : "DISABLED" |
| 103 | + subnet = var.subnet |
| 104 | + terminate_on_failure = var.terminate_on_failure |
| 105 | + test_config = var.test_config |
| 106 | + |
| 107 | + tags = merge( |
| 108 | + var.tags, |
| 109 | + { Name : var.name } |
| 110 | + ) |
| 111 | + }) |
| 112 | +} |
0 commit comments