user1187968
user1187968

Reputation: 7986

Python/AWS Parameter Store: updated SSM values

Let's say I have a AWS SSM called /config/db withe following values:

{
   "host": "localhost",
   "port": "3306"
}

now I need to add the following item(s) to the same SSM

{
  "my_version": "1.0"
}

How can I use the Python/boto3 package to archive this action?

Upvotes: 0

Views: 1469

Answers (1)

acorbel
acorbel

Reputation: 1568

You just have to use the put_parameter method to update (overwrite) the parameter.

You grab the old one, parse the JSON object then add the required attributes (my_version) and update the parameter with the serialized (json.dumps) value.

import boto3
import json

client = boto3.client('ssm')

def lambda_handler(event, context):
    old_parameter = client.get_parameter(Name='/config/db')
    print(old_parameter)
    parameter_value = json.loads(old_parameter['Parameter']['Value'])
    parameter_value['my_version'] = '1.0'
    client.put_parameter(Name='/config/db', Overwrite=True, Value=json.dumps(parameter_value))

Do not forget the required IAM permissions to update the parameter.

Upvotes: 2

Related Questions