Skip to content

Instantly share code, notes, and snippets.

@ttamg
Created January 31, 2021 09:50
Show Gist options
  • Save ttamg/2fc1f6b76542be0536dfc902a3c209d1 to your computer and use it in GitHub Desktop.
Save ttamg/2fc1f6b76542be0536dfc902a3c209d1 to your computer and use it in GitHub Desktop.
End to end job to build python app, test, upload to S3 and deploy to existing AWS Lambda
# End to end job to build, test, upload to S3 and deploy to AWS Lambda
# 1. Builds python package installing dependencies in library we can access for ZIP file
# 2. Runs tests with Pytest
# 3. Creates ZIP package for Lambda (date-stamped filename)
# 4. Uploads to your S3 bucket
# 5. Deploys to AWS Lambda function
# Runs on update to main branch
# Because tests are run, the .pyc files are included in the package (faster Lambda run times?)
name: Build, test and deploy to Lambda
on:
push:
branches: [main]
env:
S3_BUCKET: MY_BUCKET_NAME
LAMBDA_NAME: MY_LAMBDA_FUNCTION_NAME
jobs:
build-test-deploy:
name: Build and test
runs-on: ubuntu-latest
steps:
- name: checkout source code
uses: actions/checkout@v2
- name: Get current date for filename timestamp
id: date
run: echo "DATE=$(date +'%Y%m%d-%H%M')" >> $GITHUB_ENV
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt --ignore-installed --target=$GITHUB_WORKSPACE/lib; fi
python -m pip install pytest requests
- name: Test with pytest - should have created pyc files
run: pytest
- name: Create zip package (include created pyc files)
shell: bash
run: |
cd $GITHUB_WORKSPACE/lib
ls
zip -r9 $GITHUB_WORKSPACE/$LAMBDA_NAME-$DATE.zip .
cd $GITHUB_WORKSPACE
ls
zip -g $GITHUB_WORKSPACE/$LAMBDA_NAME-$DATE.zip -r app
- 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: ${{ secrets.AWS_REGION }}
- name: Copy files to S3
shell: bash
run: |
aws s3 cp $GITHUB_WORKSPACE/$LAMBDA_NAME-$DATE.zip s3://$S3_BUCKET
- name: Deploy to Lambda
shell : bash
run: |
aws lambda update-function-code --function-name $LAMBDA_NAME --s3-bucket $S3_BUCKET --s3-key $LAMBDA_NAME-$DATE.zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment