【トラブル】【AWS】AWS Glue のトラブル ~ job編 - [2] ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/10/25/232155

の続き。

AWS Glue の Job に関するトラブルシューティングが
長くなってしまったので、分冊する。

目次

【1】エラー「At least one security group must open all ingress ports」が発生
【2】エラー「An error occurred (AccessDeniedException) when calling the <Glue API名> operation」が表示
【3】エラー「message:Insufficient Lake Formation permission(s) on xxxx」が表示
【4】CreateTableした際にエラー「Lake Formation permission(s)」が表示

その他のGlueに関するトラブルシューティングは、以下の関連記事を参照のこと

boto3 AWS Glue API のトラブル ~ trigger全般 編 ~
https://dk521123.hatenablog.com/entry/2020/10/23/110821
boto3 AWS Glue API のトラブル ~ scheduled trigger編 ~
https://dk521123.hatenablog.com/entry/2020/01/16/205331
boto3 AWS Glue API のトラブル ~ job/crawler編 ~
https://dk521123.hatenablog.com/entry/2020/02/05/223307
AWS Glue のトラブル ~ job編 - [2] ~
https://dk521123.hatenablog.com/entry/2020/10/12/152659
AWS Glue のトラブル ~ job編 - [3] ~
https://dk521123.hatenablog.com/entry/2021/02/16/145848
AWS Glue のトラブル ~ crawler編 ~
https://dk521123.hatenablog.com/entry/2020/05/07/144132

【1】エラー「At least one security group must open all ingress ports」が発生

Jobからエラー「At least one security group must open all ingress ports」が発生

エラー内容

JobName: xxx and JobRunId:jr_xxx failed to execute with exception
At least one security group must open all ingress ports.
To limit traffic, the source security group in your inbound rule can be restricted
to the same security group. 

原因

セキュリティグループのInbound Rules設定の不備

詳しくは、以下の公式サイトのトラブルシューティングを参照。

https://docs.aws.amazon.com/ja_jp/glue/latest/dg/glue-troubleshooting-errors.html#error-inbound-self-reference-rule

より抜粋
~~~~
少なくとも 1 つのセキュリティグループの
すべての受信ポートを開く必要があります。
~~~~

解決案

セキュリティグループのInbound Rules設定で、
Glueが実行できるように設定する(穴をあける)

原因の切り分けのため、以下を試してみて解決するかどうか試してみるのも手かと、、、
~~~~
All TCP | TCP | 0-65535 | 対象セキュリティグループ

※
あくまで、原因調査の一時しのぎで、セキュリティ的によろしくないと思うので
ネットワーク周りに強い人に相談して、自分のシステム要件にあった
最終解決策を見出してください
~~~~

空け方は、以下の公式サイトも参考になる

https://docs.aws.amazon.com/ja_jp/glue/latest/dg/setup-vpc-for-glue-access.html

参考文献

https://stackoverflow.com/questions/51374325/at-least-one-security-group-must-open-all-ingress-ports-aws-glue-connecting-to

【2】エラー「An error occurred (AccessDeniedException) when calling the operation」が表示

エラー内容

botocore.exceptions.ClientError:
An error occurred (AccessDeniedException) when calling the <Glue API名> operation:
User: arn:aws:sts::xxxxx:assumed-role/<使用しているロール>/GlueJobRunnerSession
 is not authorized to perform:
glue: <Glue API name(今回の場合「GeWorkflowRunProperites」)> on resource:
 arn:aws:glue:<AWS region>::xxxxx:<Job name>

原因

* 使用しているロールにおいて、IAM Policy でGlue APIの使用を制限されていたため
* IAM Policy については、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/09/30/133123

発生例

* 対象JobでGlue API「GeWorkflowRunProperites」を使用しているが
 以下の「アクセス制御例」の「 "Action": ["glue:StartJobRun", "glue:StartWorkflowRun"]」で
 「glue:StartJobRun」と「glue:StartWorkflowRun」しか実行権限がない

https://dk521123.hatenablog.com/entry/2020/09/29/165636
アクセス制御例

{
  "Version": "2012-10-17",
  "Id": "GluePolicyId1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["glue:StartJobRun", "glue:StartWorkflowRun"],
      "Resource": ”*"
      ]
    }
  ]
}

解決案

* 原因をロールに使用するAPIを追加する
 etc...

【3】エラー「message:Insufficient Lake Formation permission(s) on xxxx」が表示

以下「サンプル」のように spark.sql() を実行したところ、
以下の「エラー内容」の例外が発生した。

サンプル

# sql() を実行したところ、例外が発生する
data_frame = spark.sql('SELECT DISTINCT ....')

エラー内容

'org.apache.hadoop.hive.ql.metadata.HiveException:
MetaException(message:Insufficient Lake Formation permission(s) on global_temp
 (Service: AWSGlue; Status Code: 400; Error Code: AccessDeniedException;
Request ID: xxxx-xxxxx-xxxxx));'

原因

* Glue Jobに紐づいているIAMロールにデータアクセス許可がないため

https://docs.aws.amazon.com/ja_jp/emr/latest/ManagementGuide/emr-troubleshoot-lf.html

に記載されている「リクエストされたテーブルでユーザーのアクセス許可がありません」
と同等の現象と思われる。

確認の仕方

AWS マネージメントコンソールから、
[AWS Lake Formation]-[Admins and database creators]を選択し、
Glue Jobで使用している IAM role が存在しているか確認。

もし、存在している場合は、
[AWS Lake Formation]-[Admins and database creators]-[Data permission] を選択し
Glue Jobに設定しているIAMロールが制限されていないか確認。

解決案

* [AWS Lake Formation]-[Admins and database creators] で 使用しているロールを追加

* [AWS Lake Formation]-[Admins and database creators]-[Data permission] で
 Glue Jobに設定しているIAMロールの対象リソースにアクセスできるようにする。

* その一覧に紐づいているロールをGlue Jobに紐づけ、対象リソースを追加する。

などなど。

https://docs.aws.amazon.com/ja_jp/emr/latest/ManagementGuide/emr-troubleshoot-lf.html

より抜粋
~~~~~~~
テーブルにアクセスするには、Lake Formation で
このテーブルに関連付けられたアクセス許可を更新して、
ユーザーにアクセス許可を付与する必要があります。
~~~~~~~

参考文献

https://forums.aws.amazon.com/thread.jspa?messageID=933752
https://yohei-a.hatenablog.jp/entry/20191217/1576538518

別件:エラー「Insufficient Lake Formation permission(s) on default」が表示

以下の関連記事を参照のこと

EMRのStep実行時にエラー「Insufficient Lake Formation permission(s) on default」が表示
https://dk521123.hatenablog.com/entry/2022/05/09/101121

【4】CreateTableした際にエラー「Lake Formation permission(s)」が表示

Glue Jobから boto3 API「CreateTable」を実行した際に
以下「エラー内容」が表示される

https://dk521123.hatenablog.com/entry/2021/06/11/164015

ソース抜粋(イメージ)

import boto3

glue_client = boto3.client('glue', region_name='us-west-2')
response = client.create_table(
    DatabaseName='sample_db',
    TableInput={
        'Name': 'sample_table',
            'Location': 's3://xxx/xxxx/xxxx',
            },

エラー内容

An error occurred (AccessDeniedException)
when calling the CreateTable operation:
Insufficient Lake Formation permission(s):
Requred create Table on xxxxxx

原因

* Location のパスに誤りがあったため

対応案

* Location のパスを正しく修正

関連記事

AWS Glue ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/01/221926
boto3 AWS Glue API のトラブル ~ trigger全般 編 ~
https://dk521123.hatenablog.com/entry/2020/10/23/110821
boto3 AWS Glue API のトラブル ~ scheduled trigger編 ~
https://dk521123.hatenablog.com/entry/2020/01/16/205331
boto3 AWS Glue API のトラブル ~ job/crawler編 ~
https://dk521123.hatenablog.com/entry/2020/02/05/223307
AWS Glue のトラブル ~ job編 - [1] ~
https://dk521123.hatenablog.com/entry/2019/10/25/232155
AWS Glue のトラブル ~ job編 - [3] ~
https://dk521123.hatenablog.com/entry/2021/02/16/145848
AWS Glue のトラブル ~ job編 - [4] ~
https://dk521123.hatenablog.com/entry/2021/05/10/143328
AWS Glue のトラブル ~ crawler編 ~
https://dk521123.hatenablog.com/entry/2020/05/07/144132
Glue Jobを起動した際にエラー「LANCH ERROR | java.net.URISyntaxException」が発生
https://dk521123.hatenablog.com/entry/2022/11/18/163207
AWS Lake Formation ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2020/10/13/094041
EMRのStep実行時にエラー「Insufficient Lake Formation permission(s) on default」が表示
https://dk521123.hatenablog.com/entry/2022/05/09/101121
AWS Glue ~ Boto3 / DB・テーブル操作編 ~
https://dk521123.hatenablog.com/entry/2021/06/11/164015