0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

S3テーブルを、CloudFormationとCLIで作成

Posted at

はじめに

最近興味が出てきたS3 Tableを、コンソールのGUIを使わずに作ってみようと思い、やってみました。
CloudFormationだけでは出来ず、分析サービスへ統合するところにCLIが必要になりました。また削除もひと手間かかりましたので、その方法を記事にしました。

概要

CloudFormationで以下のリソースを作ります。

  • IAMロール:S3TablesRoleForLakeFormation
  • S3テーブルバケット
  • S3テーブルの名前空間
  • S3テーブル

テーブルバケットを統合するために必要な以下のリソースは、CloudFormationで作成できなかったので、CLIから作成します。

  • Lake Formation にリソース登録
    • CloudFormationにAWS::LakeFormation::Resourceはあるものの、S3 Tableには対応していない模様
  • AWS Glue Data Catalog

参考

作成

S3テーブル・ロール作成

まず、以下のCFnテンプレートからリソースを作成します。

create_S3Table.yaml
AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  TableBucketName:
    Type: String
    Default: sample-s3table-tablebucket6201
  NamespaceName:
    Type: String
    Default: sample_s3table_namespace
  TableName:
    Type: String
    Default: sample_s3table
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Parameters:
          - TableName
          - NamespaceName
          - TableBucketName

Resources:
  # S3TablesRoleForLakeFormation
  ## すでに自動で作られている場合は不要
  S3TablesRoleForLakeFormation:
    Type: AWS::IAM::Role
    Properties:
      RoleName: S3TablesRoleForLakeFormation
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Sid: LakeFormationDataAccessPolicy
            Effect: Allow
            Principal:
              Service: lakeformation.amazonaws.com
            Action:
              - sts:AssumeRole
              - sts:SetContext
              - sts:SetSourceIdentity
            Condition:
              StringEquals:
                aws:SourceAccount: !Ref AWS::AccountId
      Policies:
        - PolicyName: LakeFormationS3TablesInlinePolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Sid: LakeFormationPermissionsForS3ListTableBucket
                Effect: Allow
                Action:
                  - s3tables:ListTableBuckets
                Resource: "*"
              - Sid: LakeFormationDataAccessPermissionsForS3TableBucket
                Effect: Allow
                Action:
                  - s3tables:CreateTableBucket
                  - s3tables:GetTableBucket
                  - s3tables:CreateNamespace
                  - s3tables:GetNamespace
                  - s3tables:ListNamespaces
                  - s3tables:DeleteNamespace
                  - s3tables:DeleteTableBucket
                  - s3tables:CreateTable
                  - s3tables:DeleteTable
                  - s3tables:GetTable
                  - s3tables:ListTables
                  - s3tables:RenameTable
                  - s3tables:UpdateTableMetadataLocation
                  - s3tables:GetTableMetadataLocation
                  - s3tables:GetTableData
                  - s3tables:PutTableData
                Resource:
                  - !Sub "arn:aws:s3tables:${AWS::Region}:${AWS::AccountId}:bucket/*"

  MyTableBucket:
    Type: AWS::S3Tables::TableBucket
    Properties:
      TableBucketName: !Ref TableBucketName

  MyTableNamespace:
    Type: AWS::S3Tables::Namespace
    Properties:
      Namespace: !Ref NamespaceName
      TableBucketARN: !GetAtt MyTableBucket.TableBucketARN

  MyTable:
    Type: AWS::S3Tables::Table
    Properties:
      IcebergMetadata:
        IcebergSchema:
          SchemaFieldList:
            - Name: "id"
              Required: true
              Type: "int"
            - Name: "name"
              Type: "string"
      Namespace: !Ref NamespaceName
      OpenTableFormat: "ICEBERG"
      TableBucketARN: !GetAtt MyTableBucket.TableBucketARN
      TableName: !Ref TableName
    DependsOn: MyTableNamespace

リソース登録

次は以下を参考に、CLIにてLake Formation にリソース登録します。

インプット用のファイルを、以下のように作ります

input.json
{
    "ResourceArn": "arn:aws:s3tables:ap-northeast-1:123456789012:bucket/*",

    "WithFederation": true,
    "RoleArn": "arn:aws:iam::123456789012:role/S3TablesRoleForLakeFormation"
}                

作成したファイルをインプットに、以下のコマンドを実行します。

aws lakeformation register-resource \
--region ap-northeast-1 \
--with-privileged-access \
--cli-input-json file://input.json

カタログ作成

最後に以下を参考に、CLIにてカタログを作成します。

インプット用のファイルを、以下のように作ります

catalog.json
{
   "Name": "s3tablescatalog",
   "CatalogInput": {
      "FederatedCatalog": {
          "Identifier": "arn:aws:s3tables:ap-northeast-1:123456789012:bucket/*",
          "ConnectionName": "aws:s3tables"
       },
       "CreateDatabaseDefaultPermissions":[],
       "CreateTableDefaultPermissions":[],
       "AllowFullTableExternalDataAccess": "True"
   }
}

以下のコマンドを実行します。

aws glue create-catalog \
--region ap-northeast-1 \
--cli-input-json file://catalog.json

これでS3テーブルが使えるようになりました。

INSERT INTO "sample_s3table" (id, name) VALUES (1, 'hogehoge');
image.png
image.png

削除

作成した逆順に削除してきます。

カタログ削除

まずはカタログから削除します。
削除するにはIDが必要なので、CLIから取得して削除コマンドの引数に使います。

# 以下の結果からカタログIDを取得
aws glue get-catalogs

aws glue delete-catalog \
--catalog-id [カタログID]

リソース削除

リソースを削除していきます。

aws lakeformation deregister-resource \
--resource-arn "arn:aws:s3tables:ap-northeast-1:123456789012:bucket/*"

S3テーブル削除

次は、最初に作成したCloudFormationスタックを削除します。

permission削除

実は、CLI実行時にpermissionも作られていますので、それを削除します。
image.png
コンソールから削除するのが楽ですが、これもCLIから消していきます。

削除するために、以下のコマンドで情報を取得します。

aws lakeformation list-permissions

以下のような出力結果から、PrincipalResourcePermissionsの中の対象のpermissionを抽出して、入力用ファイルrevoke.jsonとして保存します。

{
    "PrincipalResourcePermissions": [{
        "Principal": {
            "DataLakePrincipalIdentifier": "arn:aws:iam::123456789111:user/lf-campaign-manager"
        },
        "Resource": {
            "Database": {
                "CatalogId": "123456789111",
                "Name": "tpc"
            }
        },
        "Permissions": [
            "DESCRIBE"
        ],
        "PermissionsWithGrantOption": []
    }],
    "NextToken": "E5SlJDSTZleUp6SWpvaU9UQTNORE0zTXpFeE5Ua3pJbjE5TENKbGVIQnBjbUYwYVc5dUlqcDdJbk5sWTI5dVpITWlPakUyTm"
}
revoke.json
{
    "Principal": {
        "DataLakePrincipalIdentifier": "arn:aws:iam::123456789111:user/lf-campaign-manager"
    },
    "Resource": {
        "Database": {
            "CatalogId": "123456789111",
            "Name": "tpc"
        }
    },
    "Permissions": [
        "DESCRIBE"
    ],
    "PermissionsWithGrantOption": []
}

以下を参考に、permissionを削除します。

aws lakeformation revoke-permissions \
    --cli-input-json file://revoke.json

おわりに

S3テーブルを、CloudFormationとCLIから作って、削除してみました。
コンソールからだと良しなに作成してくれる部分で、どのようなことが行われているのか勉強になりました。

この記事がどなたかのお役に立ちましたら幸いです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?