|
| 1 | +locals { |
| 2 | + sudoers = base64gzip(file("${path.module}/files/99-custom-sudoers")) |
| 3 | + |
| 4 | + # combine user's IAM policy arn list with what is created in this module |
| 5 | + complete_aws_iam_role_policy_attachment_list = concat(var.aws_iam_role_policy_attachment_list, |
| 6 | + [ |
| 7 | + "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM", |
| 8 | + aws_iam_policy.node_configs.arn, |
| 9 | + ]) |
| 10 | +} |
| 11 | + |
| 12 | +module "ec2_instance" { |
| 13 | + source = "terraform-aws-modules/ec2-instance/aws" |
| 14 | + version = "~> 2.0" |
| 15 | + |
| 16 | + name = var.instance_name |
| 17 | + instance_count = 1 |
| 18 | + |
| 19 | + ami = var.ami |
| 20 | + instance_type = var.instance_config.root_installer_device.instance_type |
| 21 | + key_name = var.key_pair_name != null ? var.key_pair_name: aws_key_pair.this[0].id |
| 22 | + monitoring = true |
| 23 | + vpc_security_group_ids = var.security_group_list |
| 24 | + subnet_id = var.subnet_id |
| 25 | + |
| 26 | + # instance profile created in this module for each individual node |
| 27 | + iam_instance_profile = aws_iam_instance_profile.instance_profile.id |
| 28 | + |
| 29 | + tags = var.tags |
| 30 | + |
| 31 | + enable_volume_tags = true |
| 32 | + root_block_device = [ |
| 33 | + { |
| 34 | + delete_on_termination = var.instance_config.root_installer_device.delete_on_termination |
| 35 | + encrypted = var.instance_config.root_installer_device.encrypted |
| 36 | + iops = var.instance_config.root_installer_device.volume_type == "io2" ? var.instance_config.root_installer_device.iops : null |
| 37 | + kms_key_id = var.instance_config.root_installer_device.kms_key_id |
| 38 | + volume_size = var.instance_config.root_installer_device.volume_size |
| 39 | + volume_type = var.instance_config.root_installer_device.volume_type |
| 40 | + }, |
| 41 | + ] |
| 42 | + |
| 43 | + ebs_block_device = var.instance_config.ebs_block_device |
| 44 | + |
| 45 | + user_data = templatefile("${path.module}/cloud-init/user-data.yaml.tpl", { |
| 46 | + sudoers = local.sudoers |
| 47 | + user_ssh_public_key = var.user_ssh_public_key |
| 48 | + ebs_block_device_1_is_set = var.instance_config.user_data_inputs.ebs_block_device_1_is_set |
| 49 | + ebs_block_device_1_mount_path = var.instance_config.user_data_inputs.ebs_block_device_1_mount_path |
| 50 | + ebs_block_device_2_is_set = var.instance_config.user_data_inputs.ebs_block_device_2_is_set |
| 51 | + ebs_block_device_2_mount_path = var.instance_config.user_data_inputs.ebs_block_device_2_mount_path |
| 52 | + }) |
| 53 | + |
| 54 | + depends_on = [ |
| 55 | + aws_iam_instance_profile.instance_profile, |
| 56 | + ] |
| 57 | +} |
| 58 | + |
| 59 | +resource "aws_key_pair" "this" { |
| 60 | + count = var.key_pair_name == null ? 1: 0 |
| 61 | + key_name = var.instance_name |
| 62 | + public_key = var.user_ssh_public_key |
| 63 | +} |
| 64 | + |
| 65 | +# Instance profile |
| 66 | +resource "aws_iam_instance_profile" "instance_profile" { |
| 67 | + name = var.instance_name |
| 68 | + role = aws_iam_role.instance_role.name |
| 69 | +} |
| 70 | + |
| 71 | +# Instance role |
| 72 | +resource "aws_iam_role" "instance_role" { |
| 73 | + |
| 74 | + name = var.instance_name |
| 75 | + assume_role_policy = jsonencode({ |
| 76 | + Version = "2012-10-17" |
| 77 | + Statement = [ |
| 78 | + { |
| 79 | + Action = "sts:AssumeRole" |
| 80 | + Effect = "Allow" |
| 81 | + Sid = "" |
| 82 | + Principal = { |
| 83 | + Service = "ec2.amazonaws.com" |
| 84 | + } |
| 85 | + }, |
| 86 | + ] |
| 87 | + }) |
| 88 | + description = "A role for the ${var.instance_name} node" |
| 89 | +} |
| 90 | + |
| 91 | +# Attached the list of policies to the instance profile |
| 92 | +resource "aws_iam_role_policy_attachment" "attach_policies" { |
| 93 | + count = length(concat(local.complete_aws_iam_role_policy_attachment_list)) |
| 94 | + role = aws_iam_role.instance_role.name |
| 95 | + policy_arn = local.complete_aws_iam_role_policy_attachment_list[count.index] |
| 96 | +} |
| 97 | + |
| 98 | +# Policy for S3 Bucket - allows the node to get read-only access to s3 buckets for the node_config items |
| 99 | +# For the "all" nodes |
| 100 | +resource "aws_iam_policy" "node_configs" { |
| 101 | + name = "${var.instance_name}-node-configs" |
| 102 | + policy = jsonencode({ |
| 103 | + "Version" : "2012-10-17", |
| 104 | + "Statement" : [ |
| 105 | + { |
| 106 | + "Action" : ["s3:GetObject", "s3:ListBucket"], |
| 107 | + "Effect" : "Allow", |
| 108 | + "Resource" : "arn:aws:s3:::${var.environment_name}-installer/node_configs/*" |
| 109 | + } |
| 110 | + ] |
| 111 | + }) |
| 112 | +} |
0 commit comments