Home

Awesome

build

lambda-layer-kubectl

AWS CDK(Cloud Development Kit) comes with lambda-layer-kubectl which allows you to build your private AWS Lambda layer with kubectl executable. Ths repository demonstrates how to create your own AWS Lambda layer with kubectl in AWS CDK.

Basic Usage

import { App, CfnOutput, Construct, Stack, StackProps } from '@aws-cdk/core';
import * as layer from '@aws-cdk/lambda-layer-kubectl';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const kubectlLayer = new layer.KubectlLayer(this, 'KubectlLayer');
    new CfnOutput(this, 'LayerVersionArn', { value: kubectlLayer.layerVersionArn })

  }
}

const devEnv = {
  account: process.env.CDK_DEFAULT_ACCOUNT,
  region: process.env.CDK_DEFAULT_REGION,
};

const app = new App();

new MyStack(app, 'kubectl-layer-stack', { env: devEnv });

app.synth();

After deployment, the AWS Lambda layer version ARN will be returned and you can use this ARN in your Lambda functions in the same AWS region.

Outputs:
kubectl-layer-stack.LayerVersionArn = arn:aws:lambda:us-east-1:123456789012:layer:KubectlLayer600207B5:1

Customize your layer

The kubectlLayer from AWS CDK upstream does not allow you to pass custom Dockerfile(see the build-in Dockerfile). To customize the layer, we simply create our own KubectlLayer construct class in our CDK application with our custom Dockerfile.

cd src/custom-layer
# edit and customize the Dockerfile under the `custom-layer` directory
# generate the layer.zip from Dockerfile
bash build.sh

Now prepare your custom KubectlLayer construct class and run cdk deploy to generate your own layer.

import { App, CfnOutput, Construct, Stack, StackProps } from '@aws-cdk/core';
import * as customlayer from './custom-layer/custom-layer'

export class CustomLayderStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const kubectlLayer = new customlayer.KubectlLayer(this, 'CustomKubectlLayer');
    new CfnOutput(this, 'LayerVersionArn', { value: kubectlLayer.layerVersionArn })

  }
}

const devEnv = {
  account: process.env.CDK_DEFAULT_ACCOUNT,
  region: process.env.CDK_DEFAULT_REGION,
};

const app = new App();

new CustomLayderStack(app, 'custom-kubectl-layer-stack', { env: devEnv });

app.synth();

License Summary

This sample code is made available under the MIT-0 license. See the LICENSE file.