この記事は最終更新日から1年以上が経過しています。
岡崎です。
今回は、CloudFormationによるAPIGatewayのリソースとメソッドの作成について書きたいと
思います。こんな感じで書くことができます。テンプレートについては下記の通りになります。
ゴール
テンプレート部分
{ "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年に入社した新卒。ドラムとインコが好きな人。
最近は、デザイン駆動設計や関数型プログラミングに興味あり。
マネジメントも覚えていきたい系エンジニア。
最近書いた記事
- 2018.12.02シリコンバレー1日ツアーに参加してきました(後半)
- 2018.11.29【re:Invent 2018】Day3 -いよいよ本命のKeynote!-
- 2018.11.27【re:Invent 2018】Day1 - re:Inventに参加してきました!!!
- 2018.02.21CloudFormationでAPIGatewayのリソースとメソッドを作成する方法