2

Question

As CDK is becoming more complete and robust as the time goes, we are now replacing our Serverless code to CDK.

While developing we've noticed that to handle retention for lambda with CDK, it's creating a custom resource to handle it.

Code Snippet

const lambdaFn = new lambda.Function(this, 'FuncName', {
      functionName: `lambda-${props?.appCode}-FuncName-${props?.stage}`,
      runtime: lambda.Runtime.NODEJS_12_X,
      code: lambda.Code.asset('lambdas/FuncName'),
      handler: 'index.handler',
      logRetention: 14,
      role: lambdaRole,
      timeout: cdk.Duration.seconds(60),
    });

In our case, we're only allowing lambda having a specific name pattern in our DEVOPS chain. This means that to successfully deploy our stack we have to add the following permission to our "deployer role" :

"arn:aws:lambda:\*:${AWS::AccountId}:function:stack-${VAR}-*"

I don't know if there is other use cases where CDK is creating custom lambda so we left "stack-${VAR}-*" to prevent us from having this issue. As of today, we haven't find a way to manage it other way.

At the end of the day, both are generating CloudFormation stack. Before, we were handling it by directly configuring the log retention within the logGroup with the native attributes. (cf https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html) I trust that there is a reason for this implementation or that we're missing something.

Is there a way to configure the name of the custom ressource or is it planned to be implemented? Or better, that the retention option is taken into account with having the need of a custom lambda?

Thanks in advance for your insights!

Environment

CDK CLI Version: CDK Version: 1.57.0 (build 2ccfc50) OS: all Language: TypeScript

Apoths
  • 21
  • 2
  • In case it's useful there's a discussion on this topic here: https://github.com/aws/aws-cdk-rfcs/issues/83 – thudbutt Sep 06 '21 at 12:59

1 Answers1

0

you can look here: How to get logical ID of resource with CDK?

resource.node.default_child.overrideLogicalId("AnyStringHere")
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Serge L
  • 55
  • 1
  • 6
  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) Adding links is great, but it is much better to show how they solve the question. – Tomer Shetah Jan 11 '21 at 11:04