Laimonas Sutkus
Laimonas Sutkus

Reputation: 3627

AWS CDK generated resource identifiers are horrible and not readable. Any way to fix this?

Anyone, that has used AWS CDK suffers from horrible resource identifiers.

Examples of Stacks/Nested Stacks names:

enter image description here

Or examples of resource names:

enter image description here

These identifiers are horrible to read. Is there any work-around to override these identifiers?

I have tried to set ids / names / identifiers / alies of the resources. However it seems that cdk or cloudformation itself is generating these strings.

Thank you for suggestions!

Upvotes: 29

Views: 10623

Answers (2)

Cristophs0n
Cristophs0n

Reputation: 1266

Yeah, this is a good question. There's two types of IDs here:

  • Logical ID
  • Physical ID

Typically the Physical ID can be specified as a property on the resources. For instance, if you are using the CDK, you can set the functionName property when creating your Lambda (as below).

The Logical ID is also added when creating the resource and as you mentioned, however, the Logical ID is derived from a combination of what you specify and where it is within your stack. So, for example, if you have a stack that uses constructs, then this ID will be prefixed with the construct's Logical ID as well... and it's definitely not very readable.

I'd be very careful changing these IDs, especially if you have already deployed the stack, but if you really want to override them then you could do something like this in the CDK (TypeScript):

import {
  CfnResource,
} from "@aws-cdk/core";
import {
  Function,
  Runtime,
  Code,
} from "@aws-cdk/aws-lambda";

const consumerLambda = new Function(this, 'LogicalIdOnResource', {
  runtime: Runtime.NODEJS_12_X,
  handler: 'index.handler',
  code: Code.fromAsset(path.join(__dirname, 'lambda-handler')),
  functionName: 'ds-di-kafka-consumer-lambda' // PhysicalIdOnResource
});

// Override Logical ID
(consumerLambda.node.defaultChild as CfnResource).overrideLogicalId(
  'Consumer'
);

Which looks like this on CloudFormation:

enter image description here

Upvotes: 4

Mykola Korol
Mykola Korol

Reputation: 743

All of resources(or at least for most that I know) could be named manually.

For AWS::EC2::SecurityGroup that would be Properties -> GroupName

AWS::CloudWatch::Alarm - Properties -> AlarmName

AWS::Lambda::Function - Properties -> FunctionName etc.

But for some of them that would lead to consequences - you won't be able to update some of them, because they might need recreation (and the name is already occupied). So in general it's not a good practice. And obviously you won't be able to create a full env duplicate not changing some parameter for the generated name like this:

FunctionName: !Sub '${InstanceName}-your-resourse-constant-name-${Environment}'

If you don't specify the naming it would create a name like this:

${stackName}-${resourceNameInCF}-${someHashCode}, but in your case it seems you have nested stacks and it becomes pretty unreadable, especially with long names because of the names chaining.

Upvotes: 6

Related Questions