From 3d9576f560af26450cf2b2dda156b059140df46d Mon Sep 17 00:00:00 2001 From: "ZOI\\graham.geary" Date: Thu, 17 Dec 2020 11:15:16 +0100 Subject: [PATCH] #2 dynamic instance profile --- ec2.tf | 1 + iam_instance_profile.tf | 5 +++-- locals.tf | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 locals.tf diff --git a/ec2.tf b/ec2.tf index c6f98d0..6f3b89b 100644 --- a/ec2.tf +++ b/ec2.tf @@ -5,6 +5,7 @@ resource "aws_instance" "instance" { subnet_id = var.subnet_id vpc_security_group_ids = var.vpc_security_group_ids user_data = var.user_data + iam_instance_profile = local.iam_instance_profile # Modifying any of the root_block_device settings other than volume_size requires resource replacement dynamic "root_block_device" { diff --git a/iam_instance_profile.tf b/iam_instance_profile.tf index 41ea9f1..915d1f5 100644 --- a/iam_instance_profile.tf +++ b/iam_instance_profile.tf @@ -1,4 +1,5 @@ resource "aws_iam_instance_profile" "profile" { - name = var.iam_instance_profile_name - role = var.iam_role_name + count = local.create_iam_instance_profile + name = var.iam_instance_profile_name + role = var.iam_role_name } \ No newline at end of file diff --git a/locals.tf b/locals.tf new file mode 100644 index 0000000..7955790 --- /dev/null +++ b/locals.tf @@ -0,0 +1,4 @@ +locals { + create_iam_instance_profile = var.iam_instance_profile_name == "" ? 0 : 1 + iam_instance_profile = var.iam_instance_profile_name == "" ? "" : aws_iam_instance_profile[0].profile.name +} \ No newline at end of file