【发布时间】:2021-08-31 21:10:42
【问题描述】:
部署以下代码中给出的 lambda 代码时出现以下错误 不可散列的类型:'dict':TypeError 回溯(最近一次通话最后): 文件“/var/task/lambda.py”,第 69 行,在 lambda_handler response_body 类型错误:不可散列的类型:'dict' unhashable type: 'dict': TypeError Traceback (last recent call last): File "/var/task/lambda.py", line 69, in lambda_handler response_body TypeError: unhashable type: 'dict'
import boto3, json, logging, os
import requests
logs = boto3.client('logs')
ssm = boto3.client('ssm')
#Define logging properties
log = logging.getLogger()
log.setLevel(os.environ.get("LOGLEVEL"))
def lambda_handler(event, context):
#Initialize the status of the function
status="SUCCESS"
responseData = {}
RequestType = event['RequestType']
if RequestType == 'Delete' :
#Set Return Data
response_data = {"Message" : "Subscription filter deleted"}
#return the response back to the S3 URL to notify CloudFormation about the code being run
response=respond(event,context,status,response_data,None)
#Function returns the response from the S3 URL
response_body = {}
response_body['Dev Response'] = json.dumps(response)
return {
response_body
}
destinationArn=os.environ['alert_lambda_arn']
LogGroupName = event['ResourceProperties']['LogGroupName']
log.debug("LogGroupName :" + LogGroupName)
ServiceName=LogGroupName[25:-14]
log.debug("Service Name : {}".format(ServiceName))
CommomPre = event['ResourceProperties']['CommonPre']
log.debug("CommomPre :" + CommomPre)
NoOfcommomTerm =int( event['ResourceProperties']['NoOfCommonTerm'])
ServiceSpecPre = event['ResourceProperties']['ServiceSpecPre']
log.debug("ServiceSpecPre :" + ServiceSpecPre)
NoOfServiceSpecTerm = int (event['ResourceProperties']['NoOfServiceSpecTerm'])
filterpattern=""
filterpattern=getFilterpattern(CommomPre, NoOfcommomTerm, filterpattern)
filterpattern=getFilterpattern(ServiceSpecPre, NoOfServiceSpecTerm, filterpattern)
response = logs.put_subscription_filter(
logGroupName=LogGroupName,
filterName="vm-managed-" + ServiceName + "filter",
filterPattern=filterpattern,
destinationArn=destinationArn
)
#Set Return Data
response_data = {"Message" : "Subscription filter created"}
#return the response back to the S3 URL to notify CloudFormation about the code being run
response=respond(event,context,status,response_data,None)
#Function returns the response from the S3 URL
response_body = {}
response_body['Dev Response'] = json.dumps(response)
return {
response_body
}
def getFilterpattern(prefix, NoOfterm, filterpattern):
for i in range(0, NoOfterm):
term= prefix + str(i+1)
print(term)
response = ssm.get_parameter(
Name=term,
)
filterpattern=filterpattern + " " + response['Parameter']['Value']
return filterpattern
def respond(event, context, responseStatus, responseData, physicalResourceId):
#Build response payload required by CloudFormation
responseBody = {}
responseBody['Status'] = responseStatus
responseBody['Reason'] = 'Details in: ' + context.log_stream_name
responseBody['PhysicalResourceId'] = context.log_stream_name
responseBody['StackId'] = event['StackId']
responseBody['RequestId'] = event['RequestId']
responseBody['LogicalResourceId'] = event['LogicalResourceId']
responseBody['Data'] = responseData
#Convert json object to string and log it
json_responseBody = json.dumps(responseBody)
log.debug("Response body: " + str(json_responseBody))
#Set response URL
responseUrl = event['ResponseURL']
#Set headers for preparation for a PUT
headers = {
'content-type' : '',
'content-length' : str(len(json_responseBody))
}
#Return the response to the signed S3 URL
try:
response = requests.put(responseUrl,
data=json_responseBody,
headers=headers)
log.debug("Status code: " + str(response.reason))
status="SUCCESS"
return status
#Defind what happens if the PUT operation fails
except Exception as e:
log.error("send(..) failed executing requests.put(..): " + str(e))
status="FAILED"
return status
【问题讨论】:
-
进展如何?仍然不清楚你可以做些什么来解决这个问题?
-
非常感谢您的回答。它工作正常。
-
如果答案有帮助,我们将不胜感激。
标签: python amazon-web-services aws-lambda boto3 serverless