@@ -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-
60122function lambda_name {
61123 NAME=` aws ssm get-parameter \
62124 --name $FunctionName \
0 commit comments