简体   繁体   English

使用 aws cli 创建资源时如何使用标签选项?

[英]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:如果您必须分配多个键值,另一种不同的方法是:

  1. 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" } ] }
  2. 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.

相关问题 如何使用 aws ec2 create-tags(来自 aws cli)跨多个资源为同一标签赋予不同的值? - How to use aws ec2 create-tags (from aws cli) to give different values to the same tag across multiple resources? 如何将 MFA 与 AWS CLI 结合使用? - How to use MFA with AWS CLI? 为什么 AWS CLI 在 darwin (mac) 和 linux 上使用不同的 AWS 服务? rds describe-db-instances 命令什么时候使用STS? - Why the does AWS CLI use different AWS services on darwin (mac) vs. linux? When does the rds describe-db-instances command use STS? 如何将 AWS CLI 与 Digital Ocean Spaces 结合使用? - How to use AWS CLI with Digital Ocean Spaces? 如何在不使用 AWS CLI 删除现有标签的情况下将标签添加到 S3 存储桶? - How to add tags to an S3 Bucket without deleting the existing tags using AWS CLI? 使用实例配置文件时如何在 AWS CLI 中设置配置文件名称 - How to set profile name in AWS CLI when using instance profile 使用 aws cli 创建云端分配 json - Creating a cloudfront distribution using aws cli json 为什么在我设置区域后 AWS CLI 报告“Credential should be scoped to a valid region”? - Why does the AWS CLI report `Credential should be scoped to a valid region` when I have set a region? 如何使用 JMESPath 通过 DBInstanceIdentifier 查询 AWS CLI RDS 实例 - How to use JMESPath to query AWS CLI RDS instances by DBInstanceIdentifier ECS 任务。 如何在容器中使用 AWS CLI - ECS task. How to use AWS CLI within container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM