Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 994
Laimonas Sutkus Avatar asked Jan 16 '20 13:01

Laimonas Sutkus


People also ask

Is AWS CDK worth it?

Overall I think if you have a fairly small team that is owning the application, the infrastructure code of an application, and the production deployment of that application, then CDK is worth strongly considering (I'd use it myself).

How does CDK generate logical ID?

Resources have paths that are globally unique (the names of all scopes from the stack to a specific resource). Therefore, the AWS CDK generates the necessary unique identifiers by concatenating the elements of the path and adding an 8-digit hash.

Does CDK deploy build?

AWS provides useful resource templates called Constructs, which you can build and share across your organization. CDK doesn't create your resources directly, it compiles it down to CloudFormation which then deploys to AWS.


2 Answers

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.

like image 68
Mykola Korol Avatar answered Oct 08 '22 12:10

Mykola Korol


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

like image 24
Cristophs0n Avatar answered Oct 08 '22 11:10

Cristophs0n