【问题标题】:Is it possible to tag a SSH key pair in AWS? [closed]是否可以在 AWS 中标记 SSH 密钥对? [关闭]
【发布时间】:2020-07-15 15:11:28
【问题描述】:

DescribeKeypairs 表示 KeyPair 可以有标签,但是无法通过标签或 tag_key 过滤键。另外,密钥对没有特定的类型 ID,例如 i-*vol-* 等,因此也无法使用 CreateTags 进行标记。也不可能通过网络创建。 所有这一切似乎都很令人困惑,因为没有办法创建标签但它可以被标记????

有没有办法标记密钥对?

【问题讨论】:

  • 大多数“较新”的服务都有add-tags-to-resource API,它接受资源ARN,例如arn:aws:ec2:us-east-1:123456789012:key-pair/my-keyname,但EC2 API 在这里似乎是一个例外。我猜只是因为它是 AWS 最早/旧的东西之一。
  • 本周末有人更新了 KeyPairInfo 的文档。现在一切都清楚了。感谢沉默的 AWS 员工

标签: amazon-web-services amazon-ec2 tags key-pair


【解决方案1】:

嗯...我没有亲自尝试过,但在 AWS CLI 中使用 describe-key-pairs 应该会为您提供密钥 ID。

但是,我发现标记密钥 ID 的唯一方法是使用 AWS CLI 生成它并查看输出。

如果你使用简单的命令:

aws ec2 create-key-pair --key-name my-test-key --profile my-profile

那么输出就有key id。

{
    "KeyFingerprint": "6d:5c:e0:19:de:.........",
    "KeyMaterial": "......",
    "KeyName": "my-test-key",
    "KeyPairId": "key-0ab4ff01abc9999e"
}

然后您可以使用您已经知道的create-tags 标记资源。

【讨论】:

    【解决方案2】:

    1。 ID

    从适用于 Linux 的 aws-cli 版本 2.2.11 开始,我发现密钥具有 KeyPairIds ...资源 ID

    2。关于标记键

    是的,您可以使用 ec2 create-tags。使用 create-key-pair 是很成问题的。

    aws ec2 create-tags help 是以下命令的参考。

    一般命令和参数 ...

    aws ec2 create-tags \
     --resources "string" "string" --tags Key=string,Value=string ...
    

    示例。

    首先描述我的密钥以获得资源ID...密钥对ID。

    aws ec2 describe-key-pairs --profile-name adrianteri-devops
    

    输出

    {
        "KeyPairs": [
            {
                "KeyPairId": "key-044180521638ac88d",
                "KeyFingerprint": "69:b4:71:46:65:40:37:59:7c:8c:5f:fa:c6:46:5a:e4:12:e0:46:54",
                "KeyName": "adrianteri-devops",
                "Tags": []
            },
            {
                "KeyPairId": "key-0c010638921030fdc",
                "KeyFingerprint": "21:6b:f8:05:bc:96:13:8d:ba:75:41:bb:5b:43:15:f7:9c:b0:a3:a4",
                "KeyName": "ansible-devops",
                "Tags": []
            }
            {
                "KeyPairId": "key-0d047bbc242c7e0a2",
                "KeyFingerprint": "69:2d:6f:6f:af:cf:45:d3:a9:d5:e7:63:b3:54:8b:85:32:67:9e:a0",
                "KeyName": "test-keypair",
                "Tags": []
            }
    
        ]
    }
    (END)
    
    

    现在创建标签...

    aws ec2 create-tags --resources key-044180521638ac88d key-0c010638921030fdc --tags Key=Project,Value=adrianteri-com Key=CreatedBy,Value=adrianteri --profile adrianteri-devops
    

    测试密钥对有不同的标签。

    aws ec2 create-tags --resources key-0d047bbc242c7e0a2 --tags Key=Project,Value=Test Key=CreatedBy,Value=TestBot Key=Billing,Value=QualityAssuarance --profile adrianteri-live-devops
    

    再次描述我的密钥对的结果:

    {
        "KeyPairs": [
            {
                "KeyPairId": "key-044180521638ac88d",
                "KeyFingerprint": "69:b4:71:46:65:40:37:59:7c:8c:5f:fa:c6:46:5a:e4:12:e0:46:54",
                "KeyName": "adrianteri-devops-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "adrianteri-com"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "adrianteri"
                    }
                ]
            },
            {
                "KeyPairId": "key-0c010638921030fdc",
                "KeyFingerprint": "21:6b:f8:05:bc:96:13:8d:ba:75:41:bb:5b:43:15:f7:9c:b0:a3:a4",
                "KeyName": "ansible-devops-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "adrianteri-com"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "adrianteri"
                    }
                ]
            },
            {
                "KeyPairId": "key-0d047bbc242c7e0a2",
                "KeyFingerprint": "69:2d:6f:6f:af:cf:45:d3:a9:d5:e7:63:b3:54:8b:85:32:67:9e:a0",
                "KeyName": "test-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "Test"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "TestBot"
                    },
                    {
                        "Key": "Billing",
                        "Value": "QualityAssuarance"
                    }
                ]
            }
        ]
    }
    (END)
    
    

    3。按标签过滤键

    也可以使用 --filters tag-keytag :<key>

    tag-key

    aws ec2 describe-key-pairs --filters Name=tag-key,Values=Billing --profile adrianteri-devops
    

    输出

    {
        "KeyPairs": [
            {
                "KeyPairId": "key-0d047bbc242c7e0a2",
                "KeyFingerprint": "69:2d:6f:6f:af:cf:45:d3:a9:d5:e7:63:b3:54:8b:85:32:67:9e:a0",
                "KeyName": "test-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "Test"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "TestBot"
                    },
                    {
                        "Key": "Billing",
                        "Value": "QualityAssurance"
                    }
                ]
            }
        ]
    }
    (END)
    
    

    tag :<key>

    aws ec2 describe-key-pairs --filters Name=tag:CreatedBy,Values=adrianteri -profile adrianteri-devops
    

    输出

    {
        "KeyPairs": [
            {
                "KeyPairId": "key-044180521638ac88d",
                "KeyFingerprint": "69:b4:71:46:65:40:37:59:7c:8c:5f:fa:c6:46:5a:e4:12:e0:46:54",
                "KeyName": "adrianteri-devops-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "adrianteri-com"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "adrianteri"
                    }
                ]
            },
            {
                "KeyPairId": "key-0c010638921030fdc",
                "KeyFingerprint": "21:6b:f8:05:bc:96:13:8d:ba:75:41:bb:5b:43:15:f7:9c:b0:a3:a4",
                "KeyName": "ansible-devops-keypair",
                "Tags": [
                    {
                        "Key": "Project",
                        "Value": "adrianteri-com"
                    },
                    {
                        "Key": "CreatedBy",
                        "Value": "adrianteri"
                    }
                ]
            }
        ]
    }
    (END)
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 2021-02-07
      • 2013-09-18
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2015-06-25
      相关资源
      最近更新 更多