5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DatabricksのServerless ComputeからPrivateLink経由で安全にS3接続する

Posted at

はじめに

こんにちは、KCCS デジタルプラットフォーム部の林です。
私たちの部門ではDatabricksを活用したデータ分析基盤の構築や技術検証を行なっています。

データ分析基盤の構築を行う際に他のシステム(AWSアカウント)と連携を行う要件で閉域接続のみアクセスが許可されているということがあります。
VPC内で稼働する汎用コンピュートであればPrivateLinkを構築して閉域接続することが可能ですが、Serverless Computeを利用している場合はPrivateLinkを経由して別のAWSアカウントのリソースへアクセスすることができませんでした。

しかし、最近のアップデートによりServerless ComputeでもPrivateLinkを使用した接続がサポートされるようになりました。
https://docs.databricks.com/aws/en/release-notes/product/2025/august#privatelink-connections-from-serverless-compute-to-resources-in-your-vpc-through-an-nlb-is-generally-available

今回はこの機能を利用してワークスペースが存在しているAWSアカウントとは別のAWSアカウントに存在するS3へServerless ComputeからPrivateLink経由でアクセスしてみたいと思います。

アーキテクチャ概要

databricks-s3-privatelink-architecture.jpg

この構成では、Databricks Serverless ComputeからS3への通信が完全にAWSのプライベートネットワーク内で完結します。

構築手順

以下のドキュメントを参考に設定していきます。
https://docs.databricks.com/aws/en/security/network/serverless-network-security/pl-to-internal-network

なお、AWSリソースの作成手順については概要だけ記載し、詳細な内容については割愛します。

Step 1: S3 VPC Interface Endpoint の作成

まず、VPC内にS3用のInterface Endpointを作成します。

  1. VPC コンソールでEndpoint作成

    # AWS CLIでの作成例
    aws ec2 create-vpc-endpoint \
      --vpc-id vpc-xxxxxxxxx \
      --service-name com.amazonaws.region.s3 \
      --vpc-endpoint-type Interface \
      --subnet-ids subnet-xxxxxxxxx \
      --security-group-ids sg-xxxxxxxxx \
      --policy-document file://s3-endpoint-policy.json
    
  2. セキュリティグループの設定

    • インバウンド: HTTPS (443) をNLBからのトラフィックを許可
    • アウトバウンド: 必要に応じてS3への通信を許可
  3. DNS設定の確認

    • プライベートDNS名が有効になっていることを確認
    • *.s3.region.vpce.amazonaws.com の名前解決が正常に動作することを確認

Step 2: Network Load Balancer (NLB) の作成

S3 Interface EndpointへのトラフィックをルーティングするInternal NLBを作成します。

  1. NLB作成

    aws elbv2 create-load-balancer \
      --name databricks-s3-nlb \
      --scheme internal \
      --type network \
      --subnets subnet-xxxxxxxxx subnet-yyyyyyyyy
    
  2. Target Group作成

    aws elbv2 create-target-group \
      --name s3-endpoint-targets \
      --protocol TCP \
      --port 443 \
      --vpc-id vpc-xxxxxxxxx \
      --target-type ip
    
  3. S3 Interface EndpointのIPアドレスをTarget Groupに登録

    # Interface EndpointのIPアドレスを取得
    aws ec2 describe-network-interfaces \
      --filters "Name=description,Values=*s3*" \
      --query 'NetworkInterfaces[*].PrivateIpAddress'
    
    # Target Groupに登録
    aws elbv2 register-targets \
      --target-group-arn arn:aws:elasticloadbalancing:region:account:targetgroup/s3-endpoint-targets/xxxxxxxxx \
      --targets Id=10.0.1.100,Port=443 Id=10.0.2.100,Port=443
    
  4. Listener作成

    aws elbv2 create-listener \
      --load-balancer-arn arn:aws:elasticloadbalancing:region:account:loadbalancer/net/databricks-s3-nlb/xxxxxxxxx \
      --protocol TCP \
      --port 443 \
      --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:account:targetgroup/s3-endpoint-targets/xxxxxxxxx
    

Step 3: VPC Endpoint Service の作成

NLBを使用してVPC Endpoint Serviceを作成します。

  1. VPC Endpoint Service作成

    aws ec2 create-vpc-endpoint-service-configuration \
      --network-load-balancer-arns arn:aws:elasticloadbalancing:region:account:loadbalancer/net/databricks-s3-nlb/xxxxxxxxx \
      --acceptance-required
    
  2. Databricks IAMロールの許可設定

    aws ec2 modify-vpc-endpoint-service-permissions \
      --service-id vpce-svc-xxxxxxxxx \
      --add-allowed-principals arn:aws:iam::565502421330:role/private-connectivity-role-ap-northeast-1
    
  3. PrivateLinkトラフィックの設定確認

    • "Enforce inbound rules on PrivateLink traffic" が Off になっていることを確認

Step 4: Databricks Network Connectivity Configuration (NCC)の作成

Databricks側でNetwork Connectivity Configurationを作成します。

  1. NCCオブジェクトの作成

    • Databricks Account Console → Cloud resources → Network → Network Connectivity Configurations
    • "Add Network Connectivity Configuration" をクリック
    • 名前を入力(例: s3-privatelink-ncc
    • リージョンを選択(ワークスペースと同じリージョン)
  2. Private Endpoint Ruleの作成

    • 作成したNCC → Private endpoint rules タブ → "Add private endpoint rule"
    • VPC Endpoint Service名を入力: com.amazonaws.vpce.region.vpce-svc-xxxxxxxxx
    • FQDN設定: *.s3.region.vpce.amazonaws.com
    • VPC Endpoint IDをコピーして保存

Step 5: VPC Endpoint接続の承認

AWS側でDatabricksからの接続要求を承認します。

  1. 接続要求の確認

    aws ec2 describe-vpc-endpoint-connections \
      --filters "Name=service-id,Values=vpce-svc-xxxxxxxxx"
    
  2. 接続の承認

    aws ec2 accept-vpc-endpoint-connections \
      --service-id vpce-svc-xxxxxxxxx \
      --vpc-endpoint-ids vpce-xxxxxxxxx
    

Step 6: 接続状態の確認

Databricks側で接続が正常に確立されたことを確認します。

  1. NCC詳細ページで状態確認

    • Private endpoint rules タブで Status が ESTABLISHED になることを確認
    • 接続確立まで数分かかる場合があります
  2. 接続テスト
    ノートブックからS3に対して接続確認を実施する(この時点ではまだワークスペースにNCCの適用はされていない)

    • Serverless ComputeでVPC Endpointを経由しない場合
    import boto3
    import os
    from botocore.exceptions import ClientError, NoCredentialsError
    
    def get_s3_files(bucket_name):
    
        # 環境変数を設定
        os.environ['AWS_ACCESS_KEY_ID'] = "access_key_id"
        os.environ['AWS_SECRET_ACCESS_KEY'] = "secret_access_key"
        os.environ['AWS_DEFAULT_REGION'] = "region"
    
        try:
            s3_client = boto3.client('s3')
            response = s3_client.list_objects_v2(Bucket=bucket_name)
        
        if 'Contents' not in response:
            print(f"バケット '{bucket_name}' は空です")
            return
        
        print(f"バケット '{bucket_name}' のファイル一覧:")
        for obj in response['Contents']:
            print(obj['Key'])
            
        except NoCredentialsError:
            print("エラー: AWS認証情報が設定されていません")
        except ClientError as e:
            print(f"エラー: {e}")
    
    if __name__ == "__main__":
        get_s3_files("bucket-name") #接続先バケット名
    
    バケット 'bucket-name' のファイル一覧
    test.txt
    

    ※バケット内の情報が表示される

    • Serverless ComputeでVPC Endpointを経由した場合
    import boto3
    import os
    from botocore.exceptions import ClientError, NoCredentialsError
    
    def get_s3_files(bucket_name,):
    
        # 環境変数を設定
        os.environ['AWS_ACCESS_KEY_ID'] = "access_key_id"
        os.environ['AWS_SECRET_ACCESS_KEY'] = "secret_access_key"
        os.environ['AWS_DEFAULT_REGION'] = "region"
    
        # VPCエンドポイント設定
        AWS_REGION = "region"
        vpc_endpoint_url = f"https://endpoint_url" #接続先エンドポイントのURL
    
        try:
            s3_client = boto3.client(
                's3',
                region_name=AWS_REGION,
                endpoint_url=vpc_endpoint_url
            )
        
            response = s3_client.list_objects_v2(Bucket=bucket_name)
        
            if 'Contents' not in response:
                print(f"バケット '{bucket_name}' は空です")
                return
        
            print(f"バケット '{bucket_name}' のファイル一覧:")
            for obj in response['Contents']:
                print(obj['Key'])
            
        except NoCredentialsError:
            print("エラー: AWS認証情報が設定されていません")
        except ClientError as e:
            print(f"エラー: {e}")
    
    if __name__ == "__main__":
        get_s3_files("buket-name") #接続先バケット名
    

    ※この時点ではまだエンドポイントへアクセスできず、タイムアウトになる

    • 汎用コンピュートでVPC Endpointを経由した場合
    バケット 'bucket-name' のファイル一覧
    test.txt
    

    ※汎用コンピュートはVPC内で稼働しているため、Endpointへアクセスが可能

Step 7: WorkspaceへのNCC適用

作成したNCCをDatabricks Workspaceに適用します。

  1. Workspace設定の更新

    • Account Console → Workspaces → 対象ワークスペース選択
    • "Update Workspace" をクリック
    • Network Connectivity Configuration で作成したNCCを選択
    • 複数のワークスペースに適用する場合は、各ワークスペースで同様の操作を実行
    • 設定の反映まで数分かかることがあります
  2. 接続テスト
    Serverless ComputeでVPC Endpointを経由した場合

    バケット 'bucket-name' のファイル一覧
    test.txt
    

    ※Serverless ComputeでもVPC endpoint経由でS3へのアクセスが可能になる

まとめ

NCCを利用することにより、Serverless ComputeからVPC Endpointを経由したアクセスが可能になりました。
今回はS3で確認しましたが、構成的にはVPC Endpointが指定できれば他のリソースでも同様に利用できると思われるので、セキュリティを保ちながら様々なリソースへアクセスすることができそうです。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?