Skip to content

Instantly share code, notes, and snippets.

@wyllie
Last active August 30, 2024 19:54
Show Gist options
  • Save wyllie/1a2d32a3282f817e1f2bebea95ab4c38 to your computer and use it in GitHub Desktop.
Save wyllie/1a2d32a3282f817e1f2bebea95ab4c38 to your computer and use it in GitHub Desktop.
GitHub Actions workflow to deploy code to AWS Lambda Layers
# This workflow will install dependencies and create a build suitable
# to be used in an AWS Lambda Layer. The build will then be uploaded
# to S3 and then can be accessed from any lambda that uses the layer.
#
# This build is only for dev builds. Releases will be built from a
# seperate action.
#
# A new version of the layer will be created for every branch when a
# pull request is intitiated. This allows us to test the layer in a
# dev environment on AWS BEFORE the code is merged into master.
name: Build Lambda Layer
on:
pull_request:
branches: [ master ]
jobs:
deploy:
name: Upload Layer to AWS Lambda
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# For more info: https://github.com/aws-actions/configure-aws-credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: Setup Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Zip it all up and upload to S3
env:
release_bucket: <your AWS S3 bucket name goes here>
release_bucket_uri: <the uri for your S3 bucket - like s3://my_bucket_name>
release_id: ${{ format('<REPO NAME>-{0}-dev.zip', github.head_ref )}}
release_layer: ${{ format('<REPO NAME>-{0}-dev', github.head_ref )}}
run: |
mkdir python
# assuming your requirements file is in 'requirements/prod.txt'
pip install -r requirements/prod.txt -t python
pip install . -t python
echo building release $release_id
# zip it up
zip --quiet -r $release_id python
# copy the file to S3 and install it in lambda layers
aws s3 cp $release_id $release_bucket_uri
aws lambda publish-layer-version --layer-name $release_layer --content S3Bucket=$release_bucket,S3Key=$release_id --compatible-runtimes python3.8
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"lambda:PublishLayerVersion",
],
"Resource": "*"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:PutBucketNotification",
"s3:AbortMultipartUpload",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::my_bucket_name/*",
"arn:aws:s3:::my_bucket_name"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment