8

I am using a simple boto3 script to retrieve a parameter from SSM param store in my aws account. The python script looks like below:

client = get_boto3_client('ssm', 'us-east-1')
try:
    response = client.get_parameter(Name='my_param_name',WithDecryption=True)
except Exception as e:
    logging.error("retrieve param error: {0}".format(e))
    raise e
return response

If the given parameter is not available, I get a generic error in the response like below:

 An error occurred (ParameterNotFound) when calling the GetParameter operation: Parameter my_param_name not found.   

I have verified method signature from boto3 ssm docs. Related AWS API Docs confirms to return a 400 response when parameter does not exist in the param store.

My question is that how do I verify if the exception caught in the response is actually a 400 status code so that I can handle it accordingly.

Vishal
  • 1,963
  • 2
  • 20
  • 23
  • Does this answer your question? [How to handle errors with boto3?](https://stackoverflow.com/questions/33068055/how-to-handle-errors-with-boto3) – Victor Sergienko Jul 06 '22 at 21:09

3 Answers3

13

You can try catching client.exceptions.ParameterNotFound:

client = get_boto3_client('ssm', 'us-east-1')

try:
  response = client.get_parameter(Name='my_param_name',WithDecryption=True)
except client.exceptions.ParameterNotFound:
  logging.error("not found")
kichik
  • 33,220
  • 7
  • 94
  • 114
  • Perfect. Catching the ParameterNotFound response made my script running as expected. Thank you @kichik – Vishal Mar 02 '18 at 16:18
0

Catching exceptions is not always a good idea. For those who would like to avoid it, use get-parameters instead and check if your parameter is in InvalidParameters:

client = get_boto3_client('ssm', 'us-east-1')

def get_ssm_parameter(name):
    parameters = client.get_parameters(Names=[name], WithDecryption=True)
    invalid_parameters = parameters.get('InvalidParameters')
    if invalid_parameters and name in invalid_parameters:
        return None
    return parameters['Parameters'][0]
elshev
  • 1,164
  • 3
  • 16
  • 30
-1

You can look at the status via response['Error']['Code'], but since there are multiple reasons for a 400, I would recommend a better approach:

response = client.get_parameter(Name='my_param_name',WithDecryption=True)

if 'Parameters' not in response:
    raise ValueError('Response did not contain parameters key')
else:
    return response
Aphid
  • 343
  • 1
  • 9
  • This is already a part of my original question. It does not help me because the first line itself throws an exception with no details but a message of parameter not found. – Vishal Mar 02 '18 at 16:20
  • @Vishal "My question is that how do I verify if the exception caught in the response is actually a 400 status code so that I can handle it accordingly." I answered that question. I am saying validating against status 400 is a bad idea, because multiple problems cause status 400. – Aphid Mar 02 '18 at 17:32
  • thats exactly what I was asking. If you see AWS API Doc I linked in my original doc, you will see that every error responds back with status 400 (even an error that should be 500). kichik solution worked great with me because I am specifically catching a 404. Your code is not working because the system is throwing exception on the very first line in your code, not allowing me to even check if 'Parameters' is included in the response dictionary.Thanks for your post though. – Vishal Mar 02 '18 at 22:06