要在终端中检索Id Elasticbeanstalk(EBS)环境?

时间:2018-07-12 06:23:51

标签: amazon-web-services terminal elastic-beanstalk

如何在终端中获取我的Elasticbeanstalk(EBS)环境的ID?

此命令在终端中返回一个具有环境属性的对象: aws elasticbeanstalk describe-environments --environment-names my-env

是否可以从终端中的该对象仅获取EnvironmentId

{
    "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"
        }
    ]
}

在这种情况下,我期望在终端中出现的结果是e-1234567

1 个答案:

答案 0 :(得分:0)

  1. 如果要为此使用AWS CLI,则需要使用诸如aws elasticbeanstalk describe-environments --environment-names my-env之类的工具来过滤grep的输出。一种可能的方法(绝非最佳/简洁):

    aws elasticbeanstalk describe-environments --environment-names my-env | grep EnvironmentId | grep -Eo "e-[A-Za-z0-9_]+"
    
  2. 更好的解决方案是使用AWS软件开发工具包,例如boto3(Python AWS开发工具包)。

    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 SDK还可使用其他流行语言,例如Go,Java,Ruby,JavaScript,PHP。