3

So I have this function I'm trying to declare and it works and deploys just dandy unless you uncomment the logRetention setting. If logRetention is specified the cdk deploy operation adds additional parameters to the stack. And, of course, this behavior is completely unexplained in the documentation.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-readme.html#log-group

SingletonFunction.Builder.create(this, "native-lambda-s3-fun")
                        .functionName(funcName)
                        .description("")
                        // .logRetention(RetentionDays.ONE_DAY)
                        .handler("app")
                        .timeout(Duration.seconds(300))
                        .runtime(Runtime.GO_1_X)
                        .uuid(UUID.randomUUID().toString())
                        .environment(new HashMap<String, String>(){{
                            put("FILE_KEY", "/file/key");
                            put("S3_BUCKET", junk.getBucketName());
                        }})
                        .code(Code.fromBucket(uploads, functionUploadKey(
                                "formation-examples",
                                "native-lambda-s3",
                                lambdaVersion.getValueAsString()
                        )))
                        .build();
"Parameters": {
    "lambdaVersion": {
      "Type": "String"
    },
    "AssetParametersceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40aS3BucketB030C8A8": {
      "Type": "String",
      "Description": "S3 bucket for asset \"ceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40a\""
    },
    "AssetParametersceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40aS3VersionKey6A2AABD7": {
      "Type": "String",
      "Description": "S3 key for asset version \"ceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40a\""
    },
    "AssetParametersceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40aArtifactHashEDC522F0": {
      "Type": "String",
      "Description": "Artifact hash for asset \"ceefd938ac7ea929077f2e2f4cf09b5034ebdd14799216b1281f4b28427da40a\""
    }
  },

Keynan
  • 1,338
  • 1
  • 10
  • 18

2 Answers2

2

It's a bug. They're Working On It™. So, rejoice - we can probably expect a fix sometime within the next decade.


I haven't tried it yet, but I'm guessing the workaround is to manipulate the low-level CfnLogGroup construct, since it has the authoritative retentionInDays property. The relevant high-level Log Group construct can probably be obtained from the Function via its logGroup property. Failing that, the LogGroup can be created from scratch (which will probably be a headache all on its own).

Andrey
  • 8,882
  • 10
  • 58
  • 82
1

I also encountered the problem described above. From what I can tell, we are unable to specify a log group name and thus the log group name is predictable.

My solution was to simply create a LogGroup with the same name as my Lambda function with the /aws/lambda/ prefix.

Example:

var function = new Function(
    this,
    "Thing",
    new FunctionProps
    {
        FunctionName = $"{Stack.Of(this).StackName}-Thing",
        // ...
    });

_ = new LogGroup(
    this,
    "ThingLogGroup",
    new LogGroupProps
    {
        LogGroupName = $"/aws/lambda/{function.FunctionName}",
        Retention = RetentionDays.ONE_MONTH,
    });

This does not create unnecessary "AssetParameters..." CF template parameters like the inline option does.

Note: I'm using CDK version 1.111.0 and 1.86.0 with C#

  • The solution of @Robert Hodgen works for me. I am using CDK version 1.97.0 with Python. For me, after defining the Lambda, when defining the LogGroup, I could refer to the name of the function. No need for specifying a hardcoded name within the definition Lambda. In the generated CloudFormation it uses a Ref to the Lambda Function. – Aschwin Sep 07 '21 at 12:51