Using governed UDFs with Lake Formation on Amazon EMR on EC2
With emr-spark-8.2.0 and higher, you can call governed user-defined
functions (UDFs) from Spark jobs on clusters with Lake Formation fine-grained access control. A governed UDF is a Data Catalog function
that Lake Formation controls. Its code is a JAR file in Amazon S3, the custom code
artifact. A step cannot declare governed UDFs until two administrators turn
them on:
-
The data lake administrator creates the governed UDF and grants
EXECUTEon it to the job runtime role. -
The cluster administrator turns on governed UDFs in the security configuration and lists the approved governed UDFs.
Lake Formation fine-grained access control on Amazon EMR on EC2 requires runtime roles. The job runtime role needs the permissions for governed UDFs. For the runtime role setup, see Prerequisites.
This page shows the Amazon EMR on EC2 steps only. For all other information, see Using governed UDFs with Lake Formation fine-grained access control in the Amazon EMR Release Guide.
Turn on governed UDFs in the security configuration
Add the CustomArtifactConfiguration section to the security
configuration that you use for Lake Formation.
You cannot change a security configuration, so create a new one.
CustomArtifactConfiguration requires
LakeFormationConfiguration, so a security configuration for full table
access cannot turn on governed UDFs.
-
Copy the JSON of your current Lake Formation security configuration to a file.
-
Add the
CustomArtifactConfigurationsection, as the following JSON example shows. Replaceregion,111122223333,sales_db, andmask_namewith your values."CustomArtifactConfiguration": { "UdfEnabled": true, "ApprovedUdfArns": [ "arn:aws:glue:region:111122223333:userDefinedFunction/sales_db/mask_name" ] }The following table describes the fields.
CustomArtifactConfiguration fields Field Description UdfEnabledSet to trueto let steps declare governed UDFs. The default isfalse.ApprovedUdfArnsThe governed UDF ARNs that steps can declare. Use exact userDefinedFunctionARNs. The list can have up to 100 ARNs. Do not include empty or duplicate entries. -
Use the AWS CLI to create the security configuration.
aws emr create-security-configuration \ --name lf-governed-udfs \ --security-configuration file://lf-governed-udfs-sec-cfg.json -
Launch the cluster with the new security configuration.
Declare governed UDFs in a step
A step must declare each governed UDF that it calls. Amazon EMR compares the
declaration with ApprovedUdfArns.
-
Set
spark.aws.lakeformation.artifactsto a comma-separated list of governed UDF ARNs. -
In your Spark code, call each UDF by its database-qualified name.
-
Use the AWS CLI to add the step, as the following example shows. Replace
j-XXXXXXXXXXXXX,region,111122223333,JobRuntimeRole,sales_db,mask_name, andamzn-s3-demo-bucketwith your values.aws emr add-steps \ --cluster-idj-XXXXXXXXXXXXX\ --execution-role-arn arn:aws:iam::111122223333:role/JobRuntimeRole\ --steps '[{ "Type": "CUSTOM_JAR", "Name": "governed-udf-query", "ActionOnFailure": "CONTINUE", "Jar": "command-runner.jar", "Args": ["spark-submit", "--conf", "spark.aws.lakeformation.artifacts=arn:aws:glue:region:111122223333:userDefinedFunction/sales_db/mask_name", "s3://amzn-s3-demo-bucket/scripts/query.py"] }]'The example uses the JSON form of
--steps. Use this form when you declare more than one ARN, because the shorthand form splits the value at each comma.
Amazon EMR checks the declaration when you call RunJobFlow or
AddJobFlowSteps. If the check fails, Amazon EMR does not create the cluster or
the step. The response shows one of these errors:
-
Lake Formation artifacts were declared, but the cluster's security configuration does not enable custom artifacts (UDFs).SetUdfEnabledtotrue. Amazon EMR also shows this error when the account or the release does not support governed UDFs. -
Lake Formation artifactAdd the exact ARN toarnis not in the approved artifact list of the cluster's security configuration.ApprovedUdfArns.
Amazon EMR checks only the ARNs in the step arguments. If you declare governed UDFs in a different way, for example through Livy, the cluster checks the declaration when the Spark driver starts.
Find governed UDF errors
Errors from system space start and security validation errors appear in the step
stderr log, in
.
In cluster mode, look in the log of the YARN application master container of your
application. The step LogUri/cluster-id/steps/step-id/FailureDetails can show only Unknown
Error. Search the log for these texts:
System driver failed to start:SECURITY ERROR. EXITING JVM.reason = SparkSecurityValidationExceptionMulti Dialect ViewandSQL contains unsupported operations
Usually the step log is sufficient. When a step shows Spark FGAC experienced
an internal error(, give the ID to the cluster
administrator. As the cluster administrator, search the system space logs on the core
nodes for the ID:id)
sudo grep -rlid/var/log/hadoop-yarn/containers/system
Amazon EMR also copies these logs to
.
The copy can take a few minutes. The logs can contain row-level security (RLS) filter expressions and view
definitions. Give read access to the LogUri/cluster-id/containers/system/LogUri location only to cluster
administrators.