I see two potential ways. Although I must say I don't understand why this is not easier given this is a pretty legitimate use case, but it looks like it's not supported as of writing by AWS.
Solution 1: generated version label
Generate the version label on your side (for instance, with the commit hash), then make it part of your code.
For instance in your Makefile
:
VERSION=$(shell git rev-parse --short HEAD)
deploy: requirements.txt
echo $(VERSION) > version.txt
eb deploy --label $(VERSION)
.PHONY: deploy
Then you can just read this file from the instance. There are some other options, for instance using sed
to put it as a variable in one of your file.
Solution 2: get it from an undocumented file (unstable)
I tried to find this metadata on the EC2 instance, and could find it in a file that is unfortunately owned by root:
[root@... ec2-user]# cat /opt/elasticbeanstalk/deploy/manifest
{"RuntimeSources":{"yourappname":{"app-VERSION":{"s3url":""}}},"DeploymentId":24,"Serial":26}
[ec2-user@... ~]$ ls -la /opt/elasticbeanstalk/deploy/manifest
-rw-rw---- 1 root awseb 98 Mar 22 17:57 /opt/elasticbeanstalk/deploy/manifest
I'm not sure if you could do this, but you could have a post deployment command that chown or copies this file in a place where you can read it. I might try that and let you know if it worked.
A similar question has been asked here: How can you get the Elastic Beanstalk Application Version in your application? (evidently I found it only after writing the above).