{"id":1857,"date":"2016-04-15T23:58:39","date_gmt":"2016-04-15T14:58:39","guid":{"rendered":"https:\/\/ecpplus.net\/weblog\/?p=1857"},"modified":"2020-06-15T22:51:37","modified_gmt":"2020-06-15T13:51:37","slug":"aws-api-gateway-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%82%b5%e3%83%bc%e3%83%90%e3%83%ac%e3%82%b9%e3%81%aa-ifconfig-api-%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/aws-api-gateway-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%82%b5%e3%83%bc%e3%83%90%e3%83%ac%e3%82%b9%e3%81%aa-ifconfig-api-%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b\/","title":{"rendered":"AWS API Gateway \u3092\u4f7f\u3063\u3066\u30b5\u30fc\u30d0\u30ec\u30b9\u306a ifconfig API \u3092\u4f5c\u3063\u3066\u307f\u308b"},"content":{"rendered":"<p>\u30b5\u30fc\u30d0\u304c\u81ea\u5206\u81ea\u8eab\u306e\u30b0\u30ed\u30fc\u30d0\u30ebIP\u30a2\u30c9\u30ec\u30b9\u3092\u8abf\u3079\u308b\u3068\u304d\u9762\u5012\u306a\u3053\u3068\u304c\u3088\u304f\u3042\u308b\u306e\u3067\u3001API Gateway \u3092\u4f7f\u3063\u3066 ifconfig \u306e\u3088\u3046\u306a\u3053\u3068\u3092\u3059\u308b\u3002\u5358\u7d14\u306b\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u3066\u304d\u305fIP\u30a2\u30c9\u30ec\u30b9\u3092\u8fd4\u3059\u3060\u3051\u3002(\u3053\u3046\u3044\u3046\u30b5\u30fc\u30d3\u30b9\u306f\u4ed6\u306b\u3082\u3042\u308b\u3051\u3069\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u9045\u3081\u3060\u3063\u305f\u308a\u3001\u4fe1\u7528\u3067\u304d\u308b\u304b\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u308a\u3059\u308b\u306e\u3067\u3001\u81ea\u5206\u3067\u4f5c\u308b\u306e\u304c\u5b89\u5168\u305d\u3046)<\/p>\n<pre><code class=\"language-bash line-numbers\">curl https:\/\/ip.ecp.plus\n<\/code><\/pre>\n<p>\u3068\u3057\u305f\u3089\u81ea\u5206\u306eIP\u30a2\u30c9\u30ec\u30b9\u304c\u8fd4\u308b\u3068\u3044\u3046\u306e\u304c\u30b4\u30fc\u30eb\u3002\u30d6\u30e9\u30a6\u30b6\u3067\u898b\u3066\u3082\u540c\u3058\u3002\u3061\u306a\u307f\u306b\u2191\u306f\u3082\u3046\u52d5\u3044\u3066\u3044\u308b\u306e\u3067\u3054\u81ea\u7531\u306b\u304a\u4f7f\u3044\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u6700\u521d\u3001API Gateway -> Lambda \u3068\u3057\u3066\u3001Lambda \u306b IP\u30a2\u30c9\u30ec\u30b9\u3092\u6e21\u3057\u3066\u3001Lambda \u306f\u305d\u308c\u3092\u8fd4\u3057\u3066\u3001API Gateway \u306f\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u305d\u306e\u307e\u307e\u30b9\u30eb\u30fc\u3059\u308b\u3002\u3068\u3044\u3046\u306e\u3092\u3084\u3063\u305f\u304c\u3001\u5b9f\u306f Lambda \u306a\u3057\u3067\u51fa\u6765\u305f\u3088\u3046\u3060\u3002<\/p>\n<p>\u306f\u3058\u3081 Lambda \u3092\u4f7f\u3063\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u3084\u3063\u305f\u306e\u3067\u3001\u4e21\u65b9\u66f8\u304f\u3002<\/p>\n<h2>API Gateway \u306e\u8a2d\u5b9a<\/h2>\n<h3>Create Method<\/h3>\n<p>URL\u306f\u77ed\u304f\u3057\u305f\u3044\u306e\u3067\u3001\u30c9\u30e1\u30a4\u30f3\u76f4\u4e0b\u3092GET\u3057\u305f\u3068\u304d\u306b\u8fd4\u3057\u305f\u3044\u3002 <code>\/<\/code> \u306b\u5bfe\u3057\u3066 Create Method \u3067 GET \u3092\u6307\u5b9a\u3059\u308b\u3002<\/p>\n<p>Method Request \u306f\u8a8d\u8a3c\u306a\u3069\u3057\u306a\u3044\u306e\u3067\u305d\u306e\u307e\u307e\u3002<\/p>\n<h3>\/ &#8211; GET &#8211; Method Execution<\/h3>\n<p>API Gateway \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u304d\u305f IP \u30a2\u30c9\u30ec\u30b9\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u3066\u53d6\u308c\u308b\u3002<\/p>\n<pre><code class=\"language-json line-numbers\">{\n  \"ipaddress\" : \"$context.identity.sourceIp\"\n}\n<\/code><\/pre>\n<p>\u306e\u3060\u304c\u3001API Gateway \u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u5272\u308a\u632f\u3089\u308c\u308b URL \u306f\u975e\u5e38\u306b\u9577\u304f\u3001\u81ea\u5206\u306e\u30c9\u30e1\u30a4\u30f3\u3067\u4f7f\u3044\u305f\u3044\u3002Custom Domain Names \u3092\u4f7f\u3046\u305f\u3081\u306b\u306f\u3001TLS \u306e\u8a3c\u660e\u66f8\u304c\u5fc5\u8981\u306a\u306e\u3060\u304c\u3001\u304a\u624b\u8efd\u3055\u304c\u7121\u3044\u306e\u3067\u3084\u3081\u305f\u3002\u65e2\u306b\u52d5\u3044\u3066\u3044\u308b Caddy \u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u8ee2\u9001\u3059\u308b\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n<p>\u305f\u3060\u3001\u305d\u3046\u3059\u308b\u3068\u30ea\u30af\u30a8\u30b9\u30c8\u5143\u306eIP\u30a2\u30c9\u30ec\u30b9\u304c Caddy \u304c\u52d5\u3044\u3066\u3044\u308b\u30b5\u30fc\u30d0\u306e IP\u30a2\u30c9\u30ec\u30b9\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u30d8\u30c3\u30c0\u30fc\u306e <code>X-Forwarded-For<\/code> \u3092\u898b\u308b\u3053\u3068\u306b\u3059\u308b\u3002\u3053\u3053\u306e\u5148\u982d\u306b\u3001\u30aa\u30ea\u30b8\u30ca\u30eb\u306eIP\u30a2\u30c9\u30ec\u30b9\u304c\u5165\u3063\u3066\u308b\u3002<\/p>\n<pre><code class=\"language-json line-numbers\">{\n  \"ipaddress\" : \"$input.params().header.get('X-Forwarded-For')\"\n}\n<\/code><\/pre>\n<h3>Lambda<\/h3>\n<p>ipaddress \u3068\u3057\u3066\u6e21\u3063\u3066\u304d\u305f\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089\u3001\u4e00\u756a\u6700\u521d\u306eIP\u30a2\u30c9\u30ec\u30b9\u3092\u8fd4\u3059\u3060\u3051\u306e\u51e6\u7406\u3092\u66f8\u304f\u3002<\/p>\n<pre><code class=\"language-nodejs line-numbers\">exports.handler = function(event, context) {\ncontext.succeed([event.ipaddress.split(\/\\s*,\\s*\/)[0], \"\\n\"].join(''));\n};\n<\/code><\/pre>\n<h3>\/ &#8211; GET &#8211; Integration Response<\/h3>\n<p>\u518d\u3073 API Gateway \u306b\u623b\u3063\u3066\u304d\u3066\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u52a0\u5de5\u3092\u3059\u308b\u3002\u4e00\u898b\u3001Output passthrough \u3067\u826f\u3055\u305d\u3046\u3060\u304c\u3001\u3053\u308c\u3060\u3068\u30c0\u30d6\u30eb\u30af\u30aa\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u307e\u308c\u305f\u3082\u306e\u304c\u8fd4\u3063\u3066\u3057\u307e\u3046\u3002<\/p>\n<p><code>Content-Type: application\/json<\/code> \u3067 Mapping Template \u3068\u3057\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<pre><code class=\"line-numbers\">$input.path('$')\n<\/code><\/pre>\n<p>\u3053\u308c\u3067AWS\u5074\u306e\u5b9f\u88c5\u306f\u5b8c\u4e86\u3002<\/p>\n<h2>Caddy \u306e\u8a2d\u5b9a<\/h2>\n<p>ip.ecp.plus \u306b\u6765\u305f\u3082\u306e\u3092 API Gateway \u306b\u98db\u3070\u3059\u3002API Gateway \u5074\u3067 <code>GET \/<\/code> \u3092\u53e9\u304f\u305f\u3081\u306b\u3001\u8ee2\u9001\u5148\u306e\u672b\u5c3e\u306b\u30b9\u30e9\u30c3\u30b7\u30e5\u3092\u5fd8\u308c\u305a\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<pre><code class=\"language-caddy line-numbers\">ip.ecp.plus {\n  proxy \/ https:\/\/EXAMPLE.execute-api.ap-northeast-1.amazonaws.com\/prod\/\n}\n<\/code><\/pre>\n<p>\u3068\u308a\u3042\u3048\u305a\u3053\u3053\u307e\u3067\u3067\u5b8c\u6210\u3002<\/p>\n<p>\u3060\u304c\u3001Lambda \u304c\u3069\u3046\u898b\u3066\u3082\u3042\u307e\u308a\u610f\u5473\u304c\u306a\u3055\u305d\u3046\u3060\u3063\u305f\u306e\u3067\u3001API Gateway \u3060\u3051\u3067\u51fa\u6765\u306a\u3044\u304b\u898b\u3066\u3044\u305f\u3068\u3053\u308d\u3001\u958b\u767a\u4e2d\u306b\u4f7f\u3046\u7528\u9014\u3068\u601d\u3063\u3066\u3044\u305f Mock Integration \u3092\u4f7f\u3048\u3070\u51fa\u6765\u305f\u3002<\/p>\n<h2>API Gateway \u306e\u8a2d\u5b9a(\u518d)<\/h2>\n<h3>\/ &#8211; GET &#8211; Method Execution<\/h3>\n<p>Integration type \u3068\u3057\u3066 Mock Integration \u3092\u9078\u629e\u3002Mapping template \u3068\u3057\u3066<\/p>\n<pre><code class=\"language-json line-numbers\">{\n  \"statusCode\" : 200\n}\n<\/code><\/pre>\n<p>\u306e\u3088\u3046\u306a\u3082\u306e\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u308b\u304c\u3001\u3053\u308c\u304c\u306a\u3044\u3068\u52dd\u624b\u306b\u30a8\u30e9\u30fc\u306b\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u305d\u306e\u307e\u307e\u306b\u3057\u3066\u304a\u304f\u3002<\/p>\n<h3>\/ &#8211; GET &#8211; Integration Response<\/h3>\n<p>\u3069\u3046\u3084\u3089\u3001Response \u5074\u3067\u3082\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3067\u6e21\u3063\u3066\u304d\u305f\u5024\u304c\u305d\u306e\u307e\u307e\u53d6\u308c\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u3053\u3061\u3089\u306bIP\u30a2\u30c9\u30ec\u30b9\u3092\u8fd4\u3059\u51e6\u7406\u3092\u66f8\u3051\u3070\u826f\u3044\u3002<\/p>\n<pre><code class=\"line-numbers\">#set($ipAddresses = $input.params().header.get('X-Forwarded-For').split(','))\n$ipAddresses[0]\n#set($dummy = \"dummy\")\n<\/code><\/pre>\n<p>\u6700\u5f8c\u306e\u884c\u3067\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u3053\u3068\u3092\u3057\u3066\u3044\u308b\u304c\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u672b\u5c3e\u306b\u6539\u884c\u3092\u5165\u308c\u305f\u3044\u305f\u3081\u3053\u3046\u3057\u3066\u3044\u308b\u3002&#8221;\\n&#8221; \u3092\u3069\u3046\u306b\u304b\u5165\u308c\u305f\u304b\u3063\u305f\u304c\u3001\u3046\u307e\u304f\u884c\u304b\u305a\u30d0\u30c3\u30c9\u30ce\u30a6\u30cf\u30a6\u7684\u306a\u89e3\u6c7a\u65b9\u6cd5\u306b\u306a\u3063\u3066\u3044\u308b\u2026\u3002\u672c\u5f53\u306f\u826f\u3044\u65b9\u6cd5\u304c\u3042\u308b\u306f\u305a\u3060\u3002<\/p>\n<p>\u3053\u3053\u307e\u3067\u3067\u5b8c\u6210\u3002<\/p>\n<p>\u5c24\u3082\u3001\u3053\u3093\u306a\u5358\u7d14\u306a\u3082\u306e\u306a\u3089 Caddy \u52d5\u304b\u3057\u3066\u308b\u30b5\u30fc\u30d0\u3067\u51e6\u7406\u3057\u308d\u3088\u3068\u3044\u3046\u611f\u3058\u3060\u3051\u308c\u3069\u3001\u6700\u8fd1 API Gateway \u306b\u30cf\u30de\u3063\u3066\u3044\u308b\u306e\u3067\u4f5c\u3063\u3066\u307f\u305f\u3002API Gateway \u5358\u4f53\u3067\u52d5\u304b\u306a\u3044\u3068\u3001Caddy \u306e\u30b5\u30fc\u30d0\u304c\u843d\u3061\u305f\u3068\u304d\u306b\u4f7f\u3048\u306a\u304f\u3066\u6fc0\u3057\u304f\u610f\u5473\u304c\u306a\u3044\u304c\u3001Custom Domain Names \u306e\u8a2d\u5b9a\u307e\u3067\u3084\u308b\u306e\u306f\u9762\u5012\u3067\u3042\u3063\u305f\u3002Route53 \u4f7f\u3063\u3066\u305f\u3089\u30b7\u30fc\u30e0\u30ec\u30b9\u306b\u9023\u643a\u51fa\u6765\u305f\u308a\u3059\u308c\u3070\u826f\u3044\u306e\u306b\u3001\u3068\u601d\u3063\u305f\u3002<\/p>\n<h2>(\u8ffd\u8a18) CloudFront \u3092\u4f7f\u3063\u3066\u30b5\u30fc\u30d0\u30ec\u30b9\u306b\u51fa\u6765\u305f<\/h2>\n<p>\u305d\u306e\u5f8c\u3001Caddy \u306e\u4ee3\u308f\u308a\u306b CloudFront \u3092\u4f7f\u3048\u3070\u3001TLS\u8a3c\u660e\u66f8\u3082\u7121\u6599\u3067\u3001\u597d\u304d\u306a\u30c9\u30e1\u30a4\u30f3\u3067\u51fa\u6765\u308b\u3053\u3068\u304c\u5206\u304b\u3063\u305f\u3002<\/p>\n<p>CloudFront \u306e\u753b\u9762\u304b\u3089\u3001Create Distribution -> Web \u306e Get Started \u3067\u3001\u4f5c\u6210\u753b\u9762\u3078\u5165\u308b\u3002<\/p>\n<p><code>Origin Domain Name<\/code> \u306f\u3001API Gateway \u306e Invoke URL \u3092\u6307\u5b9a\u3059\u308b\u3002https:\/\/ \u3092\u4ed8\u3051\u3066\u3082\u52dd\u624b\u306b\u5916\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3001URL\u30b3\u30d4\u30da\u3067\u826f\u3044\u3002stage\u540d\u306f\u6d88\u3059\u3002\u3064\u307e\u308a\u30c9\u30e1\u30a4\u30f3\u90e8\u5206\u3060\u3051\u3092\u8a18\u8ff0\u3059\u308b\u3002<br \/>\n<code>Origin Path<\/code> \u306f\u3001stage\u540d\u3092\u5165\u308c\u308b\u3002 prod \u306a\u3089 <code>\/prod<\/code> \u3068\u3059\u308b\u3002<\/p>\n<p>\u3042\u3068\u306f\u9805\u76ee\u3092\u8aad\u307f\u306a\u304c\u3089\u8a2d\u5b9a\u3059\u308c\u3070\u826f\u3044\u3002CloudFront \u306f\u72ec\u81ea\u30c9\u30e1\u30a4\u30f3\u306eTLS\u8a3c\u660e\u66f8\u304c\u7121\u6599\u306a\u306e\u3067\u3001\u3068\u3066\u3082\u3042\u308a\u304c\u305f\u3044\u3002\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306f\u3001 CloudFront \u306b\u30ad\u30e3\u30c3\u30b7\u30e5\u3055\u308c\u308b\u3068\u524d\u306b\u5b9f\u884c\u3057\u305f\u4eba\u306eIP\u30a2\u30c9\u30ec\u30b9\u304c\u8fd4\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3055\u305b\u306a\u3044\u3088\u3046\u306b\u3057\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3002<br \/>\n<code>Behaviors<\/code> \u306e\u30bf\u30d6\u3067\u3001\u521d\u671f\u304b\u3089\u5b58\u5728\u3057\u3066\u3044\u308b\u3082\u306e\u3092\u7de8\u96c6\u3057\u3066\u3001<code>Object Caching : Customize<\/code>\u3068\u3057\u3066\u3001<code>Minimum TTL : 0<\/code>, <code>Maximum TTL : 0<\/code>, <code>Default TTL : 0<\/code> \u3068\u3059\u308c\u3070\u826f\u3044\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\u81ea\u5df1\u7ba1\u7406\u3059\u308b\u30a4\u30f3\u30d5\u30e9\u7121\u3057\u3067\u3001\u5f53\u521d\u306e\u3084\u308a\u305f\u304b\u3063\u305f\u3053\u3068\u304c\u51fa\u6765\u305f\u3002<\/p>\n<h2>API Gateway \u304b\u3089 Export \u3057\u305f Swagger \u7528\u306e YAML<\/h2>\n<p><a href=\"https:\/\/gist.github.com\/ecpplus\/1373c676f602326955f8\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gist.github.com\/ecpplus\/1373c676f602326955f8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d0\u304c\u81ea\u5206\u81ea\u8eab\u306e\u30b0\u30ed\u30fc\u30d0\u30ebIP\u30a2\u30c9\u30ec\u30b9\u3092\u8abf\u3079\u308b\u3068\u304d\u9762\u5012\u306a\u3053\u3068\u304c\u3088\u304f\u3042\u308b\u306e\u3067\u3001API Gateway \u3092\u4f7f\u3063\u3066 ifconfig \u306e\u3088\u3046\u306a\u3053\u3068\u3092\u3059\u308b\u3002\u5358\u7d14\u306b\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u3066\u304d\u305fIP\u30a2\u30c9\u30ec\u30b9\u3092\u8fd4\u3059\u3060\u3051\u3002(\u3053\u3046\u3044\u3046\u30b5\u30fc\u30d3\u30b9\u306f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2699,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[33],"class_list":["post-1857","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-aws"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=1857"}],"version-history":[{"count":5,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"predecessor-version":[{"id":2714,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1857\/revisions\/2714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2699"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}