Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Latest Published version of a Lambda Function in AWS CLI

How to get Latest Published version of a Lambda Function? Apart from $LATEST, how can I get the result as "5"

$ aws lambda list-versions-by-function --function-name My_Lambda_Function --query 'Versions[*][Version, FunctionArn]' --output json
[
    [
        "$LATEST",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:$LATEST"
    ],
    [
        "1",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:1"
    ],
    [
        "2",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:2"
    ],
    [
        "3",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:3"
    ],
    [
        "4",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:4"
    ],
    [
        "5",
        "arn:aws:lambda:us-east-2:123456789000:function:My_Lambda_Function:5"
    ]
]
like image 835
Nigam Rout Avatar asked Sep 25 '19 17:09

Nigam Rout


People also ask

What is publish new version in Lambda?

Lambda creates a new version of your function each time that you publish the function. The new version is a copy of the unpublished version of the function. Lambda doesn't create a new version if the code in the unpublished version is the same as the previous published version.

How do I find my Lambda creation date?

In the AWS Lambda UI you can look under Qualifiers Versions and view all the versions and the time of deployment. If you want to do this via the CLI you can get the info of all versions via: aws lambda list-versions-by-function --function-name carrierint-shp-crt-integrtr-gtwy-acceptance-api --region us-east-1 .

How do I remove old versions of Lambda?

To delete a specific function version, use the Qualifier parameter. Otherwise, all versions and aliases are deleted. To delete Lambda event source mappings that invoke a function, use DeleteEventSourceMapping.


1 Answers

I found this thread on GitHub insightful, you can get the maximum non-$LATEST item a few ways but only one of them is correct:

Wrong way, this command will be correct only as far as version 9 because it will string-compare the versions and decide that "9" is greater than "10":

aws lambda list-versions-by-function --function-name my_lambda \
  --query "max_by(Versions, &Version)"

Wrong way, this command will be correct only as far as version 50 (or whatever the page size is) because it will only filter the results that are returned in the first page.

aws lambda list-versions-by-function --function-name my_lambda \
  --query "Versions[-1]"

Wrong way, this command will consider all the results before filtering them, but still assumes that they will be in proper order

aws lambda list-versions-by-function --function-name my_lambda \
  --no-paginate \
  --query "Versions[-1]"

Wrong way, this command looks right for sorting purposes but will choke on to_number("$LATEST") returning null.

aws lambda list-versions-by-function --function-name my_lambda \
  --no-paginate \
  --query "max_by(Versions, &to_number(Version))" 

Wrong way, this command looks right for sorting purposes but (on my machine) doesn't accept the literal 0.

aws lambda list-versions-by-function --function-name my_lambda \
  --no-paginate \
  --query "max_by(Versions, &to_number(Version) || '0')"

Right way, this command will fetch all the all the results before considering them, and works around the issue of numeric literals by converting to a number twice.

aws lambda list-versions-by-function --function-name my_lambda \
  --no-paginate \
  --query "max_by(Versions, &to_number(to_number(Version) || '0'))"
like image 194
Ian Avatar answered Oct 01 '22 00:10

Ian