8
5

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.

aws-cliでSSMのparameter storeにデータをいれる

Last updated at Posted at 2018-10-23

使うコマンド

$ aws ssm put-patameter

文字列をそのまま入れる

$ aws ssm put-parameter --name "test" --type "String" --value "hello"
{
    "Version": 1
}

ssm get-parametersで取得できます。

$ aws ssm get-parameters --name "test" | jq .
{
  "InvalidParameters": [],
  "Parameters": [
    {
      "Name": "test",
      "LastModifiedDate": 1540291994.341,
      "Value": "hello",
      "Version": 1,
      "Type": "String",
      "ARN": "arn:aws:ssm:ap-northeast-1:99999:parameter/test"
    }
  ]
}

上書きする

アクション名はputですが、そのまま実行すると怒られます。

$ aws ssm put-parameter --name "test" --type "String" --value "hello world"

An error occurred (ParameterAlreadyExists) when calling the PutParameter operation: The parameter already exists. To overwrite this value, set the overwrite option in the request to true.

--overwriteオプションをつけて、明示的に上書きしましょう。

$ aws ssm put-parameter --name "test" --type "String" --value "hello world" --overwrite
{
    "Version": 2
}

getすると、新しいバージョンのデータが取れます。

{
  "InvalidParameters": [],
  "Parameters": [
    {
      "Name": "test",
      "LastModifiedDate": 1540292106.049,
      "Value": "hello world",
      "Version": 2,
      "Type": "String",
      "ARN": "arn:aws:ssm:ap-northeast-1:99999:parameter/test"
    }
  ]
}

以前のバージョンは、get-parameter-historyで確認できます。

$ aws ssm get-parameter-history --name "test" | jq .
{
  "Parameters": [
    {
      "Name": "test",
      "LastModifiedDate": 1540291994.341,
      "Labels": [],
      "Value": "hello",
      "Version": 1,
      "LastModifiedUser": "arn:aws:iam::999999:user/developer",
      "Type": "String"
    },
    {
      "Name": "test",
      "LastModifiedDate": 1540292106.049,
      "Labels": [],
      "Value": "hello world",
      "Version": 2,
      "LastModifiedUser": "arn:aws:iam::999999:user/developer",
      "Type": "String"
    }
  ]
}

誰が更新したかも記録してくれてるみたいですね。

セキュアに保存する

パスワードやtokenなどは、平文で保存したくないものです。
その場合はtype=SecureStringでputしましょう。

比較がしやすいので、--overwriteでいきます。

$ aws ssm put-parameter --name "test" --type "SecureString" --value "hello world" --overwrite
{
    "Version": 3
}

Historyでみると、Version: 3だけ暗号化されていますね。

$ aws ssm get-parameter-history --name "test" | jq .
{
  "Parameters": [
    {
      "Name": "test",
      "LastModifiedDate": 1540291994.341,
      "Labels": [],
      "Value": "hello",
      "Version": 1,
      "LastModifiedUser": "arn:aws:iam::999999:user/developer",
      "Type": "String"
    },
    {
      "Name": "test",
      "LastModifiedDate": 1540292106.049,
      "Labels": [],
      "Value": "hello world",
      "Version": 2,
      "LastModifiedUser": "arn:aws:iam::999999:user/developer",
      "Type": "String"
    },
    {
      "KeyId": "alias/aws/ssm",
      "Name": "test",
      "LastModifiedDate": 1540292264.596,
      "Labels": [],
      "Value": "AQICAHhpaogOe4fhQlvnUVPt0gxy8Q22XJVDE6qLcPVeg5TPEQGYPCmVdGBS8FFRbBM7HZxVAAAAaTBnBgkqhkiG9w0BBwagWjBYAgEAMFMGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMW5oTfRpPrcv8u+JvAgEQgCZ+g900m/CE7TGKmc55EAaclq2rvoDtMuSkoclDT+63ZRccx5Rlyg==",
      "Version": 3,
      "LastModifiedUser": "arn:aws:iam::999999:user/developer",
      "Type": "SecureString"
    }
  ]
}

暗号化したものを復号する

AWS CLIから復号したものをとる場合、get-parameters--with-decryptionを使います。

$ aws ssm get-parameters --name "test"  --with-decryption 
{
    "InvalidParameters": [], 
    "Parameters": [
        {
            "Name": "test", 
            "LastModifiedDate": 1540292264.596, 
            "Value": "hello world", 
            "Version": 3, 
            "Type": "SecureString", 
            "ARN": "arn:aws:ssm:ap-northeast-1:999999:parameter/test"
        }
    ]
}

後片付け

遊んだ後は、delete-parameterでちゃんとお掃除しましょう。

$ aws ssm  delete-parameter --name test
8
5
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
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?