How to Fix "Records is not defined" Error

My Lambda function is failing with this error:

It looks as if the error is saying that there are no values for “print” when the function runs.

I got that code from this blog post.

This function is supposed to move the contents of a specific s3 bucket to Azure Blog storage anytime a new file is placed into the bucket.

I’m using a docker image to bundle the .py file that contains the code I used from that blog post (as well as the requirements.txt ) into an image that is linked to the function via AWS ECR.

What do I do to get around this error?

Here is the code I’m using from that blog post:

import json
import boto3
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient


def lambda_handler(event, context):
    s3 = boto3.client("s3")
    connect_str = os.getenv('CONNECT_STR')
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    records = event["Records"]
    
    print("Received records from s3 to update:" + str(len(records)))
    for record in records:
        key = record["s3"]["object"]["key"]
        bucket_name = record["s3"]["bucket"]["name"]
        print("Processing object with key:" + str(key))
        file = s3.get_object(Bucket=bucket_name, Key=key)
        itr = file["Body"].iter_chunks()
        azure_container_client = blob_service_client.get_container_client(container=bucket_name)
        azure_container_client.upload_blob(name=key, data=itr)
        print "Object " + key + " transferred to azure successfully"ode here

The “record” in this instance would be whatever was placed into the S3 bucket (I believe) so how do I get that to register in the code as being the “records” the code is referencing?

I don’t think that is the code that you are actually running. The last line of the code you show is:

    print "Object " + key + " transferred to azure successfully"ode here

which is a syntax error, so that cannot be what you are running.

Thanks for replying; I made an error while copy/pasting the code somehow. This is it:

import json
import boto3
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient


def lambda_handler(event, context):
    s3 = boto3.client("s3")
    connect_str = os.getenv('CONNECT_STR')
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    records = event["Records"]
    
    print("Received records from s3 to update:" + str(len(records)))
    for record in records:
        key = record["s3"]["object"]["key"]
        bucket_name = record["s3"]["bucket"]["name"]
        print("Processing object with key:" + str(key))
        file = s3.get_object(Bucket=bucket_name, Key=key)
        itr = file["Body"].iter_chunks()
        azure_container_client = blob_service_client.get_container_client(container=bucket_name)
        azure_container_client.upload_blob(name=key, data=itr)
        print "Object " + key + " transferred to azure successfully"

Hi Al,

that’s still not your actual code. You are using boto3, which
requires Python 3.6 or higher:

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html

but you are using Python 2 syntax for print:

print "Object " + key + " transferred to azure successfully"

which is a syntax error in Python 3. So the code you have shown us can’t run at all, and I don’t know how you are getting the error you say you are getting.

For us to help you, you have to be more careful about giving us accurate details of the code you are running and the error you are receiving.