3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS GlueのCrawler(クローラ)をAWS CDKで生成する

Posted at

概要

  • S3にたくさん投入したデータをAthenaでサクッと分析・集計できるようにする際、Glue Crawlerが非常に便利。
  • CDKで構築した例はまだあまりなかったので記事化。

S3の設定について

  • 先日の記事で考察したが、S3はいまのところ手動設定したものを利用しても良いように思う。

Glueの設定について

ソースコード例

たとえばこんな感じ。


import * as cdk from '@aws-cdk/core';
import * as iam from '@aws-cdk/aws-iam';
import * as glue from '@aws-cdk/aws-glue';
import * as s3  from '@aws-cdk/aws-s3';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myBucketName = 'my-bucket';
    const myCrawlerName = 'myCrawler';
    const myCrawlerRoleName = 'myCrawlerRole';
    const myAthenaDBName = 'my_db';

    const myBucket = s3.Bucket.fromBucketName(this, myBucketName, myBucketName);

    const myCrawlerRole = new iam.Role(this, myCrawlerRoleName, {
      assumedBy: new iam.ServicePrincipal('glue.amazonaws.com')
    });
    myCrawlerRole.addManagedPolicy(
      iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSGlueServiceRole'));
    myCrawlerRole.addToPolicy(
      new iam.PolicyStatement({
        effect: iam.Effect.ALLOW,
        resources: [`${myBucket.bucketArn}/path-to-object-parent/*`],
        actions: ["s3:GetObject", "s3:PutObject"]
      })
    );

    const myCrawler = new glue.CfnCrawler(this, myCrawlerName, {
      role: myCrawlerRole.roleArn,
      databaseName: myAthenaDBName,
      targets: {
        s3Targets: [{ path: `${myBucket.s3UrlForObject()}/path-to-object-parent/` }]
      },
      configuration: '{"Version": 1.0, "Grouping": {"TableGroupingPolicy": "CombineCompatibleSchemas"}}'
    });
  }
}

まだ良くわからないこと

わかる方いらっしゃいましたら教えてほしいです。

  • Constructの「id」の活用方法。今のところ適当。
  • S3もCDKで管理できたら良いけど、CDK(というかIaC?)の利点を活かせるやりかたは今のところ不明。
  • TypeScriptはまだ1週間ほどしか触っていないので、いろいろ不安。
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?