簡體   English   中英

使用 aws cli 創建資源時如何使用標簽選項?

[英]How does one use the tags option when creating resources with the aws cli?

嘗試使用 aws cli 創建標記項目。

aws ec2 create-security-group --group-name $group_name --description $my_description

我無法解析文檔

--tag-specifications (list)

我試過

--tag-specifications KEY=Name,Value=$something

and --tags Key=Name,Value=$something
and --tags [Key=Name,Value=$something]
and --tags {[Key=Name,Value=$something]}

我們需要傳遞兩個屬性

  • ResourceType:在這種情況下是security-group

  • 標簽: 鍵值數組 object

     aws ec2 create-security-group --group-name 'test' --description 'test desc' --tag-specifications 'ResourceType=security-group,Tags=[{Key=purpose,Value=production},{Key=cost-center,Value=cc123}]'

發帖太晚了,但我的兩分錢..

要使用 AWS CLI 創建資源並指定標簽,您可以使用 --tags 選項。 此選項采用 Key=Value 形式的標簽列表。 您可以通過用空格分隔它們來指定多個標簽。

以下是如何使用 --tags 選項通過 AWS CLI 創建 Amazon EC2 實例的示例:

$ aws ec2 run-instances --image-id ami-12345678 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-12345678 --subnet-id subnet-12345678 --tags Key=Environment,Value=Production Key=Application,Value=MyApp

此命令將創建一個具有指定 AMI ID、實例類型、密鑰對、安全組和 su.net 的 EC2 實例,它還會將標簽 Environment=Production 和 Application=MyApp 應用於該實例。

您還可以在使用 AWS CLI 創建其他資源類型時指定標簽,例如 Amazon S3 存儲桶、Amazon RDS 數據庫等。 指定標簽的語法對於所有資源類型都是相同的。

要么

aws ec2 run-instances \
    --image-id ami-01234567890abcdef \
    --instance-type t2.micro \
    --key-name MyKeyPair \
    --security-group-ids sg-01234567890abcdef \
    --subnet-id subnet-01234567890abcdef \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyInstance},{Key=Environment,Value=Production}]'

在此示例中, --tag-specifications選項用於為實例指定兩個標簽:一個具有鍵Name和值MyInstance ,另一個具有鍵Environment和值Production

請注意,許多 AWS 資源和服務都支持--tags選項,但確切的語法和用法可能因特定資源或服務而異。 有關更多信息,請參閱 AWS CLI 文檔或您正在使用的特定資源或服務的文檔。

有關在 AWS CLI 中使用標簽的更多信息,您可以參考 AWS CLI 文檔。

如果您必須分配多個鍵值,另一種不同的方法是:

  1. 首先,創建一個 JSON 文件,其中包含要應用於安全組的標簽。 例如:

     $ vi tags.json { "Tags": [ { "Key": "Name", "Value": "MySecurityGroup" }, { "Key": "Environment", "Value": "Production" } ] }
  2. 現在,將 aws ec2 create-security-group 命令與--tags file://<path_to_tags_file>選項一起使用,示例如下...

aws ec2 create-security-group --group-name MySecurityGroup --description "My security group" --tags file:///path/to/tags.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM