How to Access AWS Systems Manager Parameter Store using Python?

Introduction

In this article, you will learn how to access AWS Systems Manager Parameter Store using Python.

Pre-requisites

  1. Create a Systems Manager parameter

Tools

  1. Visual Studio Code

Create a project folder

In this task, you will see how to create the project folder.

Step 1. Open Windows Command Prompt and run the following commands to create the new folder for this project.

mkdir AccessParameterStore
cd .\AccessParameterStore

Install the required packages

In this task, you will see how to install the required packages for this project.

Step 1. Open the AccessParameterStore folder in Visual Studio Code. Click Terminal -> New Terminal.

Step 2. Run the following command to install the packages required for this project.

pip install boto3

Implementation

In this task, you will see how to retrieve the parameter value from AWS Systems Manager Parameter store using Python.

Step 1. In Visual Studio Code, create a new file, app.py, under the AccessParameterStore folder.

Step 2. Copy and paste the below code.

# Connect to AWS Parameter store
import boto3

# Get parameter value
ssm = boto3.client('ssm')

def get_parameter(parameter_name):
    response = ssm.get_parameter(Name=parameter_name, WithDecryption=True)
    return response['Parameter']['Value']

def main():
    parameter_name = '/dev/myapp/key1'
    parameter_value = get_parameter(parameter_name)
    print(parameter_value)  

if __name__ == '__main__':  
    main()

Test the API

In this task, you will see how to test the code.

Step 1. In Visual Studio Code, run the following command in the Terminal. The parameter value gets displayed.

python.exe .\app.py 

Summary

This article describes how to access AWS Systems Manager Parameter Store using Python.


Similar Articles