AWS CDK - circular dependency when adding IAM Profile to Launch Template

Viewed 684

I am trying to build LaunchTemplate using AWS CDK (I want to use it with ASG later) and I need to specify iam_instance_profile. Unfortunately, when I do so it crashes with circular dependency.

My first attempt was to pick the role that gets built by add_auto_scaling_group_capacity method (it constructs ASG with LaunchConfiguration instead of LaunchTemplate so I can't use it for spot fleet):

instance_

profile = nodegroup_resource.node.find_child("InstanceProfile")
launch_template = aws_ec2.CfnLaunchTemplate(
            self.cluster, "launch_template",
            launch_template_data = aws_ec2.CfnLaunchTemplate.LaunchTemplateDataProperty(
                (...)
                # circural dependency
                iam_instance_profile=instance_profile
            )
        )

This fails with

Error: Resolution error: Resolution error: Unable to resolve object tree with circular reference. Path: /Resources/${Token[eks-staging.Staging.launch_template.LogicalID.1848]}/Properties/launchTemplateData/iamInstanceProfile/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host/node/host..
    at resolve (/tmp/jsii-kernel-JNb7BY/node_modules/@aws-cdk/core/lib/private/resolve.js:35:15)
    at Object.resolve (/tmp/jsii-kernel-JNb7BY/node_modules/@aws-cdk/core/lib/private/resolve.js:29:33)
    at resolve (/tmp/jsii-kernel-JNb7BY/node_modules/@aws-cdk/core/lib/private/resolve.js:118:43)

So I tried to skip adding profile on construction, explicitly adding dependency and then adding profile:

launch_template = aws_ec2.CfnLaunchTemplate(
            self.cluster, "launch_template",
            launch_template_data = aws_ec2.CfnLaunchTemplate.LaunchTemplateDataProperty(
                (...)
                # circural dependency
                # iam_instance_profile=instance_profile
            )
        )
launch_template.add_depends_on(instance_profile)
launch_template.add_property_override('iam_instance_profile', instance_profile)

This also fails but with slightly different error:

RangeError: Resolution error: Maximum call stack size exceeded.
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:1:1)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)
    at deepMerge (/tmp/jsii-kernel-QQgBdo/node_modules/@aws-cdk/core/lib/cfn-resource.js:409:17)

I thought it could be some problem with the way InstanceProfile was built in the first place so I tried to build another object with the same role attached:

role = nodegroup_resource.node.find_child('InstanceRole')
instance_profile = aws_iam.CfnInstanceProfile(self.cluster, "instance_profile", roles=[role.role_arn])

Unfortunately, it didn't help and errors are the same.

How can I solve it? Or at least how to debug the exact issue?

0 Answers
Related