Repository navigation
268 lines (227 loc) · 8.84 KB
/
Copy pathdeploy-lambda.yml
File metadata and controls
268 lines (227 loc) · 8.84 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
name: Deploy Lambda Functions
on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
type: choice
options:
- development
- staging
- production
functions:
description: 'Lambda functions to deploy (comma-separated or "all")'
required: false
default: 'all'
push:
branches:
- main
- develop
paths:
- 'lambda/**'
- '.github/workflows/deploy-lambda.yml'
env:
AWS_REGION: us-east-1
jobs:
prepare:
name: Prepare Deployment
runs-on: ubuntu-latest
outputs:
functions: ${{ steps.functions.outputs.list }}
environment: ${{ steps.env.outputs.name }}
steps:
- name: Determine environment
id: env
run: |
if [ "${{ github.event.inputs.environment }}" != "" ]; then
echo "name=${{ github.event.inputs.environment }}" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "name=production" >> $GITHUB_OUTPUT
else
echo "name=development" >> $GITHUB_OUTPUT
fi
- name: Determine functions to deploy
id: functions
run: |
if [ "${{ github.event.inputs.functions }}" == "" ] || [ "${{ github.event.inputs.functions }}" == "all" ]; then
echo 'list=["api_handler","image_processor","scheduled_task"]' >> $GITHUB_OUTPUT
else
FUNCTIONS=$(echo "${{ github.event.inputs.functions }}" | jq -R -c 'split(",")')
echo "list=$FUNCTIONS" >> $GITHUB_OUTPUT
fi
deploy:
name: Deploy ${{ matrix.function }}
runs-on: ubuntu-latest
needs: prepare
environment: ${{ needs.prepare.outputs.environment }}
strategy:
matrix:
function: ${{ fromJson(needs.prepare.outputs.functions) }}
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Package Lambda function
run: |
cd lambda/${{ matrix.function }}
# Install dependencies
if [ -f requirements.txt ]; then
pip install -r requirements.txt -t .
fi
# Create deployment package
zip -r function.zip . -x "*.pyc" -x "__pycache__/*" -x "*.git/*"
# Verify package size
SIZE=$(stat -f%z function.zip 2>/dev/null || stat -c%s function.zip)
echo "Package size: $(($SIZE / 1024 / 1024))MB"
if [ $SIZE -gt 52428800 ]; then
echo "Error: Package size exceeds 50MB limit"
exit 1
fi
- name: Deploy Lambda function
run: |
FUNCTION_NAME="${{ needs.prepare.outputs.environment }}-${{ matrix.function }}"
echo "Deploying to function: $FUNCTION_NAME"
aws lambda update-function-code \
--function-name $FUNCTION_NAME \
--zip-file fileb://lambda/${{ matrix.function }}/function.zip \
--publish
- name: Update function configuration
run: |
FUNCTION_NAME="${{ needs.prepare.outputs.environment }}-${{ matrix.function }}"
# Update environment variables based on deployment environment
aws lambda update-function-configuration \
--function-name $FUNCTION_NAME \
--environment Variables="{
ENVIRONMENT=${{ needs.prepare.outputs.environment }},
LOG_LEVEL=${{ needs.prepare.outputs.environment == 'production' && 'INFO' || 'DEBUG' }},
S3_BUCKET_NAME=${{ secrets.S3_BUCKET_NAME }}
}"
- name: Wait for function update
run: |
FUNCTION_NAME="${{ needs.prepare.outputs.environment }}-${{ matrix.function }}"
aws lambda wait function-updated \
--function-name $FUNCTION_NAME
echo "Function $FUNCTION_NAME updated successfully"
- name: Test Lambda function
run: |
FUNCTION_NAME="${{ needs.prepare.outputs.environment }}-${{ matrix.function }}"
# Invoke function with test payload
aws lambda invoke \
--function-name $FUNCTION_NAME \
--payload '{"test": true}' \
--cli-binary-format raw-in-base64-out \
response.json
cat response.json
echo ""
# Check for errors in response
if grep -q "errorMessage" response.json; then
echo "Lambda function test failed"
cat response.json
exit 1
fi
echo "Lambda function test passed"
- name: Tag function version
if: needs.prepare.outputs.environment == 'production'
run: |
FUNCTION_NAME="${{ needs.prepare.outputs.environment }}-${{ matrix.function }}"
# Create alias for this deployment
VERSION=$(aws lambda publish-version \
--function-name $FUNCTION_NAME \
--description "Deployed from commit ${{ github.sha }}" \
--query 'Version' \
--output text)
# Update or create 'live' alias
aws lambda update-alias \
--function-name $FUNCTION_NAME \
--name live \
--function-version $VERSION \
|| aws lambda create-alias \
--function-name $FUNCTION_NAME \
--name live \
--function-version $VERSION
echo "Created version $VERSION with alias 'live'"
verify:
name: Verify Deployment
runs-on: ubuntu-latest
needs: [prepare, deploy]
environment: ${{ needs.prepare.outputs.environment }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Check function health
run: |
FUNCTIONS="${{ join(fromJson(needs.prepare.outputs.functions), ' ') }}"
ENV="${{ needs.prepare.outputs.environment }}"
for func in $FUNCTIONS; do
FUNCTION_NAME="$ENV-$func"
echo "Checking health of $FUNCTION_NAME..."
# Get function configuration
aws lambda get-function-configuration \
--function-name $FUNCTION_NAME \
--query '{State: State, LastUpdateStatus: LastUpdateStatus}' \
--output table
# Get recent errors from CloudWatch
aws logs filter-log-events \
--log-group-name "/aws/lambda/$FUNCTION_NAME" \
--filter-pattern "ERROR" \
--start-time $(( $(date +%s) - 300 ))000 \
--limit 5 \
--query 'events[*].message' \
--output text || echo "No recent errors"
done
- name: Notify on success
if: success()
uses: slackapi/slack-github-action@v1
with:
payload: |
{
"text": "Lambda deployment succeeded for ${{ needs.prepare.outputs.environment }}",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Lambda Deployment Successful* :white_check_mark:\n\nEnvironment: `${{ needs.prepare.outputs.environment }}`\nFunctions: `${{ join(fromJson(needs.prepare.outputs.functions), ', ') }}`\nDeployed by: @${{ github.actor }}"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
continue-on-error: true
- name: Notify on failure
if: failure()
uses: slackapi/slack-github-action@v1
with:
payload: |
{
"text": "Lambda deployment failed for ${{ needs.prepare.outputs.environment }}",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Lambda Deployment Failed* :x:\n\nEnvironment: `${{ needs.prepare.outputs.environment }}`\nFunctions: `${{ join(fromJson(needs.prepare.outputs.functions), ', ') }}`\nDeployed by: @${{ github.actor }}\n\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Run>"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
continue-on-error: true