[英]Dynamodb lambda function Error Missing required parameter in AttributeDefinitions[0]
我正在使用 AWS Lambda Function 並希望將它們與 Dynamo-db 集成以將我的 cloudwatch 數據矩陣跟蹤到其中,以便在發送或不發送警報時跟蹤發送消息的警報以記錄在數據庫中。
如果我不使用 Dynamo-DB,function 完全沒問題,下面是 Dynamo-db 的代碼。 我從未使用過 DynamoDB,但才剛剛開始。
import json
import os
import boto3
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
def lambda_handler(event, context):
fsx = boto3.client('fsx')
cloudwatch = boto3.client('cloudwatch')
ses = boto3.client('ses')
region_name = os.environ['AWS_REGION']
dynamodb = boto3.resource('dynamodb', region_name=region_name)
now = datetime.utcnow()
start_time = (now - timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%SZ')
end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
table = []
result = []
next_token = None
while True:
if next_token:
response = fsx.describe_file_systems(NextToken=next_token)
else:
response = fsx.describe_file_systems()
for filesystem in response.get('FileSystems'):
filesystem_id = filesystem.get('FileSystemId')
table.append(filesystem_id)
next_token = response.get('NextToken')
if not next_token:
break
try:
# Create the DynamoDB table if it does not exist
table = dynamodb.create_table(
TableName='FsxNMonitorFsx',
KeySchema=[
{
'AttributeName': 'filesystem_id',
'KeyType': 'HASH' #Partition key
}
],
AttributeDefinitions=[
{
'attributeName': 'filesystem_id',
'AttributeType': 'S'
},
{
'attributeName': 'alert_sent',
'attributeType': 'BOOL'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
# Wait for the table to be created
table.meta.client.get_waiter('table_exists').wait(TableName='FsxNMonitorFsx')
except ClientError as e:
if e.response['Error']['Code'] != 'ResourceInUseException':
raise
# Code to retrieve metric data and check if alert needs to be sent
for filesystem_id in table:
response = cloudwatch.get_metric_data(
MetricDataQueries=[
{
'Id': 'm1',
'MetricStat': {
'Metric': {
'Namespace': 'AWS/FSx',
'MetricName': 'StorageCapacity',
'Dimensions': [
{
'Name': 'FileSystemId',
'Value': filesystem_id
},
{
'Name': 'StorageTier',
'Value': 'SSD'
},
{
'Name': 'DataType',
'Value': 'All'
}
]
},
'Period': 60,
'Stat': 'Sum'
},
'ReturnData': True
},
{
'Id': 'm2',
'MetricStat': {
'Metric': {
'Namespace': 'AWS/FSx',
'MetricName': 'StorageUsed',
'Dimensions': [
{
'Name': 'FileSystemId',
'Value': filesystem_id
},
{
'Name': 'StorageTier',
'Value': 'SSD'
},
{
'Name': 'DataType',
'Value': 'All'
}
]
},
'Period': 60,
'Stat': 'Sum'
},
'ReturnData': True
}
],
StartTime=start_time,
EndTime=end_time
)
storage_capacity = response['MetricDataResults'][0]['Values']
storage_used = response['MetricDataResults'][1]['Values']
if storage_capacity:
storage_capacity = storage_capacity[0]
else:
storage_capacity = None
if storage_used:
storage_used = storage_used[0]
else:
storage_used = None
if storage_capacity and storage_used:
percent_used = (storage_used / storage_capacity) * 100
else:
percent_used = None
response = dynamodb.get_item(
TableName='FsxNMonitorFsx',
Key={'filesystem_id': {'S': filesystem_id}}
)
if 'Item' in response:
alert_sent = response['Item']['alert_sent']['BOOL']
else:
alert_sent = False
# Send alert if storage usage exceeds threshold and no alert has been sent yet
if percent_used > 80 and not alert_sent:
email_body = "... some code..."
ses.send_email(
Source='abc@example.com'',
Destination={
'ToAddresses': ['examp_mail@example.com'],
},
Message={
'Subject': {
'Data': email_subject
},
'Body': {
'Html': {
'Data': email_body
}
}
}
)
# Update FsxNMonitorFsx in DynamoDB
dynamodb.update_item(
TableName='FsxNMonitorFsx',
Key={'filesystem_id': {'S': filesystem_id}},
UpdateExpression='SET alert_sent = :val',
ExpressionAttributeValues={':val': {'BOOL': True}}
)
return {
'statusCode': 200,
'body': json.dumps('Email sent!')
}
Response
{
"errorMessage": "Parameter validation failed:\nMissing required parameter in AttributeDefinitions[0]: \"AttributeName\"\nUnknown parameter in AttributeDefinitions[0]: \"attributeName\", must be one of: AttributeName, AttributeType\nMissing required parameter in AttributeDefinitions[1]: \"AttributeName\"\nMissing required parameter in AttributeDefinitions[1]: \"AttributeType\"\nUnknown parameter in AttributeDefinitions[1]: \"attributeName\", must be one of: AttributeName, AttributeType\nUnknown parameter in AttributeDefinitions[1]: \"attributeType\", must be one of: AttributeName, AttributeType",
"errorType": "ParamValidationError",
"requestId": "54de7194-f8e8-4a9f-91d6-0f77575de775",
Function Logs
START RequestId: 54de7194-f8e8-4a9f-91d6-0f77575de775 Version: $LATEST
[ERROR] ParamValidationError: Parameter validation failed:
Missing required parameter in AttributeDefinitions[0]: "AttributeName"
Unknown parameter in AttributeDefinitions[0]: "attributeName", must be one of: AttributeName, AttributeType
Missing required parameter in AttributeDefinitions[1]: "AttributeName"
Missing required parameter in AttributeDefinitions[1]: "AttributeType"
Unknown parameter in AttributeDefinitions[1]: "attributeName", must be one of: AttributeName, AttributeType
Unknown parameter in AttributeDefinitions[1]: "attributeType", must be one of: AttributeName, AttributeType
在下面的部分中,我只是將'filesystem_id'
更改為filesystem_id
,另一個是將所有 lowercae attribute
更改為第一個字母 upper-cae ,如Attribute
。
{
'AttributeName': filesystem_id,
'KeyType': 'HASH' #Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': filesystem_id,
'AttributeType': 'S'
},
{
'attributeName': 'alert_sent',
'attributeType': 'BOOL'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
Response
{
"errorMessage": "An error occurred (ValidationException) when calling the CreateTable operation: 1 validation error detected: Value 'BOOL' at 'attributeDefinitions.2.member.attributeType' failed to satisfy constraint: Member must satisfy enum value set: [B, N, S]",
"errorType": "ClientError",
有人可以幫忙嗎?
錯誤消息中清楚描述的所有問題:
AttributeDefinitions
!A!ttribute(Name or Type)
但不是!a!ttribute(Name or Type)
。 在編輯問題后,您只改進了AttributeDefinitions
的第一個元素。AttributeType
應該是S
、 N
或B
。 不是BOOL
屬性類型(字符串)——[必需]
屬性的數據類型,其中:S - 屬性是 String 類型
N - 屬性的類型為 Number
B - 屬性是二進制類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.