ようへいの日々精進XP

よかろうもん

俺の郷 〜 EC2 のタグを付けたり、外したり 〜

引き続き

出張帰りの飛行機内で

AWS SDK for Go を学んでおります。

飛行機内では

当然、インターネットは利用出来ませんので、moto を使って擬似的な API エンドポイントを実行して検証します。

github.com

moto に同梱されている moto_server を利用します。

moto_server ec2

これだけで EC2 の API エンドポイントが起動するので、AWS CLI から以下のようにアクセスします。

#
# インスタンス起動
#
aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 run-instances \
      --image-id ami-1a2b3c4d \
      --count 1 \
      --instance-type c3.large \
      --key-name MyKeyPair \
      --security-groups MySecurityGroup

エンドポイントはデフォルトでローカルホストのポート 5000 番で起動します。

ということで、今回は

EC2 インスタンスにタグを付けたり、外したりしてみたいと思います。

出来たもの

gist.github.com

以下のように使います

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1421

$ go version
go version go1.8.3 darwin/amd64

ヘルプ

$ ./tagCtrl -h
Usage of ./tagCtrl:
  -add
        タグをインスタンスに付与.
  -del
        タグをインスタンスから削除.
  -endpoint string
        AWS API のエンドポイントを指定.
  -instances string
        Instance ID 又は Instance Tag 名を指定.
  -profile string
        Profile 名を指定.
  -region string
        Region 名を指定. (default "ap-northeast-1")
  -tags string
        Tag Key(Key=) 及び Tag Value(Value=) を指定.

タグの付与

実行。

$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga' -add

確認。

$ aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 describe-instances \
      --query 'Reservations[].Instances[].Tags[]' \
      --output table
--------------------------
|    DescribeInstances   |
+-----------+------------+
|    Key    |   Value    |
+-----------+------------+
|  hogehoge |  fugafuga  |
+-----------+------------+

複数のタグを付与。

$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -add

確認。

$ aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 describe-instances \
      --query 'Reservations[].Instances[].Tags[]' \
      --output table
--------------------------
|    DescribeInstances   |
+-----------+------------+
|    Key    |   Value    |
+-----------+------------+
|  hogehoge |  fugafuga  |
|  hage     |  atama     |
+-----------+------------+

複数のインスタンスに複数のタグを付与。

$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175,i-e8b89e8b -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -add

確認。

$ aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 describe-instances \
      --query 'Reservations[].Instances[].{InstanceID:InstanceId,Tags:Tags}' \
      --output table
----------------------------
|     DescribeInstances    |
+--------------------------+
|        InstanceID        |
+--------------------------+
|  i-e7f4f175              |
+--------------------------+
||          Tags          ||
|+-----------+------------+|
||    Key    |   Value    ||
|+-----------+------------+|
||  hogehoge |  fugafuga  ||
||  hage     |  atama     ||
|+-----------+------------+|
|     DescribeInstances    |
+--------------------------+
|        InstanceID        |
+--------------------------+
|  i-e8b89e8b              |
+--------------------------+
||          Tags          ||
|+-----------+------------+|
||    Key    |   Value    ||
|+-----------+------------+|
||  hogehoge |  fugafuga  ||
||  hage     |  atama     ||
|+-----------+------------+|

タグの削除

実行。

$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175 -tags='Key=hogehoge,Value=fugafuga Key=hage,Value=atama' -del

確認。

$ aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 describe-instances \
      --query 'Reservations[].Instances[].Tags[]' \
      --output table
-------------------
|DescribeInstances|

複数のインスタンスの任意のタグだけを削除。

$ ./tagCtrl -endpoint=http://127.0.0.1:5000 -instances=i-e7f4f175,i-e8b89e8b -tags='Key=hogehoge,Value=fugafuga' -del

確認。

$ aws \
  --profile mock_profile \
  --region us-west-2 \
  --endpoint-url http://127.0.0.1:5000 \
    ec2 describe-instances \
      --query 'Reservations[].Instances[].{InstanceID:InstanceId,Tags:Tags}' \
      --output table
---------------------
| DescribeInstances |
+-------------------+
|    InstanceID     |
+-------------------+
|  i-e7f4f175       |
+-------------------+
||      Tags       ||
|+-------+---------+|
||  Key  |  Value  ||
|+-------+---------+|
||  hage |  atama  ||
|+-------+---------+|
| DescribeInstances |
+-------------------+
|    InstanceID     |
+-------------------+
|  i-e8b89e8b       |
+-------------------+
||      Tags       ||
|+-------+---------+|
||  Key  |  Value  ||
|+-------+---------+|
||  hage |  atama  ||
|+-------+---------+|

まとめ

タグやリソース等のパラメータ指定は

EC2 インスタンスの時と同様に以下の通り CreateTagsInput 関数を利用する。

func createTag(ec2Client *ec2.EC2, instances string, tags []*ec2.Tag) {
    splitedInstances := strings.Split(instances, ",")
    var instanceIds []*string
    for _, i := range splitedInstances {
        instanceIds = append(instanceIds, aws.String(i))
    }
    input := &ec2.CreateTagsInput{
        Resources: instanceIds,
        Tags: tags,
    }
    result, err := ec2Client.CreateTags(input)
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(1)
    }
    fmt.Println(result)
}

API エンドポイントの指定

クライアントの初期化時に以下のように指定したらイケた。

func awsEc2Client(profile string, region string) *ec2.EC2 {
    var config aws.Config
    if profile != "" {
        creds := credentials.NewSharedCredentials("", profile)
        config = aws.Config{Region: aws.String(region), Credentials: creds, Endpoint: aws.String(*argEndpoint)}
    } else {
        config = aws.Config{Region: aws.String(region), Endpoint: aws.String(*argEndpoint)}
    }
    sess := session.New(&config)
    ec2Client := ec2.New(sess)
    return ec2Client
}

以上

メモでした。