Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 AWS API error responses for SSM

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.

like image 891
Vishal Avatar asked Feb 23 '18 20:02

Vishal


People also ask

What does a boto3 error response look like in AWS?

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:

What is SSM agent in AWS systems manager?

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:

Why can't SSM agent connect to the Systems Manager API endpoints?

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.

Where can I find an error response for an AWS service?

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.


1 Answers

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")
like image 183
kichik Avatar answered Sep 19 '22 13:09

kichik