Deploy Bash Lambda Functions with AWS CDK
Project description
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()
Custom Dockerfile
In some cases, you may customize your own Dockerfile, for instances:
- You need extra tools or utilities such as
kubectlorhelm - 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
app = cdk.App()
stack = cdk.Stack(app, "my-stack-dev", env=dev_env)
BashExecFunction(stack, "Demo",
script=path.join(__dirname, "../demo.sh"),
dockerfile=path.join(__dirname, "../Dockerfile")
).run()
app.synth()
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cdk-lambda-bash-0.2.0.tar.gz.
File metadata
- Download URL: cdk-lambda-bash-0.2.0.tar.gz
- Upload date:
- Size: 3.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a75b3e876e7fc49ad775516649d0c9b7e6b913e49da10fdf77a61ebb23520a1
|
|
| MD5 |
b6bb5dea7eddacaa0e98d69ca63435df
|
|
| BLAKE2b-256 |
8980310f1a66c8ac2b7b7bfd4ef8aea7f94d33e10b70b846975a10561bdbaa76
|
File details
Details for the file cdk_lambda_bash-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cdk_lambda_bash-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deff06608d5c0c265443802ecc0102ddf61f4351c539177053a2fc26ebb354d7
|
|
| MD5 |
78acae934d6a9a3275bab8376ac353bb
|
|
| BLAKE2b-256 |
3ff2cf41d4a592c71b79214f7cc481c3424744ce188992866cc1e124dd8435b7
|