AWSCLIでEC2 instance起動時に同時にタグをつける方法としては、instance起動してinstance-idを取得しておいて、パイプでつないでtagをつけたり、スクリプトの中で後でタグ付けする方法があったと思います。
http://kurochan-note.hatenablog.jp/entry/2017/01/08/220155
AWSCLI EC2 Run-Instanceのなかに--tag-specificationsというoptionが入って、run-instancesの中でタグが作成できるようになりました。地味なアップデートかもしれませんが、結構うれしいです。
instanceの詳細はjsonに記述して、下記のように指定して実行します。
aws ec2 run-instances --cli-input-json file://instance.json
EC2は山ほど設定項目があるので、generate-cli-skeltonでフォーマットを出力して、必要な項目だけ入力して、不必要なものは消すとinstanceの詳細を記述したjsonの完成です。Gitにでも入れておきましょう。
http://docs.aws.amazon.com/cli/latest/userguide/generate-cli-skeleton.html
aws ec2 run-instances --generate-cli-skeleton
Instanceの設定詳細を記述したjsonサンプル
{
"ImageId": "<image-id>",
"KeyName": "<my-key>",
"SecurityGroupIds": [
"<my-sgid>"
],
"InstanceType": "<instance-type>",
"BlockDeviceMappings": [
{
"VirtualName": "Root",
"DeviceName": "/dev/sda1",
"Ebs": {
"VolumeSize": 100,
"DeleteOnTermination": true,
"VolumeType": "gp2"
}
}
],
"Monitoring": {
"Enabled": false
},
"SubnetId": "<subnet-id>",
"DisableApiTermination": false,
"IamInstanceProfile": {
"Name": "<instance-iam-role>"
},
"TagSpecifications":[
{
"ResourceType": "instance",
"Tags": [
{
"Key": "Name",
"Value": "<server-name>"
},
{
"Key": "ClusterName",
"Value": "<cluster-name>"
},
{
"Key": "Application",
"Value": "<myapp>"
},
{
"Key": "CostCenter",
"Value": "<my-cost-center>"
},
{
"Key": "Environment",
"Value": "Test"
},
{
"Key": "User",
"Value": "<user-name>"
}
]
},
{
"ResourceType": "volume",
"Tags": [
{
"Key": "Device",
"Value": "<device-name>"
},
{
"Key": "CostCenter",
"Value": "<my-cost-center>"
},
{
"Key": "backup_key",
"Value": "true"
}
]
}
]
}