Skip to content
Advertisement

Get Maven Version in an AWS Lambda

I am developing an AWS Lambdas using java. It would be very useful if, in the lambda, there was a way to log the maven version of the lambda in the log file so we can track what versions of lambdas are running in different environments. Normally this could be done pretty easily using the code below:

JavaScript

Unfortunately it always comes up null for the version. I verified that the jar file has the specified path and the pom.properties is present in the jar in the correct path with the version information contained within. Any help would be greatly appreciated!

EDIT: The answer has to be specific to running the java jar as an aws lambda. Getting it out of a jar is normally pretty trivial using the code above, but when the jar is run as an aws lambda it doesn’t seem to be able to access the various the maven files to get the version information.

NOTE on the answer: AWS sam build takes the built jars and deploys only certain files to be available to the lambda. You can see this when you run your lambda in an IDE and the aws-toolkit creates the .aws-sam/build folder. Unlike in typical jars, only the lambda class file and any resources defined in the pom.xml with the resources tag are copied here. I used the suggestion to use the git-commit-id-plugin to create a git.properties file with version information, which is created in the root of the project. To make it so aws knows to copy this file also, you need to modify the pom.xml to include the git.properties file as shown below:

JavaScript

Advertisement

Answer

If you use Git, I do the following. In my pom.xml I have (in the build/plugins section):

JavaScript

In my Java code I have:

JavaScript

And in my Lambda I have:

JavaScript

Obviously you can get whatever information from the git-properties file. It looks something like:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement