Skip to main content

Deploy Bash Lambda Functions with AWS CDK

Project description

NPM version PyPI version Release

cdk-lambda-bash

Deploy Bash Lambda Functions with AWS CDK

Why

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.

BashExecFunction

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 fn.run(), a custom resource will be created and the lambda.DockerImageFunction will be executed on deployment.

Sample

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
app = cdk.App()

stack = cdk.Stack(app, "my-stack")

# bundle your Lambda function to execute the local demo.sh in container
fn = BashExecFunction(stack, "Demo",
    script=path.join(__dirname, "../demo.sh")
)

# run it as custom resource on deployment
fn.run()

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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
fn.run(run_on_update=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
FROM public.ecr.aws/lambda/provided:al2

RUN yum install -y unzip jq

# install aws-cli v2
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
  unzip awscliv2.zip && \
  ./aws/install

# install kubectl
RUN curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/kubectl && \
  chmod +x kubectl && \
  mv kubectl /usr/local/bin/kubectl

COPY bootstrap /var/runtime/bootstrap
COPY function.sh /var/task/function.sh
COPY main.sh /var/task/main.sh
RUN chmod +x /var/runtime/bootstrap /var/task/function.sh /var/task/main.sh

WORKDIR /var/task
CMD [ "function.sh.handler" ]

Specify your own Dockerfile with the dockerfile property.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
BashExecFunction(stack, "Demo",
    script=path.join(__dirname, "../demo.sh"),
    dockerfile=path.join(__dirname, "../Dockerfile")
)

Conditional Execution

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

For example

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
install_argo_cD = BashExecFunction(...)

install_argo_cD.run(run_on_update=True)

When you run this sample, demo.sh 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:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
install_argo_cD = BashExecFunction(...)

Under the hood, demo.sh 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
#!/bin/bash

# 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 ;;
  esac
}

echo "Hello cdk lambda bash!!"

conditionalExec

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-0.5.73.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_lambda_bash-0.5.73-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file cdk-lambda-bash-0.5.73.tar.gz.

File metadata

  • Download URL: cdk-lambda-bash-0.5.73.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.10

File hashes

Hashes for cdk-lambda-bash-0.5.73.tar.gz
Algorithm Hash digest
SHA256 6d364af2c5e02e1d01fa0ff4101c6287bf0c6d3f0e0df497f505c5d846b17365
MD5 aedc07ecbe88354e6e488fdb31f67623
BLAKE2b-256 11643399710eefd7d3350f191d3dff3d14ebb5f459eaa5d5001002a2aaf356b8

See more details on using hashes here.

File details

Details for the file cdk_lambda_bash-0.5.73-py3-none-any.whl.

File metadata

  • Download URL: cdk_lambda_bash-0.5.73-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.10

File hashes

Hashes for cdk_lambda_bash-0.5.73-py3-none-any.whl
Algorithm Hash digest
SHA256 51070c9063dc9ee1ffed4c91dec5c1b7e8eb90fa7e23a98280a11e3a811bc919
MD5 5e699c728267b5ae042f3bc1ad63265f
BLAKE2b-256 a6c4683587fba33e2c69d4bec44f859f158defb4c39dae57b29eb5c4a7157dca

See more details on using hashes here.

Supported by

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