Skip to content

lambda@edge functions defaulting to aws/lambda/undefined log group

0

I'm creating lambda@edge functions via the CDK, and each of these functions is defaulting to a log group of /aws/lambda/undefined.

From the documentation it seems like the log group should be automatically created with the {functionName}, but I do not want to set that explicitly and allow CDK to figure that out.

Here's the code I'm using to create my edge functions:

export enum EdgeFunctionName {
  HTTP_HEADERS = 'http-headers',
  SSO_AUTH = 'sso-auth',
  SSO_CALLBACK = 'sso-callback',
  GATEWAY = 'gateway',
}

const EDGE_FUNCTIONS: EdgeFunctionConfig[] = [
  { name: EdgeFunctionName.HTTP_HEADERS, eventType: LambdaEdgeEventType.VIEWER_REQUEST },
  { name: EdgeFunctionName.SSO_AUTH, eventType: LambdaEdgeEventType.VIEWER_REQUEST },
  { name: EdgeFunctionName.SSO_CALLBACK, eventType: LambdaEdgeEventType.VIEWER_REQUEST },
  { name: EdgeFunctionName.GATEWAY, eventType: LambdaEdgeEventType.ORIGIN_REQUEST, includeBody: true },
];

export const createEdgeFunctions = (
  scope: Construct,
  stage: Stage,
  region: Region,
  env: DeploymentEnvironment,
): Map<EdgeFunctionName, EdgeLambda> => {
  const lambdaRole = createLambdaEdgeRole(scope, env, region, stage);
  return new Map(
    EDGE_FUNCTIONS.map((edgeFunction: EdgeFunctionConfig) => [
      edgeFunction.name as EdgeFunctionName,
      {
        functionVersion: new experimental.EdgeFunction(
          scope,
          `EdgeFunction${edgeFunction.name}-${stage}-$region}`,
          getEdgeFunctionOptions(edgeFunction.name, lambdaRole),
        ).currentVersion,
        eventType: edgeFunction.eventType,
        includeBody: edgeFunction.includeBody,
      },
    ]),
  );
};

export const getEdgeFunctionOptions = (functionName: string, lambdaEdgeRole: Role): experimental.EdgeFunctionProps => {
  return {
    runtime: Runtime.NODEJS_18_X,
    handler: 'index.handler',
    code: Code.fromAsset(`dist/lambda-edge/${functionName}/index.zip`),
    role: lambdaEdgeRole,
    timeout: Duration.seconds(5),
  };
};

Only when I pass in functionName in the edge function options does the log group name get generated correctly, but it's my understanding that CDK should generate a name for the edge function based on the ID set in the EdgeFunction constructor and the log group should be created with that same name.

1 Answer
0

The behavior you're experiencing with Lambda@Edge functions and their log groups is actually expected and different from regular Lambda functions. For Lambda@Edge functions, the log groups are created in the region where the function is executed, which can be any of the CloudFront edge locations around the world.

When you create a Lambda@Edge function, the log group name includes the region code where the function is executed. This is why you're seeing the log group name as /aws/lambda/us-east-1.<function-name> instead of just /aws/lambda/<function-name>.

The "undefined" log group name you're seeing (/aws/lambda/undefined) is likely because the function hasn't been executed yet in any edge location. Once the function is triggered and executed at an edge location, you should see log groups created with the appropriate region codes.

If you want to have more control over the log group name or its properties, you can create a custom log group and specify it in the logGroup property of the EdgeFunctionProps. However, keep in mind that Lambda@Edge functions will still create region-specific log groups when executed in different edge locations.

To resolve the "undefined" issue and ensure proper naming, you can try the following:

  1. Make sure you're giving a unique and descriptive ID to each EdgeFunction in your CDK code.
  2. You may need to trigger the Lambda@Edge function (by accessing your CloudFront distribution) to see the actual log groups created in various regions.
  3. If you want to aggregate logs from all regions, you can look into using CloudWatch Logs Insights to query across multiple log groups.

Remember that the behavior for Lambda@Edge functions differs from regular Lambda functions in terms of log group creation and naming due to their distributed nature across edge locations.
Sources
default cloudwatch loggroup name contains undesired region code. | AWS re:Post
interface EdgeFunctionProps · AWS CDK

answered a year ago

  • The issue isn't that it's including the region code in the log group name, it's that the log group name is aws/lambda/undefined.

    I'm giving a unique ID to each edge function (EdgeFunction${edgeFunction.name}-${stage}-$region}), and the log group is still aws/lambda/undefined even after the edge functions are triggered.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.