Question

From within a running Java application running on beanstalk, how can I get the beanstalk version label that is currently running?

[Multiple Edits later...]

After a few back-and-forth comments with Sony (see below), I wrote the following code which works for me now. If you put meaningful comments in your version label when you deploy, then this will tell you what you're running. We have a continuous build environment, so we can get our build environment to supply a label that leads to the check-in comments for the related code. Put this all together, and your server can tell you exactly what code its running relative to your source code check-ins. Really useful for us. OK now I'm actually answering my own question here, but with invaluable help from Sony. Seems a shame you can't remove the hard-coded values and query for those at runtime.

String getMyVersionLabel() throws IOException {
    Region region = Region.getRegion(Regions.fromName("us-west-2")); // Need to hard-code this

    AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
    AWSElasticBeanstalkClient beanstalk = region.createClient(AWSElasticBeanstalkClient.class, credentialsProvider, null);

    String environmentName = System.getProperty("PARAM2", "DefaultEnvironmentName"); // Need to hard-code this too

    DescribeEnvironmentsResult environments = beanstalk.describeEnvironments();
    for (EnvironmentDescription ed : environments.getEnvironments()) {
        if (ed.getEnvironmentName().equals( environmentName)) {
            return "Running version " + ed.getVersionLabel() + " created on " + ed.getDateCreated());
            break;
        }
    }
    return null;
}
Was it helpful?

Solution

You can use AWS Java SDK and call this directly.

See the details of describeApplicationVersions API for how to get all the versions in an application.Ensure to give your regions as well (otherwise you will get the versions from the default AWS region).

Now, if you need to know the version deployed currently, you need to call additionally the DescribeEnvironmentsRequest. This has the versionLabel, which tells you the the version currently deployed.

Here again, if you need to know the environment name in the code, you need to pass it as a param to the beanstalk configuration in the aws console, and access as a PARAM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top