Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Fixes and Improvements #25

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1f8dfe1
Update Dockerfile
mitscheltobi Feb 28, 2023
9243d6a
Update entrypoint.sh
mitscheltobi Feb 28, 2023
8945a38
Update entrypoint.sh
mitscheltobi Feb 28, 2023
c9930f0
Update entrypoint.sh
mitscheltobi Feb 28, 2023
f124aee
Update entrypoint.sh
mitscheltobi Feb 28, 2023
9823d30
Update entrypoint.sh
mitscheltobi Feb 28, 2023
4467ba0
Update entrypoint.sh
mitscheltobi Feb 28, 2023
0807622
Update Dockerfile
NicPWNs Feb 11, 2024
469a1c2
fix: sleep longer
NicPWNs Feb 11, 2024
25e0ab6
Update entrypoint.sh
NicPWNs Feb 11, 2024
f4bf690
Update README.md
NicPWNs Feb 11, 2024
a6370f2
fix: wait state
NicPWNs Feb 11, 2024
9f8418a
Update entrypoint.sh
NicPWNs Feb 11, 2024
8845858
Update entrypoint.sh
NicPWNs Feb 11, 2024
3c6413c
Update entrypoint.sh
NicPWNs Feb 11, 2024
02943c6
Update action.yml
NicPWNs Feb 11, 2024
4505bbc
Update Dockerfile
NicPWNs Feb 11, 2024
b9d3ab0
Update Dockerfile
NicPWNs Feb 11, 2024
f6c629c
Update Dockerfile
NicPWNs Feb 11, 2024
64167f4
Update action.yml
NicPWNs Feb 11, 2024
ab53ab3
Update entrypoint.sh
NicPWNs Feb 11, 2024
b4a8564
Update Dockerfile
NicPWNs Feb 11, 2024
6a5ab65
Update action.yml
NicPWNs Feb 11, 2024
1c75c19
Update entrypoint.sh
NicPWNs Feb 11, 2024
c7e7d75
Update entrypoint.sh
NicPWNs Feb 11, 2024
12574be
Update entrypoint.sh
NicPWNs Feb 11, 2024
543b5dc
Update action.yml
NicPWNs Feb 11, 2024
1dae5be
Update README.md
NicPWNs Feb 11, 2024
d33ddc2
Update README.md
NicPWNs Feb 11, 2024
f1322a9
docs(README): update
NicPWNs Feb 17, 2024
fd90006
feat(action): New Python default
NicPWNs Feb 17, 2024
7afdef1
docs(README): Update
NicPWNs Feb 17, 2024
8685909
Update entrypoint.sh
NicPWNs Feb 17, 2024
6130ea7
fix(entrypoint.sh): Wait longer for function state updates
NicPWNs Aug 7, 2024
2ac40c5
fix(entrypoint.sh): wait more
NicPWNs Aug 8, 2024
ee31823
Update entrypoint.sh
NicPWNs Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.6
FROM dhermes/python-multi

RUN apt-get update
RUN apt-get install -y jq zip
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

[![GitHubActions](https://img.shields.io/badge/listed%20on-GitHubActions-blue.svg)](https://github-actions.netlify.com/py-lambda)

A Github Action to deploy AWS Lambda functions written in Python with their dependencies in a separate layer. For now, only works with Python 3.6. PRs welcome.
A GitHub Action to deploy AWS Lambda functions written in Python with their dependencies in a separate layer. Works with Python versions 3.8, 3.9, 3.10, 3.11, 3.12. PRs welcome.

## Use
Deploys everything in the repo as code to the Lambda function, and installs/zips/deploys the dependencies as a separate layer the function can then immediately use.

### Pre-requisites
In order for the Action to have access to the code, you must use the `actions/checkout@master` job before it. See the example below.
In order for the Action to have access to the code, you must use the `actions/checkout@main` job before it. See the example below.

### Structure
- Lambda code should be structured normally/as Lambda would expect it.
- **Dependencies must be stored in a `requirements.txt`** or a similar file (provide the filename explicitly if that's the case).

### Environment variables
Stored as secrets or env vars, doesn't matter. But also please don't put your AWS keys outside Secrets.
- **AWS Credentials**
- **AWS Credentials**
That includes the `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, etc. It's used by `awscli`, so the docs for that [can be found here](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html).

### Inputs
Expand All @@ -29,6 +29,8 @@ Stored as secrets or env vars, doesn't matter. But also please don't put your AW
- Partial ARN - `123456789012:function:my-function`
- `requirements_txt`
The name/path for the `requirements.txt` file. Defaults to `requirements.txt`.
- `python_version`
The version of Python to build with. (3.8, 3.9, 3.10, 3.11, 3.12)


### Example workflow
Expand All @@ -43,12 +45,13 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@master
- uses: actions/checkout@main
- name: Deploy code to Lambda
uses: mariamrf/py-lambda-action@v1.0.0
uses: mariamrf/py-lambda-action@master
with:
lambda_layer_arn: 'arn:aws:lambda:us-east-2:123456789012:layer:my-layer'
lambda_function_name: 'my-function'
python_version: '3.12'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand Down
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Py Lambda Deploy
author: Mariam Maarouf
description: Deploy python code to AWS Lambda with dependencies in a separate layer.
description: Deploy Python code to AWS Lambda with dependencies in a separate layer.
inputs:
requirements_txt:
description: the name/path to the requirements.txt file
description: The name/path to the requirements.txt file.
required: true
default: 'requirements.txt'
lambda_layer_arn:
Expand All @@ -12,13 +12,18 @@ inputs:
lambda_function_name:
description: The Lambda function name. Check the AWS docs/readme for examples.
required: true
python_version:
description: The version of Python to build with.
required: true
default: '3.12'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.requirements_txt }}
- ${{ inputs.lambda_layer_arn }}
- ${{ inputs.lambda_function_name }}
- ${{ inputs.python_version }}
branding:
icon: 'layers'
color: 'yellow'
18 changes: 16 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ set -e
install_zip_dependencies(){
echo "Installing and zipping dependencies..."
mkdir python
pip install --target=python -r "${INPUT_REQUIREMENTS_TXT}"
pip${INPUT_PYTHON_VERSION} install --target=python -r "${INPUT_REQUIREMENTS_TXT}"
zip -r dependencies.zip ./python
}

publish_dependencies_as_layer(){
echo "Publishing dependencies as a layer..."
local result=$(aws lambda publish-layer-version --layer-name "${INPUT_LAMBDA_LAYER_ARN}" --zip-file fileb://dependencies.zip)
local result
result=$(aws lambda publish-layer-version --layer-name "${INPUT_LAMBDA_LAYER_ARN}" --zip-file fileb://dependencies.zip --compatible-runtimes "python${INPUT_PYTHON_VERSION}" --compatible-architectures "x86_64")
LAYER_VERSION=$(jq '.Version' <<< "$result")
rm -rf python
rm dependencies.zip
Expand All @@ -24,6 +25,19 @@ publish_function_code(){

update_function_layers(){
echo "Using the layer in the function..."
local function_state
local function_status
function_state=$(aws lambda get-function --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --query 'Configuration.State')
function_status=$(aws lambda get-function --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --query 'Configuration.LastUpdateStatus')
echo "Waiting... Current Function State is $function_state and Function Status is $function_status"
sleep 10
while [[ $function_state != "\"Active\"" && $function_status != "\"Successful\"" ]]
do
function_state=$(aws lambda get-function --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --query 'Configuration.State')
function_status=$(aws lambda get-function --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --query 'Configuration.LastUpdateStatus')
echo "Waiting... Current Function State is $function_state and Function Status is $function_status"
sleep 10
done
aws lambda update-function-configuration --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --layers "${INPUT_LAMBDA_LAYER_ARN}:${LAYER_VERSION}"
}

Expand Down