简体   繁体   中英

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

Trying to create a tagged item with 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

  • Tags: array of key-value 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. This option takes a list of tags in the form 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:

$ 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.

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. 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 .

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. Consult the AWS CLI documentation or the documentation for the specific resource or service you are working with for more information.

For more information about using tags with the AWS CLI, you can refer to the AWS CLI documentation.

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. 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 --group-name MySecurityGroup --description "My security group" --tags file:///path/to/tags.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM