【Tips】Redshift Spectrumでport 443: Connection timed outのエラーが出る原因と解決策

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Amazon S3とAWS Glue (Amazon Athena)への権限を付与した適切なIAM RoleをRedshiftクラスタにアタッチしているのに、外部スキーマや外部テーブルがが作成できないという現象が起きた際、Redshiftクラスタに設定したEnhanced VPC routingが原因の可能性があります。

以下、詳細を解説していきます。

現象

通常時、Redshift Spectrumで外部テーブルを作成するには、AWS GlueをサポートしているリージョンではAmazon S3とGlue Data Catalog、GlueをサポートしていないリージョンではAmazon S3とAthena Data Catalogへのアクセス権を付与したIAM Roleをアタッチする必要があります。東京リージョンでは、AWS Glueがサポートされているため、前者の権限が要求されます。

ためしに、東京リージョンに構築したRedshiftクラスタに対して、Amazon S3とAthenaへのフルアクセス権を付与したIAM Role s3-and-athena-for-redshift をアタッチし、外部スキーマとサンプルテーブルを作成してみます。サンプルデータはCreating external tables for Amazon Redshift Spectrum - Amazon Redshiftから使用しています。

create external schema s3_and_athena
from data catalog 
database 'spectrumdb'
iam_role 'arn:aws:iam::XXXXXXXXXXXX:role/s3-and-athena-for-redshift'
create external database if not exists;

create external table s3_and_athena.sales_athena(
salesid integer,
listid integer,
sellerid integer,
buyerid integer,
eventid integer,
dateid smallint,
qtysold smallint,
pricepaid decimal(8,2),
commission decimal(8,2),
saletime timestamp)
row format delimited
fields terminated by '\t'
stored as textfile
location 's3://<my-bucket>/spectrum/'
table properties ('numRows'='172000');

作成できてしまいました。東京リージョンでもAthena Data Catalogは機能しているみたいです。Amazon S3とGlueへのフルアクセス権を付与したIAM Role s3-and-glue-for-redshift をアタッチし、外部スキーマとサンプルテーブルを作成してみます。

create external schema s3_and_glue
from data catalog 
database 'spectrumdb'
iam_role 'arn:aws:iam::XXXXXXXXXXXX:role/s3-and-glue-for-redshift'
create external database if not exists;

create external table s3_and_athena.sales_glue(
salesid integer,
listid integer,
sellerid integer,
buyerid integer,
eventid integer,
dateid smallint,
qtysold smallint,
pricepaid decimal(8,2),
commission decimal(8,2),
saletime timestamp)
row format delimited
fields terminated by '\t'
stored as textfile
location 's3://<my-bucket>/spectrum/'
table properties ('numRows'='172000');

こちらも問題なく作成できました。

さて、同じことをRedshiftのEnhanced VPC routing(拡張されたVPCのルーティング)を有効化した状態で行ってみます。マネジメントコンソールで対象のRedshiftクラスタをクリックし、プロパティタブのNetwork and security settingsパネル内にある以下の部分を編集から有効化してください。

この状態だと、Athena Data CatalogでもGlue Data Catalogでも、テーブルはおろか外部スキーマすら作れなくなります。クエリを投げてもしばらく応答がない状態で、最終的に以下のように443: Connection timed outとなるはずです。

[Amazon](500310) Invalid operation: Failed to perform AWS request, curlError=Failed to connect to glue.ap-northeast-1.amazonaws.com port 443: Connection timed out;

拡張されたVPCのルーティングは、RedshiftクラスタからのトラフィックをVPC経由でルーティングする機能です。これを有効化したクラスタでSpectrumを使用するには、パブリックサービスであるGlueやAthenaのカタログへのアクセス経路を別途用意する必要があります。

解決策

Enhanced VPC routingを使用しているRedshiftでSpectrumを使用する場合、ネットワークの設定方法が3通りあります。

Using Amazon Redshift Spectrum with enhanced VPC routing - Amazon Redshift

  • ①VPCにInternet Gatewayを設定する
  • ②VPCにNAT Gatewayを設定する
  • ③VPCエンドポイントを作成する
    • GlueかAthenaに対して、Interface Endpointを作成する
    • GlueやAthenaのData Catalog越しにS3上のデータへアクセスするために、S3のGateway Endpointを作成する

今回はGlueに対してVPCエンドポイントを作成して、Spectrumで外部スキーマおよび外部テーブルを作成できるようにしていきます。まずはマネジメントコンソールのVPCダッシュボードから、エンドポイントエンドポイントの作成をクリックします。

エンドポイントの作成の画面で、検索フィルタからglueと検索すると、GlueのInterface Endpointが表示されるはずです。こちらを選択してください。それ以降の設定は、対象のRedshiftが存在するVPCとサブネット、セキュリティグループを設定してください。プライベートDNS名は有効にしておきます。ちなみにIAM RoleでAthenaのデータカタログをお使いの方は、この検索フィルタでAthenaのエンドポイントを選択して設定を進めてください。

続いて、S3のGateway Endpointを作成します。先程と同様、エンドポイントの作成画面からS3のGateway Endpointを選択します。同じようにネットワークを設定し、アクセスは便宜上フルアクセスを選択します。(環境に合わせて調節ください)

2つのエンドポイントが使用可能になったら、Redshiftにもどり外部スキーマと外部テーブルを作成してみます。

create external schema s3_and_glue_vpc
from data catalog 
database 'spectrumdb'
iam_role 'arn:aws:iam::XXXXXXXXXXXX:role/s3-and-glue-for-redshift'
create external database if not exists;

create external table s3_and_glue_vpc.sales_glue_vpc(
salesid integer,
listid integer,
sellerid integer,
buyerid integer,
eventid integer,
dateid smallint,
qtysold smallint,
pricepaid decimal(8,2),
commission decimal(8,2),
saletime timestamp)
row format delimited
fields terminated by '\t'
stored as textfile
location 's3://<my-bucket>/spectrum/'
table properties ('numRows'='172000');

select * from s3_and_glue_vpc.sales_glue_vpc limit 10;

これで外部スキーマと外部テーブルの作成と、データの参照ができるはずです。できない場合は、別の原因を調査してみてください。

注意点

VPCエンドポイントの利用には料金が発生します。詳しくは以下のリンクをご確認ください。

料金 - AWS PrivateLink | AWS

参照