aws clis を使用して ec2 インスタンスを起動するときにタグを追加する方法

aws clis を使用して ec2 インスタンスを起動するときにタグを追加する方法

CLI を使用して ec2 にインスタンスを作成しようとしています。CLI を使用してインスタンスを作成するときに、インスタンスにタグを指定する方法はありますか?

aws ec2 run-instances --image-id $ami_id --key-name $deployment_key_name \
--region $region --security-groups default --instance-type m4.large \
--user-data file://./yaml/master.yaml

答え1

2017 年 3 月 28 日現在、run-instancesコマンドの一部としてインスタンス (および接続されたボリューム) のタグを指定できます。

例:

aws ec2 run-instances --image-id ami-abc12345 --count 1 \
--instance-type t2.micro --key-name MyKeyPair \
--subnet-id subnet-6e7f829e \
--tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]' 

発表ブログ投稿: https://aws.amazon.com/blogs/aws/new-tag-ec2-instances-ebs-volumes-on-creation/

追加ドキュメント(例4を参照): http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI

答え2

使用aws ec2 タグの作成その後、インスタンス ID でタグを追加するコマンドを実行します。

答え3

これが私の提案とそれを検証する方法です:

私はプロファイル名 ATeam で SSO を使用しています。設定ファイルと資格情報ファイルでこれを適宜更新する必要があります。

インスタンスを作成します。

aws ec2 run-instances \
 --image-id ami-0578f2b35d0328762 \
 --instance-type t2.micro \
 --security-group-ids sg-065d064965244f9a0 \
 --subnet-id subnet-00d93c6b00c0a6ccb \
 --key-name devass \
 --user-data file://ec2-user-data-web-app.md \
 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=pub_1},{Key=Group,Value=devass},{Key=LifeCycle,Value=development}]' \
 --count 1 \
 --profile ATeam

重要なインスタンス情報を確認してください。申し訳ありませんが、クエリを複数行に分割する方法が見つからなかったため、クエリを 1 行に配置する必要があります。

  aws ec2 describe-instances  --query "Reservations[*].Instances[*].{Instance:InstanceId,PublicIP:PublicIpAddress,Name:Tags[?Key=='Name']|[0].Value,Group:Tags[?Key=='Group']|[0].Value,LifeCycle:Tags[?Key=='LifeCycle']|[0].Value,Status:State.Name}" \
  --profile ATeam \
  --output table

結果: describe-instances のコンソール出力

関連情報