This repository was archived by the owner on Nov 13, 2024. It is now read-only.
forked from DevGlitch/AWS_Lambda_Postgres2Parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (144 loc) · 6.57 KB
/
deploy-to-lambda.yml
File metadata and controls
167 lines (144 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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 and Link 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."
# Fetch existing Lambda layers
echo "Fetching existing layers for the Lambda function..."
EXISTING_LAYERS=$(aws lambda get-function-configuration --function-name $LAMBDA_FUNCTION_NAME --query 'Layers[*].LayerArn' --output text)
# Combine the existing and new layers
ALL_LAYERS="$EXISTING_LAYERS $LAYER_ARN"
# Update the Lambda function to include all layers
echo "Updating Lambda function with all layers..."
aws lambda update-function-configuration --function-name $LAMBDA_FUNCTION_NAME --layers $ALL_LAYERS > /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."