aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.47k stars 3.83k forks source link

aws_batch_alpha: Resource names not automatically prepended with stack name #23665

Open adonig opened 1 year ago

adonig commented 1 year ago

Describe the bug

Normally the names of all resources created by the CDK get prepended with the stack name, which helps a lot identifying the stack the resources belong to. This is not the case for the constructs from the aws_batch_alpha module.

Expected Behavior

I expected all resources created in my stack to have their names prepended with the stack name somehow.

Current Behavior

Most resources have their names prepended with the stack name but the ones from aws_batch_alpha don't. See the comments in the code below for examples.

Reproduction Steps

from typing import Any, cast

from aws_cdk import Stack
from aws_cdk import aws_batch_alpha as batch
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_ecs as ecs
from aws_cdk import aws_iam as iam

from constructs import Construct

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

        # The name of this VPC is MyCdkStack/VPC
        vpc = ec2.Vpc(self, "VPC")

        # The name of this security group is MyCdkStack-BatchEnvironmentSecurityGroup<random_chars>-<random_chars>
        batch_environment_sg = ec2.SecurityGroup(self, "BatchEnvironmentSecurityGroup", vpc=vpc)

        # The name of this compute environment is BatchComputeEnvironment<random_chars>-<random_chars>
        batch_compute_environment = batch.ComputeEnvironment(
            self,
            "BatchComputeEnvironment",
            compute_resources=batch.ComputeResources(
                maxv_cpus=16,
                type=batch.ComputeResourceType.FARGATE,
                vpc=vpc,
                vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC),
                security_groups=[batch_environment_sg],
            ),
            enabled=True,
            managed=True,
        )

        # The name of this job queue is BatchJobQueue<random_chars>-<random_chars>
        batch_job_queue = batch.JobQueue(
            self,
            "BatchJobQueue",
            compute_environments=[
                batch.JobQueueComputeEnvironment(
                    compute_environment=batch_compute_environment,
                    order=1,
                )
            ],
            priority=1,
        )

        # The name of this iam role is already predefined.
        task_execution_role = iam.Role.from_role_arn(
            self,
            "TaskExecutionRole",
            f"arn:aws:iam::{self.account}:role/ecsTaskExecutionRole",
            mutable=False,
        )

        # The name of this iam role is MyCdkStack-TaskRole<random_chars>-<random_chars>
        task_role = iam.Role(self, "TaskRole", assumed_by=iam.ServicePrincipal("ecs-tasks.amazonaws.com"))

        # The name of this job definition is BatchJobDefinition<random_chars>-<random_chars>
        batch_job_definition = batch.JobDefinition(
            self,
            "BatchJobDefinition",
            container=batch.JobDefinitionContainer(
                assign_public_ip=True,
                image=ecs.EcrImage.from_registry("docker/whalesay"),
                execution_role=task_execution_role,
                job_role=cast(iam.IRole, task_role),
                vcpus=0.25,
                memory_limit_mib=512,
            ),
            platform_capabilities=[batch.PlatformCapabilities.FARGATE],
        )

Possible Solution

Automatically prepend their names with the stack name.

Additional Information/Context

No response

CDK CLI Version

2.60.0 (build 2d40d77)

Framework Version

No response

Node.js Version

v18.12.1

OS

Fedora Linux 37

Language

Python

Language Version

Python (3.11.1)

Other information

No response

peterwoodworth commented 1 year ago

Thanks for reporting this, I was able to reproduce it

I believe this is an error with CloudFormation's implementation of this service. Since we aren't generating the name in the template here, it is up to CloudFormation to generate the name. I have reached out to them internally (P79034998), will provide updates as they become available. Thanks!

adonig commented 1 year ago

Hi @peterwoodworth! Thank you for the quick response and action. It seems like this issue also applies to AWS Secrets Manager secrets. They also don't come with a stack name prefix by default. I now chose to always manually give resources a prefixed name when possible. Is there any downside to this?

peterwoodworth commented 1 year ago

Yeah there are a couple downsides to specifying the physical names of resources, see our docs here

Assigning physical names to resources has some disadvantages in AWS CloudFormation. Most importantly, any changes to deployed resources that require a resource replacement, such as changes to a resource's properties that are immutable after creation, will fail if a resource has a physical name assigned. If you end up in that state, the only solution is to delete the AWS CloudFormation stack, then deploy the AWS CDK app again. See the AWS CloudFormation documentation for details.

adonig commented 1 year ago

Thank you! That makes a lot of sense. For now I will try to manually prefix the construct id instead of declaring an explicit name in those cases where I found that the prefixing doesn't happen automatically. I'll keep you updated if I find more cases.

peterwoodworth commented 1 year ago

I have an update here,

The CloudFormation functionality provided by the Batch service was before there was a standard naming convention function. They intentionally stuck with this, but have created an action item to migrate to the new naming convention. I can't give any ETA on this, and likely won't be posting any more updates until the functionality is implemented