Skip to content

Commit be021e6

Browse files
author
erastus.ndi
committed
feat: updated Deployment Scripts to Add Lambda Alias and Versioning
1 parent 09ad513 commit be021e6

File tree

3 files changed

+168
-9
lines changed

3 files changed

+168
-9
lines changed

.github/workflows/deploy-to-dev.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Deploy Kainoscore to Dev
33
on:
44
push:
55
branches:
6-
- main
6+
- KFP-824
77

88

99
env:

pipeline_scripts/functions.sh

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,147 @@ function update_lambda {
3838
if [ 0 -eq $? ]; then
3939
echo "Lambda $LAMBDA_NAME exists"
4040
echo
41-
aws lambda update-function-code \
41+
# Update function code and publish a new version
42+
NEW_LAMBDA_VERSION=$(aws lambda update-function-code \
4243
--function-name $LAMBDA_NAME \
4344
--s3-key $LAMBDA_ZIP_FILE \
4445
--s3-bucket $S3_ZIP_FILES \
45-
--output text
46+
--publish \
47+
--query 'Version' \
48+
--output text)
49+
50+
echo "New Lambda version created: $NEW_LAMBDA_VERSION"
51+
export NEW_LAMBDA_VERSION
4652
else
4753
echo "Lambda $LAMBDA_NAME does not exist"
4854
fi
4955
}
5056

51-
function get_lambda_alias_version {
52-
version=$(aws lambda get-alias \
57+
58+
59+
function update_core_lambda_alias {
60+
if [ -z "$NEW_LAMBDA_VERSION" ]; then
61+
echo "Error: NEW_LAMBDA_VERSION is not set"
62+
return 1
63+
fi
64+
65+
if [ -z "$CORE_ALIAS_NAME" ]; then
66+
echo "Error: CORE_ALIAS_NAME is not set"
67+
return 1
68+
fi
69+
70+
if [ -z "$LAMBDA_NAME" ]; then
71+
echo "Error: LAMBDA_NAME is not set"
72+
return 1
73+
fi
74+
75+
# Check if alias exists
76+
aws lambda get-alias --function-name $LAMBDA_NAME --name $CORE_ALIAS_NAME > /dev/null 2>&1
77+
if [ 0 -eq $? ]; then
78+
echo "Updating existing Core alias $CORE_ALIAS_NAME to point to version $NEW_LAMBDA_VERSION"
79+
aws lambda update-alias \
80+
--function-name $LAMBDA_NAME \
81+
--name $CORE_ALIAS_NAME \
82+
--function-version $NEW_LAMBDA_VERSION \
83+
--output text
84+
else
85+
echo "Creating new Core alias $CORE_ALIAS_NAME pointing to version $NEW_LAMBDA_VERSION"
86+
aws lambda create-alias \
87+
--function-name $LAMBDA_NAME \
88+
--name $CORE_ALIAS_NAME \
89+
--function-version $NEW_LAMBDA_VERSION \
90+
--output text
91+
fi
92+
}
93+
94+
function update_kfd_lambda_alias {
95+
if [ -z "$NEW_LAMBDA_VERSION" ]; then
96+
echo "Error: NEW_LAMBDA_VERSION is not set"
97+
return 1
98+
fi
99+
100+
if [ -z "$KFD_ALIAS_NAME" ]; then
101+
echo "Error: KFD_ALIAS_NAME is not set"
102+
return 1
103+
fi
104+
105+
if [ -z "$LAMBDA_NAME" ]; then
106+
echo "Error: LAMBDA_NAME is not set"
107+
return 1
108+
fi
109+
110+
# Check if alias exists
111+
aws lambda get-alias --function-name $LAMBDA_NAME --name $KFD_ALIAS_NAME > /dev/null 2>&1
112+
if [ 0 -eq $? ]; then
113+
echo "Updating existing KFD alias $KFD_ALIAS_NAME to point to version $NEW_LAMBDA_VERSION"
114+
aws lambda update-alias \
115+
--function-name $LAMBDA_NAME \
116+
--name $KFD_ALIAS_NAME \
117+
--function-version $NEW_LAMBDA_VERSION \
118+
--output text
119+
else
120+
echo "Creating new KFD alias $KFD_ALIAS_NAME pointing to version $NEW_LAMBDA_VERSION"
121+
aws lambda create-alias \
122+
--function-name $LAMBDA_NAME \
123+
--name $KFD_ALIAS_NAME \
124+
--function-version $NEW_LAMBDA_VERSION \
125+
--output text
126+
fi
127+
}
128+
129+
function get_current_core_lambda_alias {
130+
if [ -z "$CORE_ALIAS_NAME" ]; then
131+
echo "Error: CORE_ALIAS_NAME is not set"
132+
return 1
133+
fi
134+
135+
if [ -z "$LAMBDA_NAME" ]; then
136+
echo "Error: LAMBDA_NAME is not set"
137+
return 1
138+
fi
139+
140+
# Check if alias exists and get current version
141+
CURRENT_CORE_VERSION=$(aws lambda get-alias \
53142
--function-name $LAMBDA_NAME \
54-
--name $ALIAS_NAME \
143+
--name $CORE_ALIAS_NAME \
55144
--query 'FunctionVersion' \
56-
--output text)
145+
--output text 2>/dev/null)
146+
147+
if [ $? -eq 0 ] && [ "$CURRENT_CORE_VERSION" != "None" ]; then
148+
echo "Current Core alias $CORE_ALIAS_NAME points to version: $CURRENT_CORE_VERSION"
149+
export CURRENT_CORE_ALIAS_VERSION=$CURRENT_CORE_VERSION
150+
else
151+
echo "Core alias $CORE_ALIAS_NAME does not exist or could not be retrieved"
152+
export CURRENT_CORE_ALIAS_VERSION=""
153+
fi
57154
}
58155

156+
function get_current_kfd_lambda_alias {
157+
if [ -z "$KFD_ALIAS_NAME" ]; then
158+
echo "Error: KFD_ALIAS_NAME is not set"
159+
return 1
160+
fi
161+
162+
if [ -z "$LAMBDA_NAME" ]; then
163+
echo "Error: LAMBDA_NAME is not set"
164+
return 1
165+
fi
166+
167+
# Check if alias exists and get current version
168+
CURRENT_KFD_VERSION=$(aws lambda get-alias \
169+
--function-name $LAMBDA_NAME \
170+
--name $KFD_ALIAS_NAME \
171+
--query 'FunctionVersion' \
172+
--output text 2>/dev/null)
173+
174+
if [ $? -eq 0 ] && [ "$CURRENT_KFD_VERSION" != "None" ]; then
175+
echo "Current KFD alias $KFD_ALIAS_NAME points to version: $CURRENT_KFD_VERSION"
176+
export CURRENT_KFD_ALIAS_VERSION=$CURRENT_KFD_VERSION
177+
else
178+
echo "KFD alias $KFD_ALIAS_NAME does not exist or could not be retrieved"
179+
export CURRENT_KFD_ALIAS_VERSION=""
180+
fi
181+
}
59182

60183
function lambda_name {
61184
NAME=`aws ssm get-parameter \

pipeline_scripts/update-lambda-functions.sh

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,33 @@ lambda_name
1818
lambda_zipfile
1919

2020
LAMBDA_NAME="$NAME-$ENVIRONMENT"
21-
ALIAS_NAME="CoreLambda"
21+
CORE_ALIAS_NAME="CoreLambda"
2222

2323
echo
2424
print_lambda_values
2525

26+
echo
27+
echo "Getting current Core alias version for $LAMBDA_NAME"
28+
get_current_core_lambda_alias
29+
2630
echo
2731
echo "Update Lambda $LAMBDA_NAME"
2832
update_lambda
2933

34+
if [ -n "$NEW_LAMBDA_VERSION" ]; then
35+
echo
36+
echo "Updating Core alias to point to new version $NEW_LAMBDA_VERSION"
37+
38+
echo "Updating Core alias..."
39+
update_core_lambda_alias
40+
41+
echo "Core Lambda deployment completed successfully!"
42+
echo "Previous Core alias version: $CURRENT_CORE_ALIAS_VERSION"
43+
echo "New Core alias version: $NEW_LAMBDA_VERSION"
44+
else
45+
echo "Warning: No new version was created, Core alias was not updated"
46+
fi
47+
3048
# ============== Kainos Core KFD API ============
3149

3250
FunctionName="/lambda/kckfdapiname"
@@ -35,10 +53,28 @@ lambda_name
3553
lambda_zipfile
3654

3755
LAMBDA_NAME="$NAME-$ENVIRONMENT"
56+
KFD_ALIAS_NAME="KFDAPILambda"
57+
3858

3959
echo
4060
print_lambda_values
4161

62+
echo
63+
echo "Getting current KFD alias version for $LAMBDA_NAME"
64+
get_current_kfd_lambda_alias
65+
4266
echo
4367
echo "Update Lambda $LAMBDA_NAME"
44-
update_lambda
68+
update_lambda
69+
70+
if [ -n "$NEW_LAMBDA_VERSION" ]; then
71+
echo
72+
echo "Updating KFD API alias to point to new version $NEW_LAMBDA_VERSION"
73+
update_kfd_lambda_alias
74+
75+
echo "KFD API Lambda deployment completed successfully!"
76+
echo "Previous KFD API alias version: $CURRENT_KFD_ALIAS_VERSION"
77+
echo "New KFD API alias version: $NEW_LAMBDA_VERSION"
78+
else
79+
echo "Warning: No new version was created, KFD API alias was not updated"
80+
fi

0 commit comments

Comments
 (0)