Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get AWS EMR Cluster ID from Name

AWS CLI command aws emr list-clusters returns the following json. Is there a way through bash or groovy that I can use the Name to get the Id? I can't just use the Id becauseI am deleting clusters and rebuilding them with only the same name. So I know the name of the cluster is "my-cluster" and I would like to use it somehow to get the Id of the cluster. End case is I actually want the Master public DNS of the cluster.

    {
        "Clusters": [
        {
            "Status": {
                "Timeline": {
                "ReadyDateTime": 1433200405.353,
                "CreationDateTime": 1433199926.596
            },
            "State": "WAITING",
            "StateChangeReason": {
                "Message": "Waiting after step completed"
                }
            },
            "NormalizedInstanceHours": 6,
            "Id": "j-3SD91U2E1L2QX",
            "Name": "my-cluster"
        },
        {
            "Status": {
                "Timeline": {
                    "ReadyDateTime": 1433200405.353,
                    "CreationDateTime": 1433199926.596
            },
            "State": "WAITING",
            "StateChangeReason": {
                "Message": "Waiting after step completed"
            }
        },
        "NormalizedInstanceHours": 6,
        "Id": "j-3S281UEK8L2LW",
        "Name": "my-cluster2"
        }
    ]
    }
like image 617
KBouldin9 Avatar asked Dec 11 '22 08:12

KBouldin9


2 Answers

You can use the query parameter to achieve what you are trying. The command will look like below:

aws emr list-clusters --query 'Clusters[?Name==`my-cluster`].Id' --output text

You can find the complete documentation for the query parameter here.

like image 88
krishna_mee2004 Avatar answered Dec 29 '22 14:12

krishna_mee2004


To answer the complete question, one could put the above approach into a long one-liner over several lines.

aws emr describe-cluster                                       \
 --output text                                                 \
 --cluster-id  $(aws emr list-clusters                         \
                  --active                                     \
                  --query 'Clusters[?Name==`my-cluster`].Id'   \
                  --output text)                               \
 --query Cluster.MasterPublicDnsName
like image 22
eeasterly Avatar answered Dec 29 '22 13:12

eeasterly