1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

awscliでパラメータストアから取得したファイル名に書き込めなくなった時の対処

Posted at

はじまり

パラメータストアに格納したファイル名のファイルにデータを書き込む

ということをawscliを使ってやっていました。

put.sh
# !/bin/bash

FILE=$(aws ssm get-parameters --name "FILE_NAME" --output=text --query "Parameters[].Value" --with-decryption)
echo "aaaa" >> ${FILE}

ある日、実行すると・・

曖昧なリダイレクトです、となってファイルに書き込めません。

.bash
$ ./put.sh
./put.sh: 行 4: ${FILE}: 曖昧なリダイレクトです

パラメータストアに設定した値を確認すると問題なさそうです。

$ aws --version
aws-cli/2.0.6 Python/3.7.3 Linux/4.4.0-176-generic botocore/2.0.0dev10

$ aws ssm get-parameters --name "FILE_NAME" --output=text --query "Parameters[].Value" --with-decryption
$ test.txt

では、なぜ書き込めないのか??

調査した結果

16進数で確認してみたところ、結果にバイナリが混入していました。

$ aws ssm get-parameters --name "FILE_NAME" --output=text --query "Parameters[].Value" --with-decryption|od -h
$ 0000000 5b1b 313f 1b68 0d3d 6574 7473 742e 7478
                                               0000020 5b1b 0d6d 0d0a 5b1b 1b4b 3f5b 6c31 3e1b
                                                                                              0000040

stringsでみるとこんな感じ。

$ aws ssm get-parameters --name "FILE_NAME" --output=text --query "Parameters[].Value" --with-decryption|strings
[?1h
    test.txt
            [?1l

対処

stringsで表示すると1行目と3行目にバイナリが混入してる・・
しょうがないので、sedで2行目だけ取るようにしました。

put2.sh
# !/bin/bash

FILE=$(aws ssm get-parameters --name "FILE_NAME" --output=text --query "Parameters[].Value" --with-decryption|strings|sed -n 2P)
echo "aaaa" >> ${FILE}

しょうもないやり方ですが、とりあえず解決

.bash
$ ./put.sh
$ cat test.txt
$ aaaa

しかし、なぜ急に混入したのか腑に落ちない..

さらに調査

バイナリが混入したのはawscliを2.0.6にバージョンアップしたためでした。
version1.18だと問題なし。
バグなのか仕様が変わったのかは不明です。。

$ aws --version
aws-cli/1.18.20 Python/3.5.2 Linux/4.4.0-176-generic botocore/1.15.20

$ ./put.sh
$ cat test.txt
$ aaaa
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?