skip to Main Content

I’m using AWS CDK to create Infrastructure and Code but when I deployed my stacks the resource names were too long. For example, when one stack was deployed the name was something like this:

subnet-01b6cba6915f51b9d / NetworkStack/vpc/devvpc/compute-subnet-priv-a-devSubnet1

I’d like to have the last part of the name, because is very difficult to read and is horrible. That happens with the logical names too.

Does someone know how to prevent or fix this? Thanks.

Psdt: I’m using TS

A solution in the config files or a level code using AWS CDK

2

Answers


  1. Indeed, those names that can be obtained through the CDK can be somewhat complicated, I will tell you something that you could validate, I don’t know if you have already done it but these names can be customized.

    Let’s remember that CloudFormation is present in the background, what the documentation says and I saw in some cases is that a physical name can be indicated for when these resources are created, in the case of Python it could look something like this.

    bucket = s3.Bucket(self, "MyBucket", bucket_name="my-bucket-name")
    

    On my side I don’t always do it, but of course if we want an implementation aligned with specific standards and nomenclatures it will be necessary, I saw this question in AWS re:Post I share the reference so you can take a look too.

    That said, if you ask me why those random names, it is ultimately what CloudFormation does when it is not told exactly, this is backed up with the corresponding documentation, regards.

    Login or Signup to reply.
  2. The name is comprised of the logical ID and a hash generated by CloudFormation. The logical ID is calculated by concatenating the construct IDs in the path (all the construct IDs starting from the top Stack down to the resource).

    You can provide your own physical name, but this is discouraged. From the best practices doc:

    Use generated resource names, not physical names

    Names are a precious resource. Each name can only be used once. Therefore, if you hardcode a table name or bucket name into your infrastructure and application, you can’t deploy that piece of infrastructure twice in the same account. (The name we’re talking about here is the name specified by, for example, the bucketName property on an Amazon S3 bucket construct.)

    What’s worse, you can’t make changes to the resource that require it to be replaced. If a property can only be set at resource creation, such as the KeySchema of an Amazon DynamoDB table, then that property is immutable. Changing this property requires a new resource. However, the new resource must have the same name in order to be a true replacement. But it can’t have the same name while the existing resource is still using that name.

    A better approach is to specify as few names as possible. If you omit resource names, the AWS CDK will generate them for you in a way that won’t cause problems. Suppose you have a table as a resource. You can then pass the generated table name as an environment variable into your AWS Lambda function. In your AWS CDK application, you can reference the table name as table.tableName. Alternatively, you can generate a configuration file on your Amazon EC2 instance on startup, or write the actual table name to the AWS Systems Manager Parameter Store so your application can read it from there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search