Retrieving Id elasticbeanstalk (EBS) environment in the terminal?

How do I get the Id of my elasticbeanstalk (EBS) environment in the terminal?

This command returns an object in the terminal with some properties for the environment: aws elasticbeanstalk describe-environments --environment-names my-env

Is it possible to get only the EnvironmentId from that object in the terminal?

{
    "Environments": [
        {
            "ApplicationName": "xxxx-xxxx-xxxx-xxxxx", 
            "EnvironmentName": "my-env", 
            "VersionLabel": "Initial Version", 
            "Status": "Ready", 
            "EnvironmentArn": "arn:aws:elasticbeanstalk:eu-central-1:xxxxxxx:environment/xxxx-xxxxx-xxxx-xxxx/my-env", 
            "EnvironmentLinks": [], 
            "PlatformArn": "arn:aws:elasticbeanstalk:eu-central-1::platform/Multi-container Docker running on 64bit Amazon Linux/2.11.0", 
            "EndpointURL": "awseb-e-2-xxxxx-xxxxxx-xxxxx.eu-central-1.elb.amazonaws.com", 
            "SolutionStackName": "64bit Amazon Linux 2018.03 v2.11.0 running Multi-container Docker 18.03.1-ce (Generic)", 
            "EnvironmentId": "e-1234567", 
            "CNAME": "my-env.elasticbeanstalk.com", 
            "AbortableOperationInProgress": false, 
            "Tier": {
                "Version": "1.0", 
                "Type": "Standard", 
                "Name": "WebServer"
            }, 
            "Health": "Green", 
            "DateUpdated": "2018-07-12T06:10:17.056Z", 
            "DateCreated": "2018-07-11T20:03:26.970Z"
        }
    ]
}

In this case, the result that I'm expecting in my terminal to appear is e-1234567

1 answer

  • answered 2018-07-12 19:15 progfan

    1. If you want to use the AWS CLI for this, you would need to filter the output of aws elasticbeanstalk describe-environments --environment-names my-env using a tool such as grep. One possible (by no means optimal/concise) solution :

      aws elasticbeanstalk describe-environments --environment-names my-env | grep EnvironmentId | grep -Eo "e-[A-Za-z0-9_]+"
      
    2. The better solution is to use an AWS SDK such as boto3 (the Python AWS SDK).

      import boto3
      
      
      elasticbeanstalk = boto3.client(
          'elasticbeanstalk',
          region_name='us-west-2'
      )
      response = elasticbeanstalk.describe_environments(
          EnvironmentNames=['my-env']
      )
      
      if response['Environments']:
          print(response['Environments'][0]['EnvironmentId'])
      

    AWS SDKs are available in other popular languages such as Go, Java, Ruby, JavaScript, PHP as well.