Hongbo Miao
Hongbo Miao

Reputation: 49804

How to use AWS CLI to deploy lambda function to specific alias or version?

Before I have Lambda version and alias, based on the API update-function-code I can successfully deploy by

aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip

After I added version and alias, I have Lambda version

and alias

I try to deploy to staging (version 1),


1st Try

aws lambda update-function-code --function-name arn:aws:lambda:us-west-2:123456789000:function:myFunction:staging --zip-file fileb://archive.zip

gave error

Current operation does not support versions other than $LATEST. Please set the version to $LATEST or do not set a version in your request.


2nd Try

aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip --s3-object-version 1

or

aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip --s3-object-version staging

gave error

Please do not provide other FunctionCode parameters when providing a ZipFile.


How to use AWS CLI to deploy lambda function to specific alias or version correctly? Thanks

Upvotes: 5

Views: 4131

Answers (2)

Reza Nasiri
Reza Nasiri

Reputation: 1435

based on AWS documentations

A published version is immutable. That is, you can't change the code or configuration information.

so you need to publish a new version of your function and then update the alias to point to the newly created version.

Upvotes: 3

BTL
BTL

Reputation: 4656

Okay so assuming you already managed to deploy your lambda with this command :

aws lambda update-function-code --function-name $FUNCTION_NAME --zip-file fileb://lambda.zip

And that you have created 2 versions with 2 alias (staging an prod).

Now you just need to publish to the rigth version :

VERSION=1
aws lambda update-alias --function-name $FUNCTION_NAME --name staging --function-version $VERSION
VERSION=2
aws lambda update-alias --function-name $FUNCTION_NAME --name prod --function-version $VERSION

If you want to go one step further you can bind the last deployment to the latest version. So first you need to retrieve the latest version and for this I use jq but feel free to use whatever you want, and then update with this version.

VERSION=$(aws lambda publish-version --function-name $FUNCTION_NAME | jq -r .Version)
aws lambda update-alias --function-name $FUNCTION_NAME --name staging --function-version $VERSION

Here is the update-alias documentation. And here is the publish-version documention.

Upvotes: 14

Related Questions