VPC EndpointsにAWS SDK for JavaScript in Node.jsからアクセスする

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

VPCからS3へ直接アクセスできる!

待ちにまったS3のVPCエンドポイント対応です!NATインスタンスを冗長化して悶々としていた方に朗報でしたね。さて、早速試して見ようということで、画面からVPCエンドポイントの作成をすることができましたが、コマンドラインから実行できません。まだSDKが出てませんね。そこで、既存のSDKに機能追加して動作確認してみたいと思います。といっても、やり方は簡単です。

AWS SDK for JavaScriptに機能追加する

最も機能追加しやすいのはJavaScriptです。さっそくインストールしてみましょう。

npm install aws-sdk

インストールされたNodeモジュールは以下の場所に格納されています。

$ pwd
/Users/hogehoge/node_modules/aws-sdk

そして、サービス名やAPI名などが以下に記述されています。

$ pwd
/Users/hogehoge/node_modules/aws-sdk/apis

それではAPIを追加しましょうw

$ vi ec2-2015-03-01.min.json

以下のようなJSON定義を追加してください

    "DescribeVpcEndpoints": {
      "input": {
        "type": "structure",
        "members": {
          "DryRun": {
            "locationName": "dryRun",
            "type": "boolean"
          },
          "VpcEndpointId": {
            "locationName": "VpcEndpointId",
            "type": "list",
            "member": {
              "locationName": "VpcEndpointId"
            }
          },
          "Filters": {
            "shape": "S6m",
            "locationName": "Filter"
          }
        }
      },
      "output": {
        "type": "structure",
        "members": {
          "VpcEndpointSet": {
            "locationName": "vpcEndpointSet",
            "type": "list",
            "member": {
              "locationName": "item"
            }
          }
        }
      }
    },
		
    "DescribeVpcEndpointServices": {
      "input": {
        "type": "structure",
        "members": {
          "DryRun": {
            "locationName": "dryRun",
            "type": "boolean"
          }
        }
      },
      "output": {
        "type": "structure",
        "members": {
          "ServiceNameSet": {
            "locationName": "serviceNameSet",
            "type": "list",
            "member": {
              "locationName": "item"
            }
          }
        }
      }
    },
    "DescribePrefixLists": {
      "input": {
        "type": "structure",
        "members": {
          "DryRun": {
            "locationName": "dryRun",
            "type": "boolean"
          },
          "PrefixListId": {
            "locationName": "PrefixListId",
            "type": "list",
            "member": {
              "locationName": "PrefixListId"
            }
          },
          "Filters": {
            "shape": "S6m",
            "locationName": "Filter"
          }
        }
      },
      "output": {
        "type": "structure",
        "members": {
          "PrefixListSet": {
            "locationName": "prefixListSet",
            "type": "list",
            "member": {
              "locationName": "item"
            }
          }
        }
      }
    },

出来上がりです〜w。ポイントとしては、AWSのAPIを呼び出すための仕様書が用意されています。例えば、以下のドキュメントです。

Amazon Elastic Compute Cloud API Reference (API Version 2015-03-01) - DescribeVpcEndpoints

機能追加したSDKを使う

それでは、Node.jsを書いてみましょう。

DescribeVpcEndpointServices

DescribeVpcEndpointServicesは、VPNエンドポイントに対応したサービス一覧を表示します。今のところS3のみですが、このメソッド名から考えると、S3は始まりに過ぎず、今後どんどん追加されるのではと期待してしまいますね。

var AWS = require('aws-sdk'); 
AWS.config.region = 'ap-northeast-1';
var ec2 = new AWS.EC2(); 
params = {};

ec2.describeVpcEndpointServices(params, function(err, data) {
  if (err) console.log(err, err.stack); 
  else     console.log('data : %j',data);
});

実行結果

data : {"ServiceNameSet":["com.amazonaws.ap-northeast-1.s3"]}

DescribePrefixLists

DescribePrefixListsは、VPCエンドポイントを示すプリフィックス名とIDと、IPレンジを表示してくれます。

var AWS = require('aws-sdk'); 
AWS.config.region = 'ap-northeast-1';
var ec2 = new AWS.EC2(); 
params = {};

ec2.describePrefixLists(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else     console.log('data : %j',data);
});

実行結果

data : {"PrefixListSet":[{
	"prefixListName":["com.amazonaws.ap-northeast-1.s3"],
	"prefixListId":["pl-61a540WW"],
	"cidrSet":[{"item":["54.231.224.0/21"]}]}]}

DescribeVpcEndpoints

DescribeVpcEndpointsは、どのVPCのどのルーティングテーブルに、VPCエンドポイントを設定し、どんなポリシーでアクセスできるか確認することができます。

var AWS = require('aws-sdk'); 
AWS.config.region = 'ap-northeast-1';
var ec2 = new AWS.EC2(); 
params = {};

ec2.describeVpcEndpoints(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else     console.log('data : %j',data);
});

実行結果

data : {"VpcEndpointSet":[{
	"vpcId":["vpc-5a9f67XX"],
	"state":["available"],
	"routeTableIdSet":
		[{"item":["rtb-9171b5ZZ"]}],
		"vpcEndpointId":["vpce-612dc8YY"],
		"creationTimestamp":["2015-05-12T01:25:20Z"],
		"policyDocument":[
			"{\"Version\":\"2008-10-17\",
			\"Statement\":[{
				\"Sid\":\"\",
				\"Effect\":\"Allow\",
				\"Principal\":\"*\",
				\"Action\":\"*\",
				\"Resource\":\"*\"}]}"],
		"serviceName":["com.amazonaws.ap-northeast-1.s3"]}]}

まとめ

ということで、今回は、邪道な方法ではありますがw、SDK公開前にAWS APIの操作を通じて、VPCエンドポイントについて概要を把握することができました。今日明日中には各プログラミング言語に対応した正式なSDKが出ると思いますので、基本的にはそちらを待ってからご利用ください。

参考資料

AWS SDK for JavaScript in Node.js

Amazon Virtual Private Cloud User Guide (API Version 2015-03-01) - VPC Endpoints

Amazon Elastic Compute Cloud API Reference (API Version 2015-03-01) - DescribePrefixLists