简体   繁体   English

Dynamodb lambda function 错误在 AttributeDefinitions[0] 中缺少必需的参数

[英]Dynamodb lambda function Error Missing required parameter in AttributeDefinitions[0]

I am working with AWS Lambda Function and want to integrate them with Dynamo-db to keep the track of my cloudwatch data matrix into it in order to keep track of alerts for sending messages if alerts are sent or not to record in DB.我正在使用 AWS Lambda Function 并希望将它们与 Dynamo-db 集成以将我的 cloudwatch 数据矩阵跟踪到其中,以便在发送或不发送警报时跟踪发送消息的警报以记录在数据库中。

The function is perfectly fine if I don't use Dynamo-DB, below is the code with Dynamo-db.如果我不使用 Dynamo-DB,function 完全没问题,下面是 Dynamo-db 的代码。 I never used DynamoDB but just started.我从未使用过 DynamoDB,但才刚刚开始。

Code:代码:

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!')
    }

Error:错误:

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

Edit:编辑:

In the below section i just changed the 'filesystem_id' to the filesystem_id and another one is all lowercae attribute to first letter upper-cae like Attribute .在下面的部分中,我只是将'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
    }

Now the New Error:现在是新错误:

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",

Can someone please help on this.有人可以帮忙吗?

All problems clearly described in error message:错误消息中清楚描述的所有问题:

  1. The problematic part is AttributeDefinitions有问题的部分是AttributeDefinitions
  2. Allowed key names are !A!ttribute(Name or Type) but not !a!ttribute(Name or Type) .允许的键名是!A!ttribute(Name or Type)但不是!a!ttribute(Name or Type) And you improved only first element of AttributeDefinitions after editing the question.在编辑问题后,您只改进了AttributeDefinitions的第一个元素。
  3. AttributeType should be S , N or B . AttributeType 应该是SNB Not BOOL不是BOOL

AttributeType (string) -- [REQUIRED]属性类型(字符串)——[必需]
The data type for the attribute, where:属性的数据类型,其中:

S - the attribute is of type String S - 属性是 String 类型
N - the attribute is of type Number N - 属性的类型为 Number
B - the attribute is of type Binary B - 属性是二进制类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM