cloudposse / cloudposse/terraform-aws-ec2-instance

unable to pass instance_profile dynamically as the data block of instance profile is blocking

Open
#213 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
HCL
Stars
169
Forks
216
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

unable to pass instance profile dynamically from other module as the below data block is looking for existing resource in aws.

data "aws_iam_instance_profile" "given" {
count = local.enabled && var.instance_profile_enabled && var.instance_profile != "" ? 1 : 0
name = var.instance_profile
}

### Expected Behavior

Code should facilitate to pass instance profile externally from other module also, however data block looks for already existing resource in aws

data block is not required as it is not allowing to pass the instance profile dynamically from another module

### Steps to Reproduce

module "ec2_instance" {
source = "cloudposse/ec2-instance/aws"
version = "1.6.1"
namespace = var.namespace
stage = var.stage
name = var.name
enabled = var.enabled
ami = var.ami
instance_type = var.instance_type
ssh_key_pair = var.ssh_key_pair
vpc_id = var.vpc_id
subnet = var.subnet
availability_zone = var.availability_zone
security_group_enabled = false
security_groups = var.security_groups
user_data = var.user_data
user_data_base64 = var.user_data_base64
user_data_replace_on_change = var.user_data_replace_on_change
delete_on_termination = var.delete_on_termination
disable_api_stop = var.disable_api_stop
disable_api_termination = var.disable_api_termination
monitoring = var.monitoring
secondary_private_ips = var.external_network_interface_enabled ? null : var.secondary_private_ips
instance_profile_enabled = var.instance_profile_enabled
instance_profile = local.instance_profile
}
locals {
instance_profile = var.instance_profile_enabled && var.custom_instance_profile != "" ? var.custom_instance_profile : (var.instance_profile_enabled ? "BURoleForEC2" : "")
instance_profile_count = var.enabled && var.instance_profile_enabled && var.custom_instance_profile == "" ? 1 : 0
}

### Screenshots

Error: reading IAM Instance Profile (BURoleForEC2test): couldn't find resource

with module.test_ec2_1.module.ec2_instance.data.aws_iam_instance_profile.given[0],
on .terraform/modules/test_ec2_1.ec2_instance/main.tf line 84, in data "aws_iam_instance_profile" "given":
84: data "aws_iam_instance_profile" "given" {

### Environment

_No response_

### Additional Context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the data.aws_iam_instance_profile.given block in main.tf at line 84 and reproduce the failure using the module configuration shown in the issue. Trace how instance_profile is consumed when supplied from another module; done means an externally supplied profile can be used without an erroneous lookup for an existing AWS resource.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, terraform
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.