aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[aws-eks] Enable Control Plane logs in EKS cluster

Open stefanolczak opened this issue 7 years ago • 16 comments

Use Case

Enabling Control Plane logging in EKS cluster is only possible by calling EKS API after cluster is created. Doing it in CDK requires to create Custom Resource with code that calls the API. It would be nice to have it as an argument for creating EKS cluster from CDK.

Proposed Solution

Since EKS is created from python lambda when kubectlEnabled flag is enabled there is a simple way to create the EKS cluster with logging enabled. Currently the lambda code uses boto3 method eks.create_cluster() where we can pass arguments to enable logging on created cluster. (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/eks.html#EKS.Client.create_cluster). The lambda uses config as an argument for this method :

https://github.com/aws/aws-cdk/blob/c3b3c935b2086288dd79f559a49c3b5304a8e40f/packages/%40aws-cdk/aws-eks/lib/cluster-resource/index.py#L69

The config is passed as a properties of custom resource and is created here:

https://github.com/aws/aws-cdk/blob/c3b3c935b2086288dd79f559a49c3b5304a8e40f/packages/%40aws-cdk/aws-eks/lib/cluster.ts#L364-L379

So I suggest to expose a way to include logging properties in the config so it should be passed to eks.create_cluster() method without any more changes. That should result in enabling logging on newly created EKS cluster.


This is a :rocket: Feature Request

stefanolczak avatar Sep 19 '19 10:09 stefanolczak

Hi @stefanolczak, thanks for submitting a feature request! We will update this issue when there is any progress.

SomayaB avatar Oct 01 '19 23:10 SomayaB

Any updates on this? Waiting for this feature as well.

yjw113080 avatar Mar 18 '20 07:03 yjw113080

This is not highly prioritized at the moment, but more than happy to take contributions.

eladb avatar Mar 18 '20 08:03 eladb

Note that there is an abandoned PR for this: https://github.com/aws/aws-cdk/pull/8497

Consider resurrecting it once we pick this up again.

iliapolo avatar Aug 16 '20 09:08 iliapolo

Any update on this feature?

rameshmimit avatar Nov 18 '20 01:11 rameshmimit

@rameshmimit We are discussing this issue internally, we'll update here soon.

iliapolo avatar Dec 01 '20 14:12 iliapolo

In the meantime, what workarounds are available? It seems to me that click-ops or AWS cli are the alternatives, but neither is amicable to automation - is that correct? CLI will error if there's no change required.

micheal-hill avatar Mar 16 '21 06:03 micheal-hill

Since cluster logging can be updated post cluster creation, you can create a custom resource that updates that config. The AWS Custom Resource should be a good fit for this.

iliapolo avatar Mar 16 '21 13:03 iliapolo

Any update on this?

daisuke-yoshimoto avatar Apr 19 '21 13:04 daisuke-yoshimoto

If anybody wants the workaround, here's code we wrote in our CDK to enable the logging for the cluster using AwsCustomResource.

import { FargateCluster } from "@aws-cdk/aws-eks";
import { Stack } from "@aws-cdk/core";
import {
  AwsCustomResource,
  AwsCustomResourcePolicy,
} from "@aws-cdk/custom-resources";

// Enables logs for the cluster.
//
// Taken from
// https://github.com/aws/aws-cdk/issues/4159#issuecomment-855625700
export function setupClusterLogging(
  stack: Stack,
  cluster: FargateCluster
): void {
  new AwsCustomResource(stack, "ClusterLogsEnabler", {
    policy: AwsCustomResourcePolicy.fromSdkCalls({
      resources: [`${cluster.clusterArn}/update-config`],
    }),
    onCreate: {
      physicalResourceId: { id: `${cluster.clusterArn}/LogsEnabler` },
      service: "EKS",
      action: "updateClusterConfig",
      region: stack.region,
      parameters: {
        name: cluster.clusterName,
        logging: {
          clusterLogging: [
            {
              enabled: true,
              types: [
                "api",
                "audit",
                "authenticator",
                "controllerManager",
                "scheduler",
              ],
            },
          ],
        },
      },
    },
    onDelete: {
      physicalResourceId: { id: `${cluster.clusterArn}/LogsEnabler` },
      service: "EKS",
      action: "updateClusterConfig",
      region: stack.region,
      parameters: {
        name: cluster.clusterName,
        logging: {
          clusterLogging: [
            {
              enabled: false,
              types: [
                "api",
                "audit",
                "authenticator",
                "controllerManager",
                "scheduler",
              ],
            },
          ],
        },
      },
    },
  });
}

jtomaszewski avatar Jun 07 '21 06:06 jtomaszewski

So I did the above with a AwsCustomResource and it worked great until we wanted to create a FargateProfile. Even adding an explicit dependency from the FargateProfile on the logging custom resource didn't help - it fails with a "you can't update the logs while we are creating a Fargate Profile" error. Having fought with it all day I am now having to look at going lower level to a Lambda-backed custom resource to get access to a is_complete_handler or something.

jasonumiker avatar Oct 16 '21 10:10 jasonumiker

@jasonumiker in our case we're using Fargate, but the logging config is set after Fargate has been setup. We're also using eks.FargateCluster in case that's relevant.

micheal-hill avatar Oct 18 '21 01:10 micheal-hill

Thanks - yeah I eventually tried flipping the dependency and that seems to work 🤞

jasonumiker avatar Oct 18 '21 01:10 jasonumiker

CloudFormation (NOT CDK) supports EKS control plane logging settings. https://aws.amazon.com/about-aws/whats-new/2021/11/amazon-eks-cluster-configuration-aws-cloudformation/ How About CDK?

SpringMT avatar Nov 17 '21 04:11 SpringMT

CloudFormation (NOT CDK) supports EKS control plane logging settings.

I thought this would be easy...just use an escape hatch, like this:

    const cfnCluster = cluster.node.defaultChild as eks.CfnCluster;
    cfnCluster.logging = {
      clusterLogging: {
        enabledTypes: [{ type: 'api' }, { type: 'audit' }, { type: 'authenticator' }, { type: 'controllerManager' }, { type: 'scheduler' }]
      }
    }

But alas, it seems like the CDK does not use the low-level CfnCluster (aka AWS::EKS::Cluster) to provision a cluster, but depends on a custom resource:

https://github.com/aws/aws-cdk/blob/b8a4a9a9d28c65aff419beb5d7ac14fa4a0a473e/packages/%40aws-cdk/aws-eks/lib/cluster-resource.ts#L73-L102

Long story short the quick "escape hatch" hack does not work :sob:

bweigel avatar Nov 25 '21 19:11 bweigel

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jan 28 '22 18:01 github-actions[bot]