I am getting error on last line of this code. I am doing data transformation using python in AWS using lambda function. Weird part is it works absolutely fine in my local AWS account but fails on client system on the last line with below error.
import json
import boto3
import base64
import string
from typing import Optional, Iterable, Union
delete_dict = {sp_character: '' for sp_character in string.punctuation}
PUNCT_TABLE = str.maketrans(delete_dict)
output = []
def lambda_handler(event, context):
for record in event['records']:
payload = base64.b64decode(record['data']).decode('utf-8')
remove_special_char = json.loads(payload, object_pairs_hook=clean_keys)
row_w_newline = str(remove_special_char) + "\n"
row_w_newline = base64.b64encode(row_w_newline.encode('utf-8'))
output_record = {
'recordId': record['recordId'],
'result': 'Ok',
'data': row_w_newline
}
output.append(output_record)
print('Processed {} records.'.format(len(event['records'])))
return {'records': output}
This line gives below error :
return {'records': output}
Error:
An error occurred during JSON serialization of response:
Traceback (most recent call last):
File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/var/runtime/bootstrap.py", line 135, in decimal_serializer
raise TypeError(repr(o) + " is not JSON serializable")
Output:
[{'recordId': '49624207526699472828556941664751397737727737843643580418000000', 'result': 'Ok', 'data': b'eyd2ZXJzaW9uJzogJzAnLCAnaWQnOiAnMTExMTE3NzItZjMwYi00OTFjLTZiYmItMWYzYmFkM2FmM2Y5JywgJ2RldGFpbHR5cGUnOiAnQVdTIEFQSSBDYWxsIHZpYSBDbG91ZFRyYWlsJywgJ3NvdXJjZSc6ICdhd3Muc2VjdXJpdHlodWInLCAnYWNjb3VudCc6ICc5MzQ3NTU5ODkxNzYnLCAndGltZSc6ICcyMDIxLTExLTIzVDIwOjQ2OjI2WicsICdyZWdpb24nOiAndXMtZWFzdC0xJywgJ3Jlc291cmNlcyc6IFtdLCAnZGV0YWlsJzogeydldmVudFZlcnNpb24nOiAnMS4wOCcsICd1c2VySWRlbnRpdHknO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TA1NjktYjYwOC00Nzg5LThlNzctNGNlYjhlNTg2YzliJywgJ3JlYWRPbmx5JzogRmFsc2UsICdldmVudFR5cGUnOiAnQXdzQXBpQ2FsbCcsICdtYW5hZ2VtZW50RXZlbnQnOiBUcnVlLCAncmVjaXBpZW50QWNjb3VudElkJzogJzkzNDc1NTk4OTE3NicsICdldmVudENhdGVnb3J5JzogJ01hbmFnZW1lbnQnfX0K'}
Edit : Updated
I have updated my code as below and now i don't receive any error but the output has backward slash () after each and every key/value. how to avoid this ?
for record in event['records']:
payload = base64.b64decode(record['data']).decode('utf-8')
output_record = {
'recordId': record['recordId'],
'result': 'Ok',
'data': base64.b64encode(json.dumps(payload).encode('utf-8') + b'\n').decode('utf-8')
}
output.append(output_record)
return {'records': output}
Output :
"{\"version\":\"0\",\"id\":\"bcdfaff3-ff56-b1ea-a341-0f0ed44380c6\",\"detail-type\":\"Security Hub Findings - Imported\",\"source\":\"aws.securityhub\",\"account\":\"934755989176\",\"time\":\"2021-11-24T16:47:00Z\",\"region\":\"us-east-1\",\"resources\":[\"arn:aws:securityhub:us-east-1::product/aws/securityhub/arn:aws:securityhub:us-east-1:934755989176:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.7/finding/3fbf88ba-b228-4caa-a4aa-5bee36b1b007\"],\"detail\":{\"findings\":[{\"ProductArn\":\"arn:aws:securityhub:us-east-1::product/aws/securityhub\",\"Types\":[\"Software and Configuration Checks/Industry and Regulatory Standards/AWS-Foundational-Security-Best-Practices\"],\"Description\":\"This AWS control checks whether the account password policy for IAM users uses the following recommended configurations: RequireUppercaseCharacters: true, RequireLowercaseCharacters: true, RequireSymbols: true, RequireNumbers: true, MinimumPasswordLength: 8.\",\"Compliance\":{\"Status\":\"PASSED\"},\"ProductName\":\"Security Hub\",\"FirstObservedAt\":\"2021-11-18T14:03:23.999Z\",\"CreatedAt\":\"2021-11-18T14:03:23.999Z\",\"LastObservedAt\":\"2021-11-24T16:46:54.212Z\",\"CompanyName\":\"AWS\",\"FindingProviderFields\":{\"Types\":[\"Software and Configuration Checks/Industry and Regulatory Standards/AWS-Foundational-Security-Best-Practices\"],\"Severity\":{\"Normalized\":0,\"Label\":\"INFORMATIONAL\",\"Product\":0,\"Original\":\"INFORMATIONAL\"}},\"ProductFields\":{\"StandardsArn\":\"arn:aws:securityhub:::standards/aws-foundational-security-best-practices/v/1.0.0\",\"StandardsSubscriptionArn\":\"arn:aws:securityhub:us-east-1:934755989176:subscription/aws-foundational-security-best-practices/v/1.0.0\",\"ControlId\":\"IAM.7\",\"RecommendationUrl\":\"https://docs.aws.amazon.com/console/securityhub/IAM.7/remediation\",\"RelatedAWSResources:0/name\":\"securityhub-iam-password-policy-recommended-defaults-35610a74\",\"RelatedAWSResources:0/type\":\"AWS::Config::ConfigRule\",\"StandardsControlArn\":\"arn:aws:securityhub:us-east-1:934755989176:control/aws-foundational-security-best-practices/v/1.0.0/IAM.7\",\"aws/securityhub/ProductName\":\"Security Hub\",\"aws/securityhub/CompanyName\":\"AWS\",\"Resources:0/Id\":\"arn:aws:iam::934755989176:root\",\"aws/securityhub/FindingId\":\"arn:aws:securityhub:us-east-1::product/aws/securityhub/arn:aws:securityhub:us-east-1:934755989176:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.7/finding/3fbf88ba-b228-4caa-a4aa-5bee36b1b007\"},\"Remediation\":{\"Recommendation\":{\"Text\":\"For directions on how to fix this issue, consult the AWS Security Hub Foundational Security Best Practices documentation.\",\"Url\":\"https://docs.aws.amazon.com/console/securityhub/IAM.7/remediation\"}},\"SchemaVersion\":\"2018-10-08\",\"GeneratorId\":\"aws-foundational-security-best-practices/v/1.0.0/IAM.7\",\"RecordState\":\"ACTIVE\",\"Title\":\"IAM.7 Password policies for IAM users should have strong configurations\",\"Workflow\":{\"Status\":\"RESOLVED\"},\"Severity\":{\"Normalized\":0,\"Label\":\"INFORMATIONAL\",\"Product\":0,\"Original\":\"INFORMATIONAL\"},\"UpdatedAt\":\"2021-11-24T16:46:49.775Z\",\"WorkflowState\":\"NEW\",\"AwsAccountId\":\"934755989176\",\"Region\":\"us-east-1\",\"Id\":\"arn:aws:securityhub:us-east-1:934755989176:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.7/finding/3fbf88ba-b228-4caa-a4aa-5bee36b1b007\",\"Resources\":[{\"Partition\":\"aws\",\"Type\":\"AwsAccount\",\"Region\":\"us-east-1\",\"Id\":\"AWS::::Account:934755989176\"}]}]}}"
output?printthe value to see what it actually contains.