概要
AWS Glue Interactive Sessionsを使用して、JupyterノートブックでETLスクリプトをインタラクティブに開発・テストする環境を構築する具体的な手順を解説します。Glueの開発エンドポイントが廃止されたため、開発エンドポイントに代わる現在推奨される開発手法を実践的に紹介します。
目次
- はじめに
- AWS Glue Interactive Sessionsとは
- 開発エンドポイントからの移行について
- 前提条件と必要な権限
- Interactive Sessionsの環境構築
- ローカル環境でのJupyter Notebook設定
- SageMaker Studio でのGlue Interactive Sessions
- 実践的な使用例
- 注意点とベストプラクティス
- 終わりに
はじめに
AWS GlueでのETL開発において、AWS Glue Interactive Sessionsが現在の推奨手法となっています。従来の開発エンドポイントはGlue 1.0のみのサポートで新規開発が停止されているため、Interactive Sessionsを使用することで、Glue 2.0以降の最新機能を活用した柔軟で効率的なETL開発環境を構築できます。
AWS Glue Interactive Sessionsとは
AWS Glue Interactive Sessionsは、Jupyter互換のノートブックを使用してAWS Glueアプリケーションを視覚的に作成・テストできる柔軟な開発環境です。開発者は好みの環境からアプリケーションを構築・テストでき、AWS Command Line InterfaceとAPIを通じてインタラクティブセッションを作成・操作できます。
主な特徴
特徴 | 説明 | メリット |
---|---|---|
サーバーレス | インフラ管理不要 | 迅速な開発開始 |
柔軟性 | 複数の開発環境に対応 | 開発者の好みに合わせた環境選択 |
スケーラビリティ | Glue 3.0以降でAuto Scalingが利用可能 | ワークロードに応じた自動リソース調整 |
統合性 | AWS各種サービスとの連携 | 既存のAWSワークフローとの統合 |
開発エンドポイントからの移行について
開発エンドポイントの現状(2025年7月現在)
AWS Glue開発エンドポイントは現在も技術的には利用可能ですが、以下の制限と課題があります:
- Glue 1.0のみサポート: 開発エンドポイントはAWS Glue version 2.0以降をサポートしていません(AWS re:Post - https://repost.aws/questions/QU-6NoqlmdTguB267GOkUVDg/aws-glue-2-0-3-0-are-not-available-in-dev-endpoints )
- 新規開発の停止: 新規開発は行われておらず、すべての新規顧客にはInteractive Sessionsの使用が推奨されている状況です(AWS re:Post - https://repost.aws/questions/QUCx-7A3TKR8ODRmhZayQ4KA/development-endpoint-glue-version-1-0 )
- 非推奨扱い: サードパーティのガバナンスツールでは既に「[Deprecated]」として扱われているケースが見受けられます
現在の推奨事項
AWSは現在、インタラクティブな開発において以下の方法を推奨しています:
- AWS Glue Interactive Sessions(本記事の主要テーマ)
- Glue Studio Notebooks
- SageMaker Studio with Glue kernels
これらの新しいソリューションは、Glue 2.0以降の最新機能とパフォーマンス向上を活用できます。
移行のメリット
- Glue 2.0以降の最新機能とパフォーマンス向上
- 簡略化されたセットアップと管理
- より柔軟な開発環境の選択
- コスト効率の向上
前提条件と必要な権限
AWS IAMロールの設定
Interactive Sessionsを使用するために、以下の権限を含むIAMロールを作成します:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"glue:CreateSession",
"glue:GetSession",
"glue:ListSessions",
"glue:RunStatement",
"glue:GetStatement",
"glue:ListStatements",
"glue:CancelStatement",
"glue:StopSession"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::your-bucket-name",
"arn:aws:s3:::your-bucket-name/*"
]
}
]
}
必要な権限(SageMaker Studio使用の場合)
Interactive Sessionsを使用する場合、IAMロールにTagResourceとGetTagsの権限を追加する必要があります:
{
"Effect": "Allow",
"Action": [
"glue:TagResource",
"glue:GetTags"
],
"Resource": "*"
}
Interactive Sessionsの環境構築
1. AWS CLI設定の確認
aws configure list
aws glue list-sessions
2. 必要なパッケージのインストール
AWS Glue Interactive Sessionsをインストールするには、Jupyter、Boto3、およびAWS Glue Interactive SessionsをPyPiからインストール・アップグレードします:
pip3 install --user --upgrade jupyter boto3 aws-glue-sessions
3. セッションの作成と管理
Interactive Sessionsの履歴は、AWS Glue StudioのInteractive Sessionsタブで確認できます。
ローカル環境でのJupyter Notebook設定
1. Jupyter Notebookの起動
jupyter notebook
2. Glue Magicコマンドの使用
Jupyter Magicsは、セルの先頭や全体で実行できるコマンドで、%で始まるline-magicsと%%で始まるcell-magicsがあります。
新しいノートブックで以下のセルを実行してGlueセッションを開始:
%load_ext awsglue_jupyter
%%configure
{
"--glue_version": "3.0",
"--number_of_workers": 2,
"--worker_type": "G.1X",
"--enable-metrics": "",
"--enable-continuous-cloudwatch-log": "true"
}
3. セッションの開始
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
# Glue Contextの初期化
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
SageMaker Studio でのGlue Interactive Sessions
1. SageMaker Studio環境の準備
SageMaker StudioからGlue Interactive Sessionsを使用する場合の手順:
- SageMaker Studioを開く
- 新しいNotebookを作成
- カーネルとして「Glue PySpark」または「Glue Scala」を選択
2. 基本的な設定
%glue_version 3.0
%worker_type G.1X
%number_of_workers 2
%region us-east-1
# セッション開始
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
glueContext = GlueContext(SparkContext.getOrCreate())
【挿絵候補】:SageMaker StudioでのGlue Interactive Sessions設定画面の流れ(カーネル選択→Magic設定→セッション開始の手順を表現したフロー図)
実践的な使用例
S3データの読み取りと変換例
# S3からデータを読み取り
datasource = glueContext.create_dynamic_frame.from_catalog(
database="your_database",
table_name="your_table"
)
# データの確認
datasource.show()
# データ変換
transformed_data = ApplyMapping.apply(
frame=datasource,
mappings=[
("old_column", "string", "new_column", "string"),
("id", "int", "id", "int")
]
)
# 結果をS3に保存
glueContext.write_dynamic_frame.from_options(
frame=transformed_data,
connection_type="s3",
connection_options={"path": "s3://your-bucket/output/"},
format="parquet"
)
セッション情報の確認
# セッション情報を確認
%session_info
# 現在のセッションID取得
%session_id
注意点とベストプラクティス
セッション管理
- セッションの適切な停止: 使用後は必ずセッションを停止してコストを節約
- Auto Scalingの活用: Glue 3.0以降では自動スケーリングが利用可能
- セッション履歴の確認: 定期的にセッション履歴を確認し、不要なセッションを削除
セキュリティ
- IAM権限の最小化: 必要最小限の権限のみを付与
- VPCエンドポイントの活用: 可能な限りプライベート接続を使用
- データ暗号化: S3バケットでの暗号化設定
コスト最適化
Interactive Sessionsの料金は使用時間に基づいて課金されます。AWS公式ドキュメント( https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions.html )によると、セッションが実行されている間のみ料金が発生するため、開発完了後は速やかにセッションを停止することが重要です。
# セッション停止
%stop_session
終わりに
AWS Glue Interactive Sessionsは、従来の開発エンドポイントに代わる現代的で効率的なETL開発手法です。柔軟性とスケーラビリティを兼ね備えており、開発者の生産性向上に大きく寄与します。
開発エンドポイントからの移行を検討している場合は、段階的にInteractive Sessionsを導入し、新しい開発フローに慣れていくことを推奨します。特に、Auto Scalingなどの最新機能を活用することで、より効率的なETL開発が可能になります。
今後のAWS Glueの機能拡張にも期待が持てるため、継続的に最新情報をキャッチアップして、最適な開発環境を維持していくことが重要です。
参考文献・参考サイト
- 「Getting started with AWS Glue interactive sessions」AWS Documentation, https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions.html
- 「Building AWS Glue jobs with interactive sessions」AWS Documentation, https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions-chapter.html
- 「Configuring AWS Glue interactive sessions for Jupyter and AWS Glue Studio notebooks」AWS Documentation, https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions-magics.html
- 「Get Started with AWS Glue Interactive Sessions」AWS Documentation, https://docs.aws.amazon.com/sagemaker/latest/dg/getting-started-glue-sm.html
- 「Setting up networking for development for AWS Glue」AWS Documentation, https://docs.aws.amazon.com/glue/latest/dg/start-development-endpoint.html
- 「Developing scripts using development endpoints」AWS Documentation, https://docs.aws.amazon.com/glue/latest/dg/dev-endpoint.html
- 「Development Endpoint & Glue Version 1.0」AWS re:Post, https://repost.aws/questions/QUCx-7A3TKR8ODRmhZayQ4KA/development-endpoint-glue-version-1-0
- 「AWS Glue 2.0/3.0 are not available in Dev endpoints」AWS re:Post, https://repost.aws/questions/QU-6NoqlmdTguB267GOkUVDg/aws-glue-2-0-3-0-are-not-available-in-dev-endpoints
- 「Introducing AWS Glue interactive sessions for Jupyter」AWS Blog, https://aws.amazon.com/blogs/big-data/introducing-aws-glue-interactive-sessions-for-jupyter/
- 「Auto Scaling in AWS Glue interactive sessions is now generally available」AWS What's New, https://aws.amazon.com/about-aws/whats-new/2024/10/auto-scaling-aws-glue-interactive-sessions/