Skip to content

AWS Glue - Error "Index 0 out of bounds for length 0" upon writing DynamicFrame

0

I am writing a simple AWS Glue ETL job as a Python script which reads a record from a Snowflake table and updates the corresponding record in Salesforce. However, I am getting the following error "Error Category: UNCLASSIFIED_ERROR; Failed Line Number: 25; An error occurred while calling o170.pyWriteDynamicFrame. Index 0 out of bounds for length 0".

The script is the following:

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
from awsglue import DynamicFrame

args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

Snowflake_source = glueContext.create_dynamic_frame.from_options(connection_type="snowflake", connection_options={"autopushdown": "on", "query": "select \"Id\", \"Other field\" from DATABASE.COVE.TABLE where \"Id\" = 'some_id'", "connectionName": "Snowflake_Connection", "sfDatabase": "DATABASE"})

Snowflake_source = Snowflake_source.rename_field("\"Other field\"", "Other_Field__c").rename_field("\"Id\"", "Id")

Salesforce_target = glueContext.write_dynamic_frame.from_options(frame=Snowflake_source, connection_type="salesforce", connection_options={"connectionName": "Salesforce_Connection", "API_VERSION": "v62.0", "ENTITY_NAME": "Entity__c", "WRITE_OPERATION": "UPDATE", "TRANSFER_MODE": "SYNC", "FAIL_ON_FIRST_ERROR": "true"})

job.commit()

The Snowflake query returns 1 row with an Id that exists in Salesforce, so the Update operation should succeed. I confirmed through logging that the Snowflake_Source DF contains 1 record, after the field renaming, in this JSON format: {"Other_Field__c": 2024-11-19 13:25:37.0, "Id": "some_id"}.

But the above error occurs, apparently before a call is effectively made to Salesforce.

Update: The above error only occurs when the renamed fields that are sent to Salesforce do not have double quotes included in the name, as above. When double or single quotes are embedded in the name (eg. ""Other_Field__c"") then we get the error: "One or more records failed to be written. [WriteRecordResult{isSuccess=false, recordId=, errorMessage=[{"message":"At least 1 record is required","errorCode":"REQUIRED_FIELD_MISSING"}]}]". This indicates that the job is able to connect to Salesforce (also because the job is able to retrieve the Object schema from Salesforce according to the logs) but seems to be sending an empty request? Other_Field in this case is a timestamp in Snowflake, but I get the same results when converting it to a string.

asked a year ago203 views

2 Answers
0
Accepted Answer

The issue was that the Salesforce target's connection_options did not include the field "ID_FIELD_NAMES", which had to be set to "Id". This was not included since the documentation says this field is only needed for Upsert operations, which turns out to be false.

answered a year ago

AWS
EXPERT

reviewed a year ago

0

The error you're encountering "Index 0 out of bounds for length 0" when calling pyWriteDynamicFrame suggests there's an issue with the data being passed to the Salesforce connection.

Based on your description, there are several potential causes for this error:

  1. Data Format Issue: The error might be occurring because the DynamicFrame structure doesn't match what the Salesforce connector expects. The JSON format you mentioned shows the data is present, but there might be an incompatibility with how the Salesforce connector processes it.

  2. Connection Configuration: There could be an issue with the Salesforce connection configuration. The error occurs during the write operation, which suggests the problem is related to how the data is being prepared for writing to Salesforce.

  3. Field Mapping: While you've renamed the fields to match Salesforce field names, there might still be a mismatch in the expected format or data types.

Here are some troubleshooting steps you can try:

  1. Check CloudWatch Logs: Review the full Glue job logs in CloudWatch for additional error messages or stack traces that might provide more context about the failure.

  2. Verify Salesforce Connection: Ensure that your Salesforce connection is properly configured and has the necessary permissions to perform update operations on the specified entity.

  3. Data Transformation: Try explicitly converting your DynamicFrame to a specific format before writing:

# Convert to DataFrame and back to DynamicFrame to ensure proper structure
df = Snowflake_source.toDF()
clean_dynamic_frame = DynamicFrame.fromDF(df, glueContext, "clean_df")
  1. Resource Allocation: Verify that your Glue job has sufficient resources allocated. Resource constraints can sometimes cause unexpected errors.

  2. Test with a Smaller Dataset: If possible, try running the job with a simplified dataset to isolate whether it's a data-specific issue.

The error specifically mentions "pyWriteDynamicFrame" which is the method being called when you use write_dynamic_frame.from_options(). This suggests the issue is occurring at the interface between your Glue job and the Salesforce connector, likely related to how the data is being prepared or passed to Salesforce.
Sources
Error in AWS Glue ETL pipeline | AWS re:Post
An error occurred while calling o1109.pyWriteDynamicFrame - AWS Glue Job | AWS re:Post
Abstract DataSink class - AWS Glue

answered a year ago

  • The Salesforce connection seems to work correctly in the connection edit screen. Also, the update still fails if only the Id field is sent, so it's not likely a data issue.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.