Skip to content

Write Spark dataframe to Iceberg table in S3table

0

I am using Spark to read and write to a S3table bucket. I have configured it the following way:

spark = SparkSession.builder.appName("SparkIcebergSQL")
.config("spark.jars.packages", "org.apache.iceberg:iceberg-spark-runtime-3.4_2.12:1.4.2")
.config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
.config("spark.sql.defaultCatalog","s3tables")
.config("spark.sql.catalog.s3tables", "org.apache.iceberg.spark.SparkCatalog")
.config("spark.sql.catalog.s3tables.client.region", "eu-central-1")
.config("spark.sql.catalog.s3tables.glue.account-id", "account_number")
.config("spark.sql.catalog.s3tables.catalog-impl", "org.apache.iceberg.aws.glue.GlueCatalog")
.config("spark.sql.catalog.s3tables.glue.id","account_number:s3tablescatalog/qde-lakehouse-tables")
.config("spark.sql.catalog.s3tables.warehouse","s3://qde-lakehouse-tables/bucket/qde-lakehouse-tables")
.getOrCreate()

reading an iceberg table works: dim_table = spark.sql(f"SELECT * FROM {namespace}.{table_name_iceberg};") I have also tested if deleting a single record works and it does: spark.sql(f"DELETE FROM producer_caches.qis_pc_olaf_test_table WHERE id_kolom = '42';")

but now I want to write a spark dataframe to an iceberg table and then I ran into problems. I have tried: scd2_table.writeTo(f"{namespace}.{table_name_iceberg}")
.append()

and scd2_table.createOrReplaceTempView("temp_data") spark.sql(f""" INSERT INTO {namespace}.{table_name_iceberg} SELECT * FROM temp_data """) where scd2_table is a spark dataframe.

I have also looked at: https://repost.aws/questions/QUwVF6859STACze_vcy850QA/how-to-write-data-into-s3-table-buckets-with-glue-job-spark but that did not resolve my issue.

The error is: Exception Analysis] { "Event": "GlueExceptionAnalysisTaskFailed", "Timestamp": 1761296337778, "Failure Reason": "Access Denied (Service: S3, Status Code: 403, Request ID: A7NN7C8Z948JC5Z0, Extended Request ID: fRNOiZUKEl+UIUlw/BMflyPONOv6tSXe74BLaZvfH3gng0978YZybMtE5pl6qb/d85nZWtjFeqd6/2PGRTqSfsGalmUfQ00pcZQvkIVqy1k=)", "Stack Trace": [

it seems to be some permission issue but I can't figure out where. Anybody any ideas?

asked a year ago420 views

1 Answer
0

Based on your error message, you're encountering a permission issue when trying to write to your Iceberg table in an S3 table bucket. The "Access Denied (Service: S3, Status Code: 403)" error indicates that the IAM role or user executing your Spark job doesn't have sufficient permissions to write to the S3 location.

Here are some steps to resolve this issue:

  1. Check IAM permissions: Ensure your IAM role has the necessary permissions for both S3 and AWS Glue. You'll need at minimum:
  • s3:GetBucketLocation
  • s3:GetObject
  • s3:ListBucket
  • s3:PutObject
  • glue:* (or more specific Glue permissions)
  1. Verify S3 table bucket permissions: S3 table buckets have specific permission requirements. Make sure your role has the AmazonS3TablesFullAccess policy attached (which you mentioned you have).

  2. Check your Spark configuration: Your configuration looks mostly correct, but ensure the warehouse path is properly set. For S3 table buckets, the warehouse path should point to the S3 table bucket location.

  3. Try using explicit table properties: When writing to the table, you might want to specify additional table properties:

scd2_table.writeTo(f"{namespace}.{table_name_iceberg}") \
.option("write-format", "parquet") \
.tableProperty("format-version", "2") \
.append()
  1. Verify table existence: Make sure the table already exists before attempting to append data. If it doesn't exist, use .createOrReplace() instead of .append().

  2. Check cross-account permissions: If you're working across AWS accounts, ensure proper cross-account access is configured.

  3. Verify region settings: Ensure the region in your Spark configuration matches the region where your S3 table bucket is located.

If you're still encountering issues after checking these items, you might need to examine CloudTrail logs to see the specific permission that's being denied, or check if there are any bucket policies or ACLs restricting access to your S3 table bucket.
Sources
Working with Iceberg tables by using Apache Spark - AWS Prescriptive Guidance
Use an Iceberg cluster with Spark - Amazon EMR
Using the Iceberg framework in AWS Glue - AWS Glue
How to write data into S3 table buckets with Glue Job Spark | AWS re:Post

answered a year ago

  • My glue job has full s3 and s3table bucket permission and has ALL permission in lakeformation on ALL_TABLES. The suggestion did not resolve my problem.

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.