How can you get the Elastic Beanstalk Application

2019-06-15 14:12发布

问题:

We want to be able to retrieve the elastic beanstalk application version in our PHP code. I don't see that EB passes it to us in any server configuration files, which I find it strange. Does anyone else know how we might be able to get this?

回答1:

At least for Docker containers - you can use the information stored in /opt/elasticbeanstalk/deploy/manifest.



回答2:

I've just been looking for a solution myself.

For now, at least, the following works:

unzip -z "${EB_CONFIG_SOURCE_BUNDLE}" | tail -n1

To elaborate,$EB_CONFIG_SOURCE_BUNDLE contains a path to the zip archive of your application (i.e. /opt/elasticbeanstalk/deploy/appsource/source_bundle). The version tag is embedded as a comment in this file.



回答3:

You can use AWS Elastic Beanstalk API to retrieve your application version information.

Describe Application Versions returns descriptions for existing application versions.

Sample request

https://elasticbeanstalk.us-east-1.amazon.com/?ApplicationName=SampleApp
&Operation=DescribeApplicationVersions
&AuthParams 

Sample response

<DescribeApplicationVersionsResponse xmlns="https://elasticbeanstalk.amazonaws.com/docs/2010-12-01/">
  <DescribeApplicationVersionsResult>
    <ApplicationVersions>
      <member>
        <SourceBundle>
          <S3Bucket>amazonaws.com</S3Bucket>
          <S3Key>sample.war</S3Key>
        </SourceBundle>
        <VersionLabel>Version1</VersionLabel>
        <Description>description</Description>
        <ApplicationName>SampleApp</ApplicationName>
        <DateCreated>2010-11-17T03:21:59.161Z</DateCreated>
        <DateUpdated>2010-11-17T03:21:59.161Z</DateUpdated>
      </member>
    </ApplicationVersions>
  </DescribeApplicationVersionsResult>
  <ResponseMetadata>
    <RequestId>773cd80a-f26c-11df-8a78-9f77047e0d0c</RequestId>
  </ResponseMetadata>
</DescribeApplicationVersionsResponse>


回答4:

Consolidating on the answers from @Georgij and @IanBlenke here are the ways you can find the version.

1. Most Reliable (Manifest)

You can sudo cat /opt/elasticbeanstalk/deploy/manifest

Output:

{"RuntimeSources":{"PLATFORM_NAME":{"app-ae22-190115_152512":{"s3url":""}}},"DeploymentId":45,"Serial":53}

2. You can look at the eb-activity logs

Secondly you could look at the eb-activity logs. This will only show you in the log line... You have to assume it's been a successful installation too..

tail /var/log/eb-activity.log | grep -i "app-.*@"



回答5:

While the best way is really to ask AWS directly:

aws elasticbeanstalk describe-environments  | \
    jq -r '.Environments | .[] | .EnvironmentName + " " + .VersionLabel'

I've had limited success deducing the same 4 or 5 digit hash by using:

git rev-parse --short=4 $(git log -1 --pretty=format:%h)


回答6:

In PHP application, you can get it using

aws elasticbeanstalk describe-environments --environment-names <environment-name>

You should add the below environment variables in the php script to get it working.

AWS_DEFAULT_REGION AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY

I have used putenv() function to set the environment variables and shell_exec() to get the json output. Parsed the json output to get the VersionLabel which is the actual application version.



回答7:

tail /var/log/eb-activity.log | grep -i "\[Application update .*\] : Completed activity." | tail -1 | sed -E 's/.*Application update (.*)@.*/\1/'

Outputs the actual app version ID like app-2.15.0-31-gf4a2918 in our case.

This works from inside any EB EC2, requires no API hit or git repo (some deploy by zip). Useful for sending a notification about a recent deployment.