This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
🚀 Deploy to AWS Lambda #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 🚀 Deploy to AWS Lambda | |
on: | |
# Uncomment any of the following lines to trigger the workflow based on your needs | |
# Trigger the workflow on push or pull request, | |
# push: | |
# branches: | |
# - main | |
# Trigger the workflow on a schedule, | |
# schedule: | |
# - cron: '0 0 * * *' # Run every day at midnight UTC | |
# Allows you to trigger this workflow manually from the Actions tab | |
workflow_dispatch: | |
# Allow only one workflow run at a time | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
LAMBDA_FUNCTION_NAME: Postgres2Parquet | |
steps: | |
- name: 🛒 Checkout | |
uses: actions/checkout@v3 | |
- name: 🔧 Install AWS CLI | |
run: | | |
echo "Installing AWS CLI..." | |
curl -sS "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip -q awscliv2.zip | |
sudo ./aws/install --update | |
echo "AWS CLI installation complete." | |
- name: ⚙️ Configure AWS CLI | |
run: | | |
echo "Configuring AWS CLI..." | |
aws configure set region us-east-1 | |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
echo "AWS CLI configured." | |
- name: 🐍 Create Python virtual environment | |
run: | | |
python -m venv venv | |
source venv/bin/activate | |
pip install --upgrade pip | |
- name: 🏗️ Build psycopg2 and sqlalchemy for Linux | |
run: | | |
source venv/bin/activate | |
pip install psycopg2-binary sqlalchemy -t ./python | |
zip -r my_lambda_layer.zip python/ | |
- name: 📦 Upload Lambda Layer | |
run: | | |
LAYER_NAME="${LAMBDA_FUNCTION_NAME}-dependencies" | |
MY_LAYER_PATH="my_lambda_layer.zip" | |
# Create a new version of the Lambda layer | |
echo "Creating a new version of Lambda Layer..." | |
LAYER_ARN=$(aws lambda publish-layer-version \ | |
--layer-name $LAYER_NAME \ | |
--description "Python Dependencies Layer" \ | |
--compatible-runtimes python3.10 \ | |
--license-info "MIT" \ | |
--zip-file "fileb://$MY_LAYER_PATH" \ | |
--query 'LayerVersionArn' \ | |
--output text) | |
echo "New layer version created successfully." | |
echo "Ensuring layer is linked to function..." | |
aws lambda update-function-configuration --function-name $LAMBDA_FUNCTION_NAME --layers $LAYER_ARN > /dev/null 2>&1 | |
# Check the status of the update | |
while true; do | |
status=$(aws lambda get-function-configuration --function-name $LAMBDA_FUNCTION_NAME --query 'LastUpdateStatus' --output text) | |
if [ "$status" = "Successful" ]; then | |
echo "Layer linked to function successfully." | |
break | |
elif [ "$status" = "Failed" ]; then | |
echo "Layer update failed." | |
exit 1 | |
else | |
echo "Checking Layer update status..." | |
sleep 5 | |
fi | |
done | |
- name: 🚀 Deploy Code to AWS Lambda | |
run: | | |
SOURCE_FILE="lambda_function.py" | |
QUERY_FILE="query.sql" | |
# Deploy the Python file and query.sql to Lambda | |
echo "Creating deployment package..." | |
zip deployment.zip $SOURCE_FILE $QUERY_FILE | |
echo "Uploading deployment package to Lambda..." | |
aws lambda update-function-code --function-name $LAMBDA_FUNCTION_NAME --zip-file fileb://deployment.zip > /dev/null 2>&1 | |
# Check the status of the update | |
while true; do | |
status=$(aws lambda get-function-configuration --function-name $LAMBDA_FUNCTION_NAME --query 'LastUpdateStatus' --output text) | |
if [ "$status" = "Successful" ]; then | |
echo "Lambda function update completed successfully." | |
break | |
elif [ "$status" = "Failed" ]; then | |
echo "Lambda function update failed." | |
exit 1 | |
else | |
echo "Checking Lambda function update status..." | |
sleep 5 | |
fi | |
done | |
- name: 🌍 Update Lambda Environment Variables | |
run: | | |
echo "Updating Lambda function environment variables..." | |
aws lambda update-function-configuration \ | |
--function-name $LAMBDA_FUNCTION_NAME \ | |
--environment '{ | |
"Variables":{ | |
"ENVIRONMENT":"${{ secrets.ENVIRONMENT }}", | |
"DB_NAME_PROD":"${{ secrets.DB_NAME_PROD }}", | |
"DB_HOST_PROD":"${{ secrets.DB_HOST_PROD }}", | |
"DB_PORT_PROD":"${{ secrets.DB_PORT_PROD }}", | |
"DB_USER":"${{ secrets.DB_USER }}", | |
"DB_PASSWORD_PROD":"${{ secrets.DB_PASSWORD_PROD }}", | |
"S3_PATH":"${{ secrets.S3_PATH }}", | |
"FILE_NAME":"${{ secrets.FILE_NAME }}" | |
} | |
}' > /dev/null 2>&1 | |
# Check the status of the update | |
while true; do | |
status=$(aws lambda get-function-configuration --function-name $LAMBDA_FUNCTION_NAME --query 'LastUpdateStatus' --output text) | |
if [ "$status" = "Successful" ]; then | |
echo "Lambda function environment variables updated." | |
break | |
elif [ "$status" = "Failed" ]; then | |
echo "Lambda function environment variables update failed." | |
exit 1 | |
else | |
echo "Checking Lambda function environment variables update status..." | |
sleep 5 | |
fi | |
done | |
- name: 📤 Publish New Lambda Version | |
run: | | |
echo "Publishing new version of the lambda function..." | |
new_version=$(aws lambda publish-version --function-name $LAMBDA_FUNCTION_NAME --query 'Version' --output text) | |
echo "New lambda function version $new_version published." |