Привет, я новичок в python, и я пытаюсь сравнить две таблицы и получить отчет по cidr, и мне нужно отправить отчет по электронной почте, используя SNS topi c, ниже код, который я написал .
Я могу получить отчет, а при публикации sh сообщение Я получаю сообщение об ошибке проверки, любые предложения, как действовать дальше, и есть ли способ отправить сообщение в виде таблицы, как показано ниже.
код:
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
client = boto3.client('sns')
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table('cidr')
cidr_atd = dynamodb.Table('cidr_allocated')
def lambda_handler(event, context):
# TODO implement
main_cdr = table.scan()
sub_cdr = cidr_atd.scan()
regions = {}
for cdr in main_cdr['Items']:
regions[cdr['region']] = {}
regions[cdr['region']]['provider'] = cdr['provider']
regions[cdr['region']]['subnet'] = cdr['subnet'].split('/')[1]
regions[cdr['region']]['cidravaliable'] = [i for i in range(int(regions[cdr['region']]['subnet']), 33)]
# for key, val in regions.items():
# print(key, ' subnet_missing : ', val['subnet_missing'])
for scdr in sub_cdr['Items']:
regions[scdr['region']]['cidravaliable'].remove(int(scdr['subnet '].split('/')[1]))
#print('-' * 110)
#print('| Region' + ' ' * 17 + '| Provider' + ' ' * 5 + '| Remaining CIDRs' + ' ' * 52 + '|')
#print('-' * 110)
#for key, val in regions.items():
# del regions[key]['subnet']
# region = '| ' + key
# provider = '| ' + val['provider']
# subnet_missing = '| ' + ', '.join(list(map(str, val['subnet_missing'])))
# print(region + ' ' * (25 - len(region)) + provider + ' ' * (15 - len(provider)) + subnet_missing + ' ' * (69 - len(subnet_missing)) + '|')
#print('-' * 110)
def publishOntoSNSTopic(regions):
client.publish(
TargetArn='arn:aws:sns:eu-west-2:333333333:test',
Message=regions,
Subject='test'
)
publishOntoSNSTopic(regions)
return {
'statusCode': 200,
'body': json.dumps(regions)
}
ответ:
{
"errorMessage": "Parameter validation failed:\nInvalid type for parameter Message, value: {'us-west-2': {'provider': 'aws', 'cidravaliable': [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]}, 'centralus': {'provider': 'azure', 'cidravaliable': [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]}, 'eu-west-1': {'provider': 'aws', 'cidravaliable': [22, 24, 25, 26, 27, 28, 29, 30, 31, 32]}, 'ap-southeast-1': {'provider': 'aws', 'cidravaliable': [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]}, 'eu-west-2': {'provider': 'aws', 'cidravaliable': [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]}, 'eu-central-1': {'provider': 'aws', 'cidravaliable': [19, 21, 23, 24, 26, 28, 29, 30, 31, 32]}}, type: <class 'dict'>, valid types: <class 'str'>",
"errorType": "ParamValidationError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 51, in lambda_handler\n publishOntoSNSTopic(regions)\n",
" File \"/var/task/lambda_function.py\", line 45, in publishOntoSNSTopic\n client.publish(\n",
" File \"/var/runtime/botocore/client.py\", line 316, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 607, in _make_api_call\n request_dict = self._convert_to_request_dict(\n",
" File \"/var/runtime/botocore/client.py\", line 655, in _convert_to_request_dict\n request_dict = self._serializer.serialize_to_request(\n",
" File \"/var/runtime/botocore/validate.py\", line 297, in serialize_to_request\n raise ParamValidationError(report=report.generate_report())\n"
сообщение, которое я пытаюсь создать вместо json:
Function logs:
START RequestId: 21b40036-a590-4854-ac5b-2b23bff56ecd Version: $LATEST
--------------------------------------------------------------------------------------------------------------
| Region | Provider | Remaining CIDRs |
--------------------------------------------------------------------------------------------------------------
| us-west-2 | aws | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 |
| centralus | azure | 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 |
| eu-west-1 | aws | 22, 24, 25, 26, 27, 28, 29, 30, 31, 32 |
| ap-southeast-1 | aws | 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 |
| eu-west-2 | aws | 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 |
| eu-central-1 | aws | 19, 21, 23, 24, 26, 28, 29, 30, 31, 32 |
--------------------------------------------------------------------------------------------------------------