I want to retrieve AWS secrets using python boto3 and I came across this sample code:
- aws-doc-sdk-examples/python/example_code/secretsmanager/get_secret_value.py at main · awsdocs/aws-doc-sdk-examples · GitHub
- Secrets Manager examples using SDK for Python (Boto3) – AWS SDK Code Examples
But it is confusing. I don’t see boto3 library import in the python file. Not an expert of Python, so any help in understanding this much appreciated.
I was expecting to have the AWS secrets name and boto3 libarary as part of the python function.
2
Answers
Per the documentation, each of the example folders has one or more main runner scripts.
For the Secrets Manager examples, you would run either:
Each of these ‘runner’ scripts imports the relevant Python code e.g. get_secret_value.py.
The code is structured this way so that you can easily test the code while separating the test code from the reusable utility code (that you would potentially use).
To work with AWS Secrets Manager using the boto3 library in Python, you indeed need to import the boto3 library first. Additionally, to retrieve a secret, you need to know the name or the ARN (Amazon Resource Name) of the secret you wish to retrieve.
Here’s a simple, complete example that demonstrates how to import the boto3 library and retrieve a secret from AWS Secrets Manager. This example assumes you have already set up your AWS credentials in a way that boto3 can automatically detect them (for example, by using the AWS CLI and running aws configure).
I hope this helps