Reputation: 171
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"
]
]
Upvotes: 12
Views: 10544
Reputation: 38335
If your lambda is always published using a Lambda function alias pointing to your latest version, then the cli query can be reduced to a single call w/o any pagination using the get-alias command.
The following are placeholders; replace with your values as-needed:
[FunctionName]
[AliasName]
[AwsAccountNumber]
(nothing to replace; just a placeholder)Full query:
aws lambda get-alias \
--function-name [FunctionName] \
--name [AliasName]
Result:
{
"AliasArn": "arn:aws:lambda:us-east-1:[AwsAccountNumber]:function:[FunctionName]:[AliasName]",
"Name": "[AliasName]",
"FunctionVersion": "15",
"Description": "Yada Yada Yada",
"RevisionId": "7aa54e5c-1b32-4436-abcd-cf0a1aacad5a"
}
Reduced query to return only the version number:
aws lambda get-alias \
--function-name [FunctionName] \
--name [AliasName] \
--query "to_number(FunctionVersion)"
Payload:
15
Upvotes: -1
Reputation: 11810
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'))"
Upvotes: 12
Reputation: 59956
You can get all the version using list-versions-by-function
and then extract second last with jq
aws lambda list-versions-by-function --function-name LogsToElasticsearch_goabode --max-items 5 \
--query 'Versions[*].[Version,FunctionName]' | jq '.[-1]'
Or to get all with name
aws lambda list-versions-by-function --function-name my_lambda \
--max-items 5 --query 'Versions[*].[Version,FunctionName]'
Upvotes: 2