目次

目次

CloudFormationでAPIGatewayのリソースとメソッドを作成する方法

アバター画像
岡崎拓哉
アバター画像
岡崎拓哉
最終更新日2018/02/21 投稿日2018/02/21

岡崎です。

今回は、CloudFormationによるAPIGatewayのリソースとメソッドの作成について書きたいと 思います。こんな感じで書くことができます。テンプレートについては下記の通りになります。

ゴール

APIGateway.png

テンプレート部分

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Resources" : {
    "MediaPlayerRestApi" : {
      "Type" : "AWS::ApiGateway::RestApi",
      "Properties" : {
        "Description" : "MediaPlayer専用API",
        "Name" : "MediaPlayer"
      }
    },
    "MeResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Fn::GetAtt": ["MediaPlayerRestApi", "RootResourceId"] },
        "PathPart": "me"
      }
    },
    "MeGetMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "MeResource" },
        "HttpMethod" : "GET",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "UsersResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Fn::GetAtt": ["MediaPlayerRestApi", "RootResourceId"] },
        "PathPart": "users"
      }
    },
    "UsersAuthenticationResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Ref": "UsersResource" },
        "PathPart": "authentication"
      }
    },
    "UsersExtendedIdsResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Ref": "UsersResource" },
        "PathPart": "extendIds"
      }
    },
    "UsersPostMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "UsersResource" },
        "HttpMethod" : "POST",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      },
    },
    "UsersAuthenticationPostMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "UsersAuthenticationResource" },
        "HttpMethod" : "POST",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "UsersExtendIdsPostMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "UsersExtendedIdsResource" },
        "HttpMethod" : "POST",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "PushResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Fn::GetAtt": ["MediaPlayerRestApi", "RootResourceId"] },
        "PathPart": "push"
      }
    },
    "PushResourceVarMessage" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Ref": "PushResource" },
        "PathPart": "{message_id}"
      }
    },
    "PushPostMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "PushResource" },
        "HttpMethod" : "POST",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "PushDeleteMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "PushResource" },
        "HttpMethod" : "DELETE",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "PushPutMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "PushResourceVarMessage" },
        "HttpMethod" : "POST",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    },
    "VersionResource" : {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId" : { "Ref": "MediaPlayerRestApi" },
        "ParentId" : { "Fn::GetAtt": ["MediaPlayerRestApi", "RootResourceId"] },
        "PathPart": "version"
      }
    },
    "VersionGetMethod" : {
      "Type" : "AWS::ApiGateway::Method",
      "Properties" : {
        "RestApiId" : { "Ref" : "MediaPlayerRestApi" },
        "ResourceId" : { "Ref" : "VersionResource" },
        "HttpMethod" : "GET",
        "AuthorizationType" : "NONE",
        "Integration" : {
          "Type" : "MOCK",
          "RequestTemplates" : {
            "application/json": "{ \"statusCode\" : 200 }"
          },
          "IntegrationResponses" : [
            { "StatusCode" : "200" }
          ]
        },
        "MethodResponses" : [
          { "StatusCode" : "200" }
        ]
      }
    }
  }
}
アバター画像

岡崎拓哉

2016年に入社した新卒。ドラムとインコが好きな人。
最近は、デザイン駆動設計や関数型プログラミングに興味あり。
マネジメントも覚えていきたい系エンジニア。

目次