Skip to main content

Deploy Bash Lambda Functions with AWS CDK

Project description

NPM version PyPI version Release


Deploy Bash Lambda Functions with AWS CDK


AWS Lambda has the docker container image support since AWS re:Invent 2020 which allows you to run your Lambda code in a custom container image. Inspired by nikovirtala/cdk-eks-experiment, cdk-lambda-bash allows you to specify a local shell script and bundle it up as a custom resource in your cdk stack. On cdk deployment, your shell script will be executed in a Lambda container environment.


At this moment, we are offering BashExecFunction construct class which is a high-level abstraction of lambda.Function. By defining the script property which poins to your local shell script, on cdk deploy, this script will be bundled into a custom docker image and published as a lambda.DockerImageFunction.

If you, a custom resource will be created and the lambda.DockerImageFunction will be executed on deployment.


Use the npm dist tag to opt in CDKv1 or CDKv2:

// for CDKv2
npm install cdk-lambda-bash
npm install cdk-lambda-bash@latest

// for CDKv1
npm install cdk-lambda-bash@cdkv1


const app = new cdk.App();

const stack = new cdk.Stack(app, 'my-stack');

// bundle your Lambda function to execute the local in container
const fn = new BashExecFunction(stack, 'Demo', {
  script: path.join(__dirname, '../'),

// run it as custom resource on deployment;

Re-execution on assets update

By default, if you update your shell script or Dockerfile and re-deploy your CDK application, the BashExecFunction will not be re-executed. Use runOnUpdate to enable the re-execution on update.{ runOnUpdate: true });

Custom Dockerfile

In some cases, you may customize your own Dockerfile, for instances:

  1. You need extra tools or utilities such as kubectl or helm
  2. You need build from your own base image

In these cases, create a custom Dockerfile as below and add extra utilities i.e. kubectl:

click and view custom Dockerfile sample

RUN yum install -y unzip jq

# install aws-cli v2
RUN curl "" -o "" && \
  unzip && \

# install kubectl
RUN curl -o kubectl && \
  chmod +x kubectl && \
  mv kubectl /usr/local/bin/kubectl

COPY bootstrap /var/runtime/bootstrap
COPY /var/task/
COPY /var/task/
RUN chmod +x /var/runtime/bootstrap /var/task/ /var/task/

WORKDIR /var/task
CMD [ "" ]

Specify your own Dockerfile with the dockerfile property.

new BashExecFunction(stack, 'Demo', {
  script: path.join(__dirname, '../'),
  dockerfile: path.join(__dirname, '../Dockerfile'),

Conditional Execution

In the user script(e.g., you are allowed to determine the event type and act accordingly.

For example

const installArgoCD = new BashExecFunction(...){runOnUpdate: true});

When you run this sample, will receive onCreate event and you can run your custom logic to "install ArgoCD" like kubectl apply -f URL. However, if you comment it off and deploy again:

const installArgoCD = new BashExecFunction(...)

//{runOnUpdate: true});

Under the hood, will receive onDelete event and you can run your custom logic to "uninstall ArgoCD" like kubectl delete -f URL.

Check the full sample code below:

Click and view the sample code

# implement your business logic below
function onCreate() {
  echo "running kubectl apply -f ..."

function onUpdate() {
  echo "do nothing on update"

function onDelete() {
  echo "running kubectl delete -f ..."

function getRequestType() {
  echo $1 | jq -r .RequestType

function conditionalExec() {
  requestType=$(getRequestType $EVENT_DATA)

  # determine the original request type
  case $requestType in
    'Create') onCreate $1 ;;
    'Update') onUpdate $1 ;;
    'Delete') onDelete $1 ;;

echo "Hello cdk lambda bash!!"


exit 0

In Action

See this tweet

Project details

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-lambda-bash-2.0.327.tar.gz (40.7 kB view hashes)

Uploaded Source

Built Distribution

cdk_lambda_bash-2.0.327-py3-none-any.whl (39.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page