[英]How does one use the tags option when creating resources with the aws cli?
Trying to create a tagged item with aws cli.尝试使用 aws cli 创建标记项目。
aws ec2 create-security-group --group-name $group_name --description $my_description
I can't parse the documentation我无法解析文档
--tag-specifications (list)
I tried with我试过
--tag-specifications KEY=Name,Value=$something
and --tags Key=Name,Value=$something
and --tags [Key=Name,Value=$something]
and --tags {[Key=Name,Value=$something]}
We need to pass two properties我们需要传递两个属性
ResourceType: in this case security-group
ResourceType:在这种情况下是
security-group
Tags: array of key-value object标签: 键值数组 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}]'
too late to the post but my two cents..发帖太晚了,但我的两分钱..
To create a resource with the AWS CLI and specify tags, you can use the --tags option.要使用 AWS CLI 创建资源并指定标签,您可以使用 --tags 选项。 This option takes a list of tags in the form Key=Value.
此选项采用 Key=Value 形式的标签列表。 You can specify multiple tags by separating them with a space.
您可以通过用空格分隔它们来指定多个标签。
Here's an example of how to use the --tags option to create an Amazon EC2 instance with the AWS CLI:以下是如何使用 --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
This command will create an EC2 instance with the specified AMI ID, instance type, key pair, security group, and su.net, and it will also apply the tags Environment=Production and Application=MyApp to the instance.此命令将创建一个具有指定 AMI ID、实例类型、密钥对、安全组和 su.net 的 EC2 实例,它还会将标签 Environment=Production 和 Application=MyApp 应用于该实例。
You can also specify tags when creating other resource types with the AWS CLI, such as Amazon S3 buckets, Amazon RDS databases, and so on.您还可以在使用 AWS CLI 创建其他资源类型时指定标签,例如 Amazon S3 存储桶、Amazon RDS 数据库等。 The syntax for specifying tags is the same for all resource types.
指定标签的语法对于所有资源类型都是相同的。
OR要么
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}]'
In this example, the --tag-specifications
option is used to specify two tags for the instance: one with the key Name
and the value MyInstance
, and another with the key Environment
and the value Production
.在此示例中,
--tag-specifications
选项用于为实例指定两个标签:一个具有键Name
和值MyInstance
,另一个具有键Environment
和值Production
。
Note that the --tags
option is supported by many AWS resources and services, but the exact syntax and usage may vary depending on the specific resource or service.请注意,许多 AWS 资源和服务都支持
--tags
选项,但确切的语法和用法可能因特定资源或服务而异。 Consult the AWS CLI documentation or the documentation for the specific resource or service you are working with for more information.有关更多信息,请参阅 AWS CLI 文档或您正在使用的特定资源或服务的文档。
For more information about using tags with the AWS CLI, you can refer to the AWS CLI documentation.有关在 AWS CLI 中使用标签的更多信息,您可以参考 AWS CLI 文档。
Another distinct way of doing it in case you have to assign multiple key values:如果您必须分配多个键值,另一种不同的方法是:
First, create a JSON file with the tags you want to apply to your security group.首先,创建一个 JSON 文件,其中包含要应用于安全组的标签。 For example:
例如:
$ vi tags.json { "Tags": [ { "Key": "Name", "Value": "MySecurityGroup" }, { "Key": "Environment", "Value": "Production" } ] }
now, use the aws ec2 create-security-group command with the --tags file://<path_to_tags_file>
option, example below...现在,将 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.