SSM的Boto3 AWS API错误响应 [英] Boto3 AWS API error responses for SSM

查看:129
本文介绍了SSM的Boto3 AWS API错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的boto3脚本从aws帐户中的SSM参数存储中检索参数. python脚本如下所示:

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.   

我已经从 boto3验证了方法签名ssm docs .相关 AWS API文档确认返回400响应当参数存储中不存在参数时.

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.

我的问题是,如何验证响应中捕获的异常是否实际上是400状态代码,以便我可以进行相应处理.

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.

推荐答案

您可以尝试捕获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")

这篇关于SSM的Boto3 AWS API错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆