Package: cdk-lambda-bash
cdk-lambda-bashDeploy Bash Lambda Functions with AWS CDKWhyAWS Lambda has thedocker container image supportsince AWS re:Invent 2020 which allows you to run your Lambda code in a custom container image. Inspired bynikovirtala/cdk-eks-experiment,cdk-lambda-bashallows 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.BashExecFunctionAt this moment, we are offeringBashExecFunctionconstruct class which is a high-level abstraction oflambda.Function. By defining thescriptproperty which poins to your local shell script, oncdk deploy, this script will be bundled into a custom docker image and published as alambda.DockerImageFunction.If youfn.run(), a custom resource will be created and thelambda.DockerImageFunctionwill be executed on deployment.InstallUse the npm dist tag to opt in CDKv1 or CDKv2://forCDKv2
npminstallcdk-lambda-bash
or
npminstallcdk-lambda-bash@latest

//forCDKv1
npminstallcdk-lambda-bash@cdkv1Sampleconstapp=newcdk.App();conststack=newcdk.Stack(app,'my-stack');//bundleyourLambdafunctiontoexecutethelocaldemo.shincontainerconstfn=newBashExecFunction(stack,'Demo',{script:path.join(__dirname,'../demo.sh'),})//runitascustomresourceondeploymentfn.run();Re-execution on assets updateBy default, if you update your shell script or Dockerfile and re-deploy your CDK application, theBashExecFunctionwill not be re-executed. UserunOnUpdateto enable the re-execution on update.fn.run({runOnUpdate:true});Custom DockerfileIn some cases, you may customize your ownDockerfile, for instances:You need extra tools or utilities such askubectlorhelmYou need build from your own base imageIn these cases, create a customDockerfileas below and add extra utilities i.e.kubectl:click and view custom Dockerfile sampleFROMpublic.ecr.aws/lambda/provided:al2

RUNyuminstall-yunzipjq# install aws-cli v2RUNcurl"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"-o"awscliv2.zip"&&\unzipawscliv2.zip&&\./aws/install# install kubectlRUNcurl-okubectlhttps://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/kubectl&&\chmod+xkubectl&&\mvkubectl/usr/local/bin/kubectl

COPYbootstrap/var/runtime/bootstrap
COPYfunction.sh/var/task/function.sh
COPYmain.sh/var/task/main.sh
RUNchmod+x/var/runtime/bootstrap/var/task/function.sh/var/task/main.sh

WORKDIR/var/task
CMD["function.sh.handler"]Specify your ownDockerfilewith thedockerfileproperty.newBashExecFunction(stack,'Demo',{script:path.join(__dirname,'../demo.sh'),dockerfile:path.join(__dirname,'../Dockerfile'),});Conditional ExecutionIn the user script(e.g.demo.sh), you are allowed to determine the event type and act accordingly.For exampleconstinstallArgoCD=newBashExecFunction(...)installArgoCD.run({runOnUpdate:true});When you run this sample,demo.shwill receiveonCreateevent and you can run your custom logic to "install ArgoCD" likekubectl apply -f URL. However, if you comment it off and deploy again:constinstallArgoCD=newBashExecFunction(...)//installArgoCD.run({runOnUpdate:true});Under the hood,demo.shwill receiveonDeleteevent and you can run your custom logic to "uninstall ArgoCD"
likekubectl delete -f URL.Check the full sample code below:Click and view the sample code#!/bin/bash# implement your business logic belowfunctiononCreate(){echo"running kubectl apply -f ..."}functiononUpdate(){echo"do nothing on update"}functiononDelete(){echo"running kubectl delete -f ..."}functiongetRequestType(){echo$1|jq-r.RequestType}functionconditionalExec(){requestType=$(getRequestType$EVENT_DATA)# determine the original request typecase$requestTypein'Create')onCreate$1;;'Update')onUpdate$1;;'Delete')onDelete$1;;esac}echo"Hello cdk lambda bash!!"conditionalExecexit0In ActionSee thistweet