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.
Using Boto3, the error response from an AWS service will look similar to a success response, except that an Error nested dictionary will appear with the ResponseMetadata nested dictionary. Here is an example of what an error response might look like:
SSM Agent runs on your managed Amazon Elastic Compute Cloud (Amazon EC2) instance and processes requests from the AWS Systems Manager service. The following conditions must be met to use SSM Agent:
When SSM Agent can't connect with the Systems Manager endpoints, you see error messages similar to the following in the SSM Agent logs: The following are some common reasons why SSM Agent can't connect with the Systems Manager API endpoints on port 443: Instance egress security group rules don't allow outgoing connections on port 443.
For a complete list of error responses from the services you’re using, consult the individual service’s AWS documentation, specifically the error response section of the AWS service’s API reference. These references also provide context around the exceptions and errors.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With