Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After upgrading to cdk v2, getting Stack name must be <= 128 characters #20124

Closed
Alon-Katz opened this issue Apr 28, 2022 · 7 comments · Fixed by #20528
Closed

After upgrading to cdk v2, getting Stack name must be <= 128 characters #20124

Alon-Katz opened this issue Apr 28, 2022 · 7 comments · Fixed by #20528
Assignees
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort in-progress This issue is being actively worked on. p1 package/tools Related to AWS CDK Tools or CLI

Comments

@Alon-Katz
Copy link

Alon-Katz commented Apr 28, 2022

Describe the bug

I have been using cdk with version 1.147.1, and my cdk apps were successfully deployed.
After upgrading to cdk v2, I cant deploy my cdk app, getting "Stack name must be <= 128 characters" error message.

Looking at the stack name it tried to create, I cant understand what is going on, why is this name generated when I have provided a short logical id to all my resources and to the stack name itself.

I have a construct that exports an existing eks cluster (code is simplified just for example)

class EksCluster(Construct):
    def __init__(
        self,
        scope: Construct,
        stack_name: str,
        stage: str,
    ) -> None:
        super().__init__(scope, stack_name)
       self.cluster = self.import_cluster()

    def import_cluster(self) -> aws_eks.ICluster:
        return aws_eks.Cluster.from_cluster_attributes(
            self,
            id="imported-eks-cluster",
            cluster_name=cluster_name,
            kubectl_role_arn=kubectl_role_arn,
            open_id_connect_provider=open_id_provider,
            vpc=vpc,
        )

Then a simple app.py with the cdk app

app = App()
aws_lb_controller_stack = AwsLBControllerStack(
    app,
    f"aws-load-balancer-controller-{STAGE}",
    stage=STAGE,
    cluster_config=cluster_config,
    env=Environment(region=REGION, account=ACCOUNT),
)
app.synth()

And finally the stack itself, that uses the construct to use the eks cluster and create a service account for example

class AwsLBControllerStack(Stack):
    def __init__(
        self,
        scope: Construct,
        construct_id: str,
        stage: str,
        cluster_config: dict,
        **kwargs,
    ) -> None:
        super().__init__(scope, construct_id, **kwargs)
        self.eks_cluster = EksCluster(self, "eks-cluster", stage=stage)
        service_account = self.eks_cluster.cluster.add_service_account(
            "aws-lb-controller-service-account",
            name=AWS_LB_CONTROLLER_SERVICE_ACCOUNT_NAME,
            namespace=AWS_LB_CONTROLLER_NAMESPACE,
        )

This worked fine on cdk v1.147.1, but now after upgrading the cdk version and trying to deploy this code, it returns the following error:

jsii.errors.JSIIError: Stack name must be <= 128 characters. Stack name: 'awsloadbalancercontrollersandboxzolaawsloadbalancercontrollersandboxzolaeksclusterimportedeksekscluster980273B4KubectlProviderC92A5312'

Can you please explain what generated such a long name?
And most importantly, how can this be fixed?

Expected Behavior

Stack names should be short and understandable like they are defined in their logical ids, and most importantly it should allow me to deploy my cdk app without generating the name and then not allowing me to deploy when I did not define such name anywhere in the app.

Current Behavior

Stack names are generated with multiple duplicates inside and just unreadable names.

Reproduction Steps

Posted above a simple setup in python

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.21.1

Framework Version

No response

Node.js Version

v17.9.0

OS

macOS monterey 12.3.1

Language

Python

Language Version

3.10.2

Other information

No response

@Alon-Katz Alon-Katz added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 28, 2022
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Apr 28, 2022
@ryparker ryparker added the p1 label Apr 28, 2022
@rix0rrr rix0rrr added effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels May 9, 2022
@rix0rrr rix0rrr removed their assignment May 9, 2022
@michaelsatish
Copy link

I ran into the problem stated here, and am unable to move forward in completing my CDK application. Just wondering if any timeline is established for this to get resolved?

@Alon-Katz
Copy link
Author

The way I could solve this issue, was to shorten the base stack name to something unreadable (because I had to remove a lot of letters). This also created a downtime for our system because I had to delete the stack first and only then re-deploy with the shorten name.
This is unreasonable and if I knew it beforehand I would not upgrade to cdk v2 until this issue is resolved.

@michaelsatish
Copy link

@Alon-Katz Thank you for the comment. I can synthesize the stack with shorten name. No ideal, but works till this gets fixed.

@comcalvi comcalvi self-assigned this May 23, 2022
@comcalvi
Copy link
Contributor

comcalvi commented May 23, 2022

@michaelsatish how is STAGE defined in your reproduction example? I haven't quite been able to reproduce this yet, but I have been able to synthesize a stack which contains nested stacks with long logical IDs, eg;

awsloadbalancercontrollerSTAGEeksclusterimportedekscluster8787A0B1KubectlProviderNestedStackawsloadbalancercontrollerSTAGEeksclusterimportedekscluster8787A0B1KubectlProviderNestedStackResourceE69C9315

This was present in the template produced by this app. cdk synth executes successfully. This isn't what you described though; could you share a full app that reproduces this exactly?

Stack code:

from aws_cdk import (
    core as cdk,
    aws_ec2 as ec2,
    aws_eks as eks,
)

from aws_cdk import core

class EksCluster(core.Construct):
    def __init__(
        self,
        scope: core.Construct,
        stack_name: str,
        stage: str,
    ) -> None:
        super().__init__(scope, stack_name)
        self.cluster = self.import_cluster()

    def import_cluster(self) -> eks.ICluster:
        return eks.Cluster.from_cluster_attributes(
            self,
            id="imported-eks-cluster",
            cluster_name="magic-cluster",
            kubectl_role_arn="arn:aws:iam::123456789012:role/MagicServiceRole",
            open_id_connect_provider=eks.OpenIdConnectProvider(self, 'openIdProvider', url='magic-url'),
            vpc=ec2.Vpc(self, "vpc"),
        )

class AwsLBControllerStack(core.Stack):
    def __init__(
        self,
        scope: core.Construct,
        construct_id: str,
        stage: str,
        #cluster_config: dict,
        **kwargs,
    ) -> None:
        super().__init__(scope, construct_id, **kwargs)
        clust = EksCluster(scope=self, stack_name="eks-cluster", stage=stage)
        service_account = clust.cluster.add_service_account(
            "aws-lb-controller-service-account",
        )

app.py:

from aws_cdk import core

from python_reprod.python_reprod_stack import AwsLBControllerStack

STAGE = "STAGE"

app = core.App()
aws_lb_controller_stack = AwsLBControllerStack(
    app,
    f"aws-load-balancer-controller-{STAGE}",
    stage=STAGE,
    #cluster_config=cluster_config,
    env=core.Environment(region='us-east-1', account="123456789012"),
)
app.synth()

@michaelsatish
Copy link

Hi @comcalvi,

Here is an example stack code to reproduce the error. Hope this helps.

package versions
aws-cdk-lib = "==2.25.0"
constructs = "==10.1.16"

app.py

#!/usr/bin/env python3
from aws_cdk import App

from pipeline import PipelineStack

app = App()
PipelineStack(app, "ApplicationPipelineStack")
app.synth()

pipeline.py

from aws_cdk import Stack
from aws_cdk import aws_s3 as s3
from aws_cdk import pipelines
from constructs import Construct

from stages import ApplicationStage


class PipelineStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        bucket = s3.Bucket(self, "ApplicationCodeBucket")

        the_pipeline = pipelines.CodePipeline(
            self,
            "CdkPipeline",
            self_mutation=False,
            synth=pipelines.ShellStep(
                id="Synth",
                input=pipelines.CodePipelineSource.s3(bucket, "code"),
                commands=[
                    "npm install -g aws-cdk@latest",
                    "python3 -m pip install --user pipenv",
                    "pipenv install --deploy --system",
                ],
            ),
        )

        application_stage = ApplicationStage(self, "ApplicationStage")
        the_pipeline.add_stage(application_stage)

stages.py

from aws_cdk import Stage
from constructs import Construct

from stacks import ApplicationStack


class ApplicationStage(Stage):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        _ = ApplicationStack(self, "ApplicationStack")

stacks.py

from aws_cdk import Stack
from aws_cdk import aws_eks as eks
from aws_cdk import aws_iam as iam
from constructs import Construct


class ApplicationStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        cluster = eks.Cluster.from_cluster_attributes(
            self,
            "ApplicationCluster",
            cluster_name="ApplicationDevCluster",
            kubectl_role_arn="arn:aws:iam::12345:role/service-role/AmazonExample",
            open_id_connect_provider=iam.OpenIdConnectProvider(
                self, "ApplicationClusterIdentityProvider", url="http://"
            ),
        )

        service_acount = cluster.add_service_account("ApplicationServiceAccount")

        cluster.add_helm_chart(
            "ApplicationHelmChart",
            chart="nginx-ingress",
            repository="https://helm.nginx.com/stable",
            namespace="kube-system",
            values={"service_acount": service_acount.service_account_name},
        )
cdk ls

jsii.errors.JSIIError: Stack name must be <= 128 characters. Stack name: 'ApplicationStage-ApplicationStackApplicationPipelineStackApplicationStageApplicationStackApplicationClusterF3918940KubectlProvider5C2B31FB'

@comcalvi
Copy link
Contributor

Thanks @michaelsatish, that reproduces the issue. I'll be working on a fix for this asap.

@comcalvi comcalvi added the in-progress This issue is being actively worked on. label May 25, 2022
@mergify mergify bot closed this as completed in #20528 Jul 12, 2022
mergify bot pushed a commit that referenced this issue Jul 12, 2022
When imported, Kubectl Providers result in a new nested stack being created. Nested stack names are created by generating a unique ID (which includes the entire construct id path). Kubectl provider ids are constructed the same way (they can't be specified manually, so the code uses a unique ID). This results in the final ID for the stack containing the id of the provider repeated twice. The makes the name hard to decipher, and more importantly can result in stack names being longer than 128 characters.

This caps all generated stack names at 128 characters. Ideally, we would be able to change way in which these stack names are generated; however, that would be a breaking change.

Fixes #20124.

----


*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort in-progress This issue is being actively worked on. p1 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants