Bootstrap FreeKB - Amazon Web Services (AWS) - List EC2 key pairs using the AWS CLI
Amazon Web Services (AWS) - List EC2 key pairs using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

The aws ec2 describe-key-pairs command can then be used to list the EC2 key pairs that have been created. An EC2 key pair is typically used to make an SSH connection onto your EC2 instances.

~]$ aws ec2 describe-key-pairs
{
    "KeyPairs": [
        {
            "KeyPairId": "key-0551234a925abcd7a",
            "KeyFingerprint": "86:0e:bb:db:6b:78:7c:67:0e:b1:91:38:0a:ce:95:71:4d:2b:72:2e",
            "KeyName": "default",
            "KeyType": "rsa",
            "Tags": []
        },
        {
            "KeyPairId": "key-04582539453c591c1",
            "KeyFingerprint": "b6:eb:8a:a0:5e:4e:a6:0b:66:3c:3d:93:6d:42:63:d7:f5:32:23:71",
            "KeyName": "my-key",
            "KeyType": "rsa",
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "my-key"
                }
            ]
        }
    ]
}

 

And here is an example of how to limit the output using the --query option.

aws ec2 describe-key-pairs --query 'KeyPairs[?KeyName==`default`]'

 

And here is an example of how to limit the output using the --filters option. This will only work if the key has Tags.

aws ec2 describe-key-pairs --filters "Name=tag-value,Values=my-key"

 

The aws ec2 create-key-pair command can be used to create a key pair that can then be used to make an SSH connection onto your EC2 instances.

 ~]$ aws ec2 create-key-pair --key-name my-key
{
    "KeyFingerprint": "b6:eb:8a:a0:5e:4e:a6:0b:66:3c:3d:93:6d:42:63:d7:f5:32:23:71",
    "KeyMaterial": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA4ASycG6bvaPyUdDBiOgJ+EW/LWNtO0P7hQiCm8nOh7GAxL40\nMD4nkBejZ0CgoGXRcUOxF0CzBssrVv5d9tSlgVP16Oc3EbfVKWIWnChN5asDy8EP\n7t3Pe1/6dLA4B5iRbVcQV3FZ9NypujS/PUdgKpuhCWt36uq4ncAWlrbqjJuHOJrc\nDPu6d5yuF0eu8n975+3zbf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IEdkx+BV/tfFQs0ugg4xuK\nldoGrnumS1cWRwH+eLyYZQGn/WcUa0+Dvx6XvwfZKEP1XWJO/yA6iVXxwCaB38VT\ngcHRAJMrVvFUlOD8k1mGY0UeRe0jQ1w3ILr94WmOYn8kQx8b9z9BIASjByXQ2II1\nq2YFR3sCgYEA0euiepqA8HXCaaZ2HD8cKXK5cNZKBwa/UlEmB7O54g8rkGaQYq2A\namBZWIMi7EiffDSQWPKpnY3+0m46j4wAmGZXz7OTcLAIYDOqvXSVQv22GYpBNu6e\nSNUGOJ7r54g0d9KL/OAGX6thc4nLbhW9EUfhmy8MnLrz7vhOHj6oGbI=\n-----END RSA PRIVATE KEY-----",
    "KeyName": "my-key",
    "KeyPairId": "key-04582539453c591c1"
}

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 073ada in the box below so that we can be sure you are a human.