Skip to content

Commit 5ba9a8b

Browse files
authored
feat: updated Deployment Scripts to Add Lambda Alias and Versioning (#42)
* feat: updated Deployment Scripts to Add Lambda Alias and Versioning ---------
1 parent 09ad513 commit 5ba9a8b

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

pipeline_scripts/functions.sh

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,87 @@ 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_lambda_alias {
60+
if [ -z "$LAMBDA_NAME" ]; then
61+
echo "Error: LAMBDA_NAME is not set"
62+
return 1
63+
fi
64+
65+
if [ -z "$ALIAS_NAME" ]; then
66+
echo "Error: ALIAS_NAME is not set"
67+
return 1
68+
fi
69+
70+
if [ -z "$NEW_LAMBDA_VERSION" ]; then
71+
echo "Error: NEW_LAMBDA_VERSION is not set"
72+
return 1
73+
fi
74+
75+
# Check if alias exists
76+
aws lambda get-alias --function-name $LAMBDA_NAME --name $ALIAS_NAME > /dev/null 2>&1
77+
if [ 0 -eq $? ]; then
78+
echo "Updating existing alias $ALIAS_NAME to point to version $NEW_LAMBDA_VERSION"
79+
aws lambda update-alias \
80+
--function-name $LAMBDA_NAME \
81+
--name $ALIAS_NAME \
82+
--function-version $NEW_LAMBDA_VERSION \
83+
--output text
84+
else
85+
echo "Creating new alias $ALIAS_NAME pointing to version $NEW_LAMBDA_VERSION"
86+
aws lambda create-alias \
87+
--function-name $LAMBDA_NAME \
88+
--name $ALIAS_NAME \
89+
--function-version $NEW_LAMBDA_VERSION \
90+
--output text
91+
fi
92+
}
93+
94+
function get_current_lambda_alias {
95+
if [ -z "$LAMBDA_NAME" ]; then
96+
echo "Error: LAMBDA_NAME is not set"
97+
return 1
98+
fi
99+
100+
if [ -z "$ALIAS_NAME" ]; then
101+
echo "Error: ALIAS_NAME is not set"
102+
return 1
103+
fi
104+
105+
# Check if alias exists and get current version
106+
CURRENT_VERSION=$(aws lambda get-alias \
53107
--function-name $LAMBDA_NAME \
54108
--name $ALIAS_NAME \
55109
--query 'FunctionVersion' \
56-
--output text)
110+
--output text 2>/dev/null)
111+
AWS_EXIT_CODE=$?
112+
113+
if [ $AWS_EXIT_CODE -eq 0 ] && [ "$CURRENT_VERSION" != "None" ]; then
114+
echo "Current alias $ALIAS_NAME points to version: $CURRENT_VERSION"
115+
export CURRENT_ALIAS_VERSION=$CURRENT_VERSION
116+
else
117+
echo "Alias $ALIAS_NAME does not exist or could not be retrieved"
118+
export CURRENT_ALIAS_VERSION=""
119+
fi
57120
}
58121

59-
60122
function lambda_name {
61123
NAME=`aws ssm get-parameter \
62124
--name $FunctionName \

pipeline_scripts/update-lambda-functions.sh

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,27 @@ ALIAS_NAME="CoreLambda"
2323
echo
2424
print_lambda_values
2525

26+
echo
27+
echo "Getting current Core alias version for $LAMBDA_NAME"
28+
get_current_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+
update_lambda_alias
39+
40+
echo "Core Lambda deployment completed successfully!"
41+
echo "Previous Core alias version: $CURRENT_ALIAS_VERSION"
42+
echo "New Core alias version: $NEW_LAMBDA_VERSION"
43+
else
44+
echo "Warning: No new version was created, Core alias was not updated"
45+
fi
46+
3047
# ============== Kainos Core KFD API ============
3148

3249
FunctionName="/lambda/kckfdapiname"
@@ -35,10 +52,28 @@ lambda_name
3552
lambda_zipfile
3653

3754
LAMBDA_NAME="$NAME-$ENVIRONMENT"
55+
ALIAS_NAME="KFDAPILambda"
56+
3857

3958
echo
4059
print_lambda_values
4160

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

0 commit comments

Comments
 (0)