0
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 3 years have passed since last update.

aws ssm parameter を環境変数に一括でセットする

Last updated at Posted at 2021-11-15

Overview

ssm のパラメータを1個ずつ環境変数にセットするのがめんどくさいのでまとめてやっちまうか、っていう話。

cf.

https://dev.classmethod.jp/articles/aws-cli-all-ssm-parameter-get/
https://docs.aws.amazon.com/cli/latest/reference/ssm/get-parameters.html

aws ssm get-parameters の結果

ってこういう形になってるじゃないですか。

sample.json
{
    "Parameters": [
        {
            "Name": "AAAAAAA",
            "Type": "String",
            "Value": "あああああああ",
            "Version": 1,
            "LastModifiedDate": 1585235884.237,
            "ARN": "arn:aws:ssm:ap-southeast-1:123456789012:parameter/AAAAAAA"
        },
        {
            "Name": "BBBBBBB",
            "Type": "String",
            "Value": "12345678",
            "Version": 1,
            "LastModifiedDate": 1585235895.919,
            "ARN": "arn:aws:ssm:ap-southeast-1:123456789012:parameter/BBBBBBB"
        },
        {
            "Name": "CCCCCCC",
            "Type": "String",
            "Value": "いいいいいいい",
            "Version": 1,
            "LastModifiedDate": 1585235959.944,
            "ARN": "arn:aws:ssm:ap-southeast-1:123456789012:parameter/CCCCCCCC"
        },
        {
            "Name": "DDDDDDD",
            "Type": "String",
            "Value": "0987654321",
            "Version": 1,
            "LastModifiedDate": 1585235975.3,
            "ARN": "arn:aws:ssm:ap-southeast-1:123456789012:parameter/DDDDDDD"
        }
    ]
}

で、これを

Name=Value の形で環境変数にセットして、

bash
export AAAAAAA=あああああああ
export BBBBBBB=12345678
export CCCCCCC=いいいいいいい
export DDDDDDD=0987654321

以降のシェル芸で使いたい

bash
$ echo ${AAAAAAA}
あああああああ

っていうよくあるアレ。

まぁ ssm に限らず、セットしたい key と value を含む json であれば(つまり json に変換できれば)だいたいこれで対応できます。

やりかた

Name=Value の形に整形

jq 使います。

bash
$ cat sample.json | jq -r '.Parameters[] | [.Name, .Value] | join("=")'
AAAAAAA=あああああああ
BBBBBBB=12345678
CCCCCCC=いいいいいいい
DDDDDDD=0987654321

Name/foo/bar/baz/AAAAAAA みたいにパス形式になってるときは

.Parameters[] | [(.Name|split("/")[-1]), .Value] | join("=") みたいに .Name を適当に整形すればおk。

export に食わせる

export コマンドって複数まとめて設定できるの知ってた?

bash
$ export $(cat sample.json | jq -r '.Parameters[] | [.Name, .Value] | join("=")')

$ env | grep -e AAAAAAA -e BBBBBBB -e CCCCCCC -e DDDDDDD
AAAAAAA=あああああああ
CCCCCCC=いいいいいいい
BBBBBBB=12345678
DDDDDDD=0987654321

まとめると

ワンライナーのシェル芸だとこんな感じ。

bash
$ export $(aws ssm get-parameters --with-decryption \
    --names AAAAAAA BBBBBBB CCCCCCC DDDDDDD \
    | jq -r '.Parameters[] | [.Name, .Value] | join("=")')

jq スクリプト

に外出しした方が、シェルのクォートとか気にしなくていいのでオススメ。

cnv.jq
.Parameters[]
| [.Name, .Value]
| join("=")
bash
$ export $(aws ssm get-parameters --with-decryption \
    --names AAAAAAA BBBBBBB CCCCCCC DDDDDDD | jq -r -f cnv.jq)

別解

いったんファイルに落として source してもOK。

bash
$ cat sample.json | jq -r '.Parameters[] | [.Name, .Value] | join("=")' >.env

$ set -a
$ . .env
$ set +a

$ env | grep -e AAAAAAA -e BBBBBBB -e CCCCCCC -e DDDDDDD
AAAAAAA=あああああああ
CCCCCCC=いいいいいいい
BBBBBBB=12345678
DDDDDDD=0987654321

set -a って何?

普通に AAAAAAA=あああああああ だけを . してもシェル変数になってしまうので、
すべて環境変数としてセットするためのオプション。
set -o allexport と同等。

cf. https://atmarkit.itmedia.co.jp/ait/articles/1805/10/news023.html

シェル変数と環境変数の違いについてはこちらを参照。
https://qiita.com/kure/items/f76d8242b97280a247a1

っていう話。

0
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
0
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?