Skip to content

Commit f226bea

Browse files
committed
Cubed Lithops Runtime Builder Template
0 parents  commit f226bea

5 files changed

Lines changed: 414 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Build and Deploy Lithops Runtime
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- 'Dockerfile'
8+
- '.lithops/config'
9+
workflow_dispatch:
10+
11+
permissions:
12+
id-token: write
13+
contents: read
14+
15+
env:
16+
PYTHON_VERSION: '3.12'
17+
RUNTIME_NAME: 'cubed-runtime'
18+
19+
jobs:
20+
build:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Read AWS region from config
26+
id: config
27+
run: |
28+
region=$(python3 -c "
29+
import yaml
30+
with open('.lithops/config') as f:
31+
c = yaml.safe_load(f)
32+
print(c['aws']['region'].strip())
33+
")
34+
echo "region=$region" >> $GITHUB_OUTPUT
35+
36+
- name: Configure AWS credentials
37+
uses: aws-actions/configure-aws-credentials@v4
38+
with:
39+
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
40+
aws-region: ${{ steps.config.outputs.region }}
41+
42+
- name: Set up Python
43+
uses: actions/setup-python@v5
44+
with:
45+
python-version: ${{ env.PYTHON_VERSION }}
46+
47+
- name: Install Lithops
48+
run: pip install 'lithops[aws]'
49+
50+
- name: Copy Lithops config
51+
run: |
52+
mkdir -p ~/.lithops
53+
grep -v 'log_filename' .lithops/config > ~/.lithops/config
54+
55+
- name: Delete existing runtime
56+
run: lithops runtime delete -d ${{ env.RUNTIME_NAME }} -b aws_lambda
57+
58+
- name: Build runtime
59+
run: lithops runtime build ${{ env.RUNTIME_NAME }} -b aws_lambda -f Dockerfile
60+
61+
- name: Grant Lambda access to ECR repository
62+
run: |
63+
set -euo pipefail
64+
runtime="${{ env.RUNTIME_NAME }}"
65+
repo=$(aws ecr describe-repositories \
66+
--query 'repositories[].repositoryName' \
67+
--output text | tr '\t' '\n' | grep "$runtime" | head -1)
68+
echo "Setting ECR policy on: $repo"
69+
aws ecr set-repository-policy \
70+
--repository-name "$repo" \
71+
--policy-text '{
72+
"Version": "2012-10-17",
73+
"Statement": [{
74+
"Sid": "LambdaECRAccess",
75+
"Effect": "Allow",
76+
"Principal": {"Service": "lambda.amazonaws.com"},
77+
"Action": ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer"]
78+
}]
79+
}'
80+
81+
- name: Deploy runtime
82+
run: lithops runtime deploy ${{ env.RUNTIME_NAME }} -b aws_lambda

.lithops/config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
lithops:
2+
backend: aws_lambda
3+
storage: aws_s3
4+
log_filename: lithops.log # comment out to see logs in the console
5+
# log_level: WARNING
6+
7+
aws:
8+
region: us-east-1 # your AWS region
9+
10+
aws_lambda:
11+
execution_role: arn:aws:iam::... # LambdaExecutionRoleArn
12+
user_id: AROAXXXXXXXXXXXXXXXXX # GitHubActionsRoleId
13+
runtime_memory: 2000
14+
runtime_timeout: 180

Dockerfile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Python 3.12
2+
FROM python:3.12-slim-bookworm
3+
4+
RUN apt-get update \
5+
# Install aws-lambda-cpp build dependencies
6+
&& apt-get install -y \
7+
g++ \
8+
make \
9+
cmake \
10+
unzip \
11+
git \
12+
# cleanup package lists, they are not used anymore in this image
13+
&& rm -rf /var/lib/apt/lists/* \
14+
&& apt-cache search linux-headers-generic
15+
16+
ARG FUNCTION_DIR="/function"
17+
18+
# Copy function code
19+
RUN mkdir -p ${FUNCTION_DIR}
20+
21+
# Update pip
22+
RUN pip install --upgrade --ignore-installed pip wheel six setuptools \
23+
&& pip install --upgrade --no-cache-dir --ignore-installed \
24+
awslambdaric \
25+
botocore \
26+
boto3 \
27+
redis \
28+
httplib2 \
29+
requests \
30+
numpy \
31+
scipy \
32+
pandas \
33+
pika \
34+
kafka-python \
35+
cloudpickle \
36+
ps-mem \
37+
tblib
38+
39+
# Set working directory to function root directory
40+
WORKDIR ${FUNCTION_DIR}
41+
42+
# Add Lithops
43+
COPY lithops_lambda.zip ${FUNCTION_DIR}
44+
RUN unzip lithops_lambda.zip \
45+
&& rm lithops_lambda.zip \
46+
&& mkdir handler \
47+
&& touch handler/__init__.py \
48+
&& mv entry_point.py handler/
49+
50+
RUN pip install \
51+
cubed \
52+
obstore
53+
54+
# Add extra dependencies here
55+
# RUN pip install my-package another-package
56+
57+
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
58+
CMD [ "handler.entry_point.lambda_handler" ]

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Cubed Lithops Runtime Builder
2+
3+
A GitHub template repository for building and deploying [Lithops](https://lithops-cloud.github.io/) Lambda runtimes for [Cubed](https://github.com/cubed-dev/cubed) via CI — no local Docker required.
4+
5+
When you push changes to the `Dockerfile`, GitHub Actions builds a Docker image and deploys it as a Lambda container runtime named `cubed-runtime`.
6+
7+
## Prerequisites
8+
9+
- An AWS account
10+
- A GitHub account
11+
- The [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
12+
13+
## Setup
14+
15+
### 1. Create this repo from the template
16+
17+
Click **Use this template****Create a new repository**.
18+
19+
### 2. Bootstrap AWS
20+
21+
Run this once from your local machine. Replace `YOUR_ORG` with your GitHub username or organisation (e.g. `octocat`) and `YOUR_REPO` with the name you gave this repository.
22+
23+
```bash
24+
aws cloudformation deploy \
25+
--template-file cloudformation/github-oidc-role.yml \
26+
--stack-name cubed-lithops-github-actions \
27+
--parameter-overrides GitHubOrg=YOUR_ORG GitHubRepo=YOUR_REPO \
28+
--capabilities CAPABILITY_NAMED_IAM
29+
```
30+
31+
> **Already have a GitHub OIDC provider?** Use `--parameter-overrides GitHubOrg=YOUR_ORG GitHubRepo=YOUR_REPO CreateOIDCProvider=false` to skip creating one.
32+
33+
This creates a GitHub OIDC identity provider (if one doesn't already exist), an IAM role for GitHub Actions, and an IAM role for Lambda to assume when running functions. When it completes, retrieve both ARNs:
34+
35+
```bash
36+
aws cloudformation describe-stacks \
37+
--stack-name cubed-lithops-github-actions \
38+
--query 'Stacks[0].Outputs' \
39+
--output table
40+
```
41+
42+
### 3. Add the secret
43+
44+
In your repo: **Settings → Secrets and variables → Actions → New repository secret**
45+
46+
| Name | Value |
47+
|------|-------|
48+
| `AWS_ROLE_ARN` | `GitHubActionsRoleArn` from the previous step |
49+
50+
### 4. Edit `.lithops/config`
51+
52+
Replace the placeholder values:
53+
54+
```yaml
55+
aws:
56+
region: us-east-1 # your AWS region
57+
58+
aws_lambda:
59+
execution_role: arn:aws:iam::... # LambdaExecutionRoleArn from the previous step
60+
user_id: AROAXXXXXXXXXXXXXXXXX # GitHubActionsRoleId from the previous step
61+
```
62+
63+
### 5. Add your dependencies
64+
65+
Edit the `Dockerfile` to add extra packages (there is a clearly marked section near the bottom), then push — the CI pipeline builds and deploys the `cubed-runtime` Lambda runtime automatically.
66+
67+
## Manual trigger
68+
69+
You can also trigger a build from **Actions → Build and Deploy Lithops Runtime → Run workflow**.
70+
71+
## Customisation
72+
73+
### Changing the runtime name
74+
75+
The runtime is named `cubed-runtime` by default. To use a different name, edit the `RUNTIME_NAME` env var at the top of `.github/workflows/build-runtime.yml`:
76+
77+
```yaml
78+
env:
79+
RUNTIME_NAME: 'my-runtime-name'
80+
```
81+
82+
Remember to also update the `runtime` key in your local Lithops config if you change this.
83+
84+
### Using the latest Cubed from GitHub
85+
86+
To use the latest development version of Cubed from the `main` branch instead of the PyPI release, replace the `cubed` line in the `Dockerfile`:
87+
88+
```dockerfile
89+
RUN pip install \
90+
'git+https://github.com/cubed-dev/cubed.git#egg=cubed' \
91+
obstore
92+
```

0 commit comments

Comments
 (0)