amazon api gateway - 開発者ガイド · amazon api gateway 開発者ガイド swagger...

632
Amazon API Gateway 開発者ガイド

Upload: vuonglien

Post on 08-Aug-2018

546 views

Category:

Documents


49 download

TRANSCRIPT

  • Amazon API Gateway

  • Amazon API Gateway

    Amazon API Gateway: Copyright 2018 Amazon Web Services, Inc. and/or its affiliates. All rights reserved.

    Amazon's trademarks and trade dress may not be used in connection with any product or service that is not Amazon's, in any mannerthat is likely to cause confusion among customers, or in any manner that disparages or discredits Amazon. All other trademarks notowned by Amazon are the property of their respective owners, who may or may not be affiliated with, connected to, or sponsored byAmazon.

  • Amazon API Gateway

    Table of ContentsAmazon API Gateway ? ................................................................................................................ 1

    AWS ................................................................................................... 1AWS ..................................................................... 2

    .............................................................................................................. 2API Gateway API ............................................................................................ 2API Gateway API ............................................................................................... 3

    API Gateway ............................................................................................................. 3API Gateway ................................................................................................................... 3API Gateway ................................................................................................................... 6

    ......................................................................................................................... 7API GatewayAPI ...................................................................................... 7

    AWS ............................................................................... 7IAM AWS ............................ 8IAM API Gateway API .............. 8 .................................................................................................................... 10

    API .............................................................................................................. 10 API .................................................................. 11 .................................................................................................................... 18 ........................................................................................ 18

    Lambda API .................................................................................................. 19Lambda API ........................................................................... 19 Lambda API ............................................... 28 Lambda API ....................................................................... 29

    HTTP API ..................................................................................................... 39HTTP API .............................................................................. 39HTTP API .............................................................................. 45

    API ......................................................................................... 74AWS API ................................................................................................... 76

    .......................................................................................................................... 76 1: ......................................................................................... 77 2: GET .................................................................................. 77 3: AWS ................................................... 77 4: .................................................. 79 5: API ......................................................................................... 79 6: API ............................................................................................ 80 7: ............................................................................................... 80

    API ....................................................................................................................................... 81API ..................................................................... 81API ......................................................................................................... 82

    API Gateway API .................................................. 82AWS CLI API ............................... 83Node.js AWS SDK API ......................... 88API Gateway REST API API ................................ 94Swagger API .................................... 102 API .............................................................................................. 103 API ........................................................................................... 106

    API ..................................................................................................... 113 ................................................................................................ 114 ................................................................................... 120 .......................................................................... 122

    API ................................................................................................................. 126 ...................................................................................................... 126 ................................................................................................ 133Lambda ............................................................................................. 134

    iii

  • Amazon API Gateway

    HTTP ................................................................................................ 150 ................................................................................................... 154 ......................................................................................... 160

    ..................................................................................... 163API Gateway ............................................................................ 164 ...................................................................................... 164API Gateway ...................... 168API Gateway REST API ...................... 169Swagger .............................................. 170

    ..................................................................................................... 170 ................... 171 ................................................................. 173 .................................................. 200 ................................................................................ 205

    ........................................................................................... 214API Gateway ............................................................................ 215API Gateway .............................................. 217API Gateway REST API .............................................. 221 .............................................................. 225 ...................................................................................................... 225

    ................................................................................................. 232API ................................................................................................... 233 ....................................................................... 235 .................................................................... 236

    ........................................................................................................ 237API Gateway ....................................................... 237API Gateway .......................................................... 238API Gateway ....................................................... 242 API Swagger .................................................. 245

    API .................................................................................................................... 248API () ................................... 250 API ........................................................................................... 252

    API .............................................................................................................. 254 API .................................................................................. 255 API ..................................................................................... 256Swagger API ............................................. 257: Swagger basePath ................................................................................ 258 ............................................................................................. 260

    API ...................................................................................................... 261API Gateway ....................................................................................... 261

    Amazon API Gateway .......................................................... 262API Gateway .................................................................................. 263API Gateway API ............................................. 265API Gateway IAM ............................................. 268API Gateway AWS .............................................. 269

    IAM .......................................................................................................... 270API API Gateway ...................................... 270API API Gateway ..................................................... 271API ............................................................................... 272API .................................................................... 275API ............................................................................... 277API Gateway API IAM ......................................................... 279API IAM ......................................................................... 283 IAM ............................................................... 284

    CORS ................................................................................................ 286 ........................................................................................................................ 287 CORS ........................................................................ 287

    iv

  • Amazon API Gateway

    Swagger CORS .................................................................... 289Lambda ................................................................................................. 290

    API Gateway Lambda ................................................................... 291Lambda Lambda ............................................................. 292Lambda ...................................................................................... 296Amazon API Gateway Lambda .................................................... 297Lambda ................................................................................... 299Lambda API ......................................................................... 301 Lambda ....................................................... 304

    Amazon Cognito ...................................................................................... 305 ..................................................... 306Amazon Cognito ........................................................................ 307API ............................................................................................. 308 API ...................................................................... 312

    SSL ................................................................................... 313API Gateway ........................................ 313API SSL ..................................................................... 314 ........................................................... 315 HTTPS ...................................... 317 ................................................................................... 317HTTP HTTP ........................................ 318

    API ..................................................................................... 334 API ..................................................................................... 335 ...................................................................................... 335API ............................................................................................. 335API Gateway API .......................................... 337API Gateway ......................... 340API Gateway REST API API .......................................... 345API Gateway CLI REST API 345API Gateway API ............................................................................... 350

    API ...................................................................................................................... 351API Gateway API ................................................................................ 351

    ......................................................................................................... 351 ................................................................................................... 360

    API Gateway API .......................................................... 360API ................................................................................... 361RESOURCE .......................................................................... 363METHOD ............................................................................. 363QUERY_PARAMETER ............................................................. 364PATH_PARAMETER ............................................................... 365REQUEST_HEADER .............................................................. 365REQUEST_BODY .................................................................. 366RESPONSE .......................................................................... 366RESPONSE_HEADER ............................................................. 367RESPONSE_BODY ................................................................. 367MODEL ............................................................................... 367AUTHORIZER ...................................................................... 368

    API Gateway REST API API .......................................................... 369API ................................................................................... 369RESOURCE .......................................................................... 370METHOD ............................................................................. 373QUERY_PARAMETER ............................................................. 375PATH_PARAMETER ............................................................... 376REQUEST_BODY .................................................................. 377REQUEST_HEADER ............................................................... 378RESPONSE .......................................................................... 379RESPONSE_HEADER ............................................................. 380

    v

  • Amazon API Gateway

    AUTHORIZER ...................................................................... 380MODEL ............................................................................... 381 ................................................................................................ 383 ............................................................................................. 384

    API ........................................................................................................... 384 API ........................................ 384 ................................................................................ 385 ................................................................................ 385 ................................................................................ 385API .................................................. 386 ............................... 387

    API .................................................................................................. 389API Gateway REST API ..................................... 390API Gateway ..................................... 392

    API ........................................................................................ 392API ............................................................................................................................... 393

    ....................................................................................................................... 394AWS CLI .................................................................................. 394 API .................................................................................. 395

    ....................................................................................................................... 396API Gateway ........................................... 396API ...................................................................................................... 400API ............................................................................................. 402API ............................................................................................. 408 ......................................................................................................... 410API .......................................................................................... 420

    Canary .......................................................................... 422API Gateway Canary ................................................................... 423Canary .............................................................................. 424Canary .................................................................................................... 431Canary .................................................................................................... 433Canary ........................................................................................... 437

    API ........................................................................................................... 439API ................................................................................... 439JSON API Swagger .................................................................. 440YAML API Swagger .................................................................. 440JSON Postman API Swagger ............................... 440API Swagger YAML API Gateway ....... 441API Gateway API ...................................................... 441

    API SDK ........................................................................................................... 441API Gateway API SDK ............................................... 442AWS CLI API SDK ......................................................... 445 Lambda .............................................................................................. 446API Gateway API ...................................................................................... 447 API Swagger ....................................................................................... 452

    API ........................................................................................... 457AWS Certificate Manager .................................................................. 459 .............................................................. 461 ................................................................. 467 ......................................................................................... 472

    SaaS API ....................................................................................................... 478API Gateway AWS Marketplace ........................................................... 479 ................................................................. 480

    API ............................................................................................................................... 482API Gateway API URL ...................................................... 482 ............................................................................... 483

    ........................................................................................................................ 483

    vi

  • Amazon API Gateway

    API Gateway ................................................................ 483Postman API ......................................................................................... 484 SDK API ......................................................................... 484

    API Gateway Java SDK ................................................................ 485API Gateway Android SDK ............................................................ 488API Gateway JavaScript SDK ........................................................... 490API Gateway Ruby SDK ............................................................... 491Objective-C Swift API Gateway iOS SDK ........................... 494

    AWS Amplify JavaScript API ..................................................... 503API .............................................................................................. 503

    CloudTrail API ...................................................................... 504Amazon CloudWatch API execution ................................................... 505

    Swagger .............................................................................................................................. 512x-amazon-apigateway-any-method ..................................................................................... 512

    x-amazon-apigateway-any-method .............................................................................. 513x-amazon-apigateway-api-key-source .............................................................................. 513

    x-amazon-apigateway-api-key-source .......................................................................... 513x-amazon-apigateway-authorizer ..................................................................................... 514

    x-amazon-apigateway-authorizer ................................................................................ 515x-amazon-apigateway-authtype ......................................................................................... 517

    x-amazon-apigateway-authtype .................................................................................. 517 ...................................................................................... 517

    x-amazon-apigateway-binary-media-type ..................................................................................... 518x-amazon-apigateway-binary-media-types .................................................................... 518

    x-amazon-apigateway-documentation ......................................................................................... 518x-amazon-apigateway-documentation .......................................................................... 518

    x-amazon-apigateway-gateway-responses ................................................................................... 519x-amazon-apigateway-gateway-responses .................................................................... 519

    x-amazon-apigateway-gateway-responses.gatewayResponse ......................................................... 519x-amazon-apigateway-gateway-responses.gatewayResponse .......................................... 520

    x-amazon-apigateway-gateway-responses.responseParameters ..................................................... 520x-amazon-apigateway-gateway-responses.repsonseParameters ...................................... 521

    x-amazon-apigateway-gateway-responses.responseTemplates ....................................................... 521x-amazon-apigateway-gateway-responses.responseTemplates ........................................ 521

    x-amazon-apigateway-integration ................................................................................... 522x-amazon-apigateway-integration ................................................................................ 524

    x-amazon-apigateway-integration.requestTemplates ...................................................................... 525x-amazon-apigateway-integration.requestTemplates ....................................................... 526

    x-amazon-apigateway-integration.requestParameters .................................................................... 526x-amazon-apigateway-integration.requestParameters ...................................... 526

    x-amazon-apigateway-integration.responses ................................................................................ 527x-amazon-apigateway-integration.responses ..................................................... 527

    x-amazon-apigateway-integration.response .................................................................................. 528x-amazon-apigateway-integration.response ....................................................... 529

    x-amazon-apigateway-integration.responseTemplates ................................................................... 529x-amazon-apigateway-integration.responseTemplate ...................................................... 530

    x-amazon-apigateway-integration.responseParameters .................................................................. 530x-amazon-apigateway-integration.responseParameters ..................................... 530

    x-amazon-apigateway-request-validator ....................................................................................... 530x-amazon-apigateway-request-validator ............................................................ 531

    x-amazon-apigateway-request-validators ..................................................................................... 531x-amazon-apigateway-request-validators .......................................................... 532

    x-amazon-apigateway-request-validators.requestValidator .............................................................. 532x-amazon-apigateway-request-validators.requestValidator ........................... 532

    ......................................................................................................... 534Lambda API ................................................................................................ 534

    API Lambda IAM .............................. 536

    vii

  • Amazon API Gateway

    Lambda ............................................................................ 536Lambda API ............................................................................ 538Lambda GET ......................... 538Lambda JSON POST ....................... 542Lambda GET ......................... 544Lambda API Swagger .................................................................... 549

    Amazon S3 API ............................................................................... 553API Amazon S3 IAM ......................... 554Amazon S3 API ............................................ 555 Amazon S3 API ............................ 556API Amazon S3 ........................................................... 562API Amazon S3 ....................... 565REST API API ............................................................. 568Amazon S3 API Swagger ................................................ 570

    Amazon Kinesis API ......................................................................... 579API Kinesis IAM .................................. 580Kinesis API .................................................................... 582Kinesis .............................................................................. 582Kinesis ..................................................................... 587Kinesis ............................................................ 592Kinesis API Swagger ...................................................... 603

    API Gateway REST API ................................................................................................................... 610 ............................................................................................................................ 611

    API Gateway ................................................................................................................ 611API API Gateway .................................................................. 611API API Gateway .......................................... 613

    ............................................................................................................................. 614 ............................................................................................................................ 617

    ............................................................................................................................. 618AWS ................................................................................................................................ 624

    viii

  • Amazon API Gateway AWS

    Amazon API Gateway ?Amazon API Gateway API AWS AWS AWS API

    AWS (p. 1) (p. 2) API Gateway (p. 3) Amazon API Gateway (p. 3) API Gateway (p. 6)

    AWS API Gateway AWS RESTful (API) AWS

    API Gateway

    API Gateway RESTful APIAWS

    1

    https://aws.amazon.com/what-is-cloud-computing/https://aws.amazon.com/https://en.wikipedia.org/wiki/Backplanehttp://docs.aws.amazon.com/apigateway/api-reference/resource/rest-api/

  • Amazon API Gateway AWS

    Amazon Simple Storage Service Amazon DynamoDB APIGateway API AWS Lambda IRS

    () API Gateway 1 API AWS API AWS API API Gateway

    Amazon API Gateway API AWS

    AWS API Gateway AWS Lambda AWS AWS Lambda API Gateway API Lambda AWS Lambda API Gateway AWS Lambda HTTP (p. 130)

    API Gateway API

    API Gateway API AWS

    API API Gateway API API API AWS IAM

    AWS API IAM Amazon Cognito ID ID ID AmazonAmazon Cognito FacebookGoogle

    API Gateway API API API API Gateway (apigateway) API API

    /incomes HTTP (GETPOSTPUTPATCHDELETE ) 1 API POST /incomes GET /expenses

    API REST API API

    2

    http://docs.aws.amazon.com/AmazonS3/latest/dev/welcome.htmlhttp://docs.aws.amazon.com/amazondynamodb/latest/developerguide/welcome.htmlhttp://docs.aws.amazon.com/lambda/latest/dg/welcome.htmlhttp://docs.aws.amazon.com/apigateway/api-reference/resource/method/http://docs.aws.amazon.com/apigateway/api-reference/resource/method-response/http://docs.aws.amazon.com/apigateway/api-reference/resource/integration/http://docs.aws.amazon.com/apigateway/api-reference/resource/integration-response/http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html

  • Amazon API Gateway API Gateway API

    DynamoDB API DynamoDB IAM API Gateway ( HTTP ) API Gateway

    API API Amazon API Gateway (p. 7)API Gateway API Gateway REST API (p. 610)API AWS (CLI) AWS SDK REST API (: Postman)API AWS CloudFormation Swagger API Gateway (p. 512) API APIGateway

    API Gateway API API execute-api API Gateway API Gateway API API API API Gateway API REST API (:CURLPostman API Gateway SDK) API API

    apigateway execute-api API Gateway IAM API

    API Gateway API Gateway API Gateway AWSLambda Amazon EC2 API AWS API Gateway API (SDK)

    API Gateway API API API Gateway Lambda

    Amazon API Gateway API Gateway

    API Gateway AWS

    3

    https://en.wikipedia.org/wiki/Payload_(computing)http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-reference.htmlhttp://docs.aws.amazon.com/general/latest/gr/rande.html#apigateway_region

  • Amazon API Gateway API Gateway

    1. HTTP AWS Lambda AWS RESTful (API)

    2. HTTP API API Gateway API

    HTTP Lambda AWS 1 API HTTP IAM API Gateway Lambda () API API API APIGateway HTTP 1 API

    API

    API Gateway API 1 API

    API

    API Gateway AWS ()

    API

    API Gateway API{rest-api-id}.execute-api.{region}.amazonaws.com API API : AWS

    CloudFront API Gateway API API CloudFront (POP) API API

    API : AWS EC2 API API CloudFront API Gateway CloudFront API API DNS Route53 API

    API : VPC API API API Gateway VPC

    API

    API Gateway API APIGateway API CSV API Lambda (p. 290) (p. 334)API

    API

    API (p. 4)

    4

    http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-latency

  • Amazon API Gateway API Gateway

    API

    API (: 'dev''prod''beta''v2' ) API APIID

    AWS API API API (p. 4)

    APIGateway API

    API Gateway API

    Velocity Template Language (VTL)

    API Gateway API API

    API API Gateway

    API SDK

    API

    VPC API

    API VPC API Gateway

    API Gateway HTTP Lambda HTTP API Gateway HTTP Lambda API Gateway Lambda

    5

    http://velocity.apache.org/engine/devel/vtl-reference.html

  • Amazon API Gateway API Gateway

    API Gateway Lambda HTTP greedy ({proxy+} ) ANY

    (p. 334) API 1 API API

    API Gateway API Gateway Amazon API Gateway

    Amazon API Gateway API AWS AWS_IAMCUSTOM COGNITO_USER_POOLS

    API API

    API Gateway

    6

    https://aws.amazon.com/api-gateway/pricing/http://docs.aws.amazon.com/apigateway/api-reference/resource/method/#authorizationType

  • Amazon API Gateway API GatewayAPI

    Amazon API Gateway Amazon API Gateway 3 AWS Lambda AWS HTTP API Gateway API API API Gateway API API Gateway API (p. 129)

    Amazon API Gateway API API

    API GatewayAPI (p. 7) API Gateway API (p. 10) Lambda API Gateway API (p. 19) HTTP API Gateway API (p. 39) API Gateway API (p. 74) AWS API Gateway API (p. 76)

    API GatewayAPI

    AWS (p. 7) IAM AWS (p. 8) IAM API Gateway API (p. 8) (p. 10)

    Amazon API Gateway AWS

    AWS AWS

    AWS

    1. https://aws.amazon.com/ [AWS ] 2.

    7

    https://aws.amazon.com/

  • Amazon API Gateway IAM

    AWS

    API Gateway API AWS Identity and AccessManagement API Gateway API API API Gateway IAM IAM AWS IAM IAM

    IAM AWS AWS Identity and Access Management(IAM) AWS AWS IAM API Gateway

    IAM API Gateway IAM IAM

    IAM API Gateway 1 IAM IAM Creating IAM Groups

    IAM API Gateway IAM IAM

    API API IAM ARN ARN API

    IAM API Gateway API AWS IAM AWS AWS IAM AWS IAM IAM

    IAM AWS CLI AWS SDK IAM IAM ARN AWS ARN arn:aws:iam::aws:policy/PolicyName ARN arn:aws:iam::123456789012:policy/PolicyName

    [AmazonAPIGatewayAdministrator] (arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator) AWS API API Gateway

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "apigateway:*" ], "Resource": "arn:aws:apigateway:*::/*" }

    8

    http://docs.aws.amazon.com/apigateway/api-reference/resource/http://docs.aws.amazon.com/apigateway/api-reference/resource/http://docs.aws.amazon.com/apigateway/api-reference/link-relation/http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html#Using_CreateUser_consolehttp://docs.aws.amazon.com/IAM/latest/UserGuide/id_groups_create.htmlhttp://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.htmlhttp://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html

  • Amazon API Gateway IAM API Gateway

    API

    ]}

    IAM AWS API Gateway (arn:aws:apigateway:*::/*) API Gateway ("Action":["apigateway:*"]) ("Effect":"Allow")

    API IAM Action "Action": ["apigateway:*"] "Action":["apigateway:GET","apigateway:POST"] Resource ["apigateway:*"] ["arn:aws:apigateway::123456789012:/restapis/*/documentation/*"] )IAM API (p. 270)

    API [AmazonAPIGatewayInvokeFullAccess] (arn:aws:iam::aws:policy/AmazonAPIGatewayInvokeFullAccess) AWS IAM API Gateway API

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "execute-api:Invoke" ], "Resource": "arn:aws:execute-api:*:*:*" } ]}

    IAM API IAM API (p. 270)

    IAM

    Note

    IAM IAM

    API Gateway AWS Lambda Amazon Simple Storage Service Amazon Kinesis AWS AWS API Gateway IAM Lambda

    { "Version": "2012-10-17", "Statement": [ {

    9

    http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-using.html#attach-managed-policy-consolehttp://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-using.html#attach-managed-policy-consolehttp://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage.html

  • Amazon API Gateway

    "Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "*" } ]}

    IAM API Gateway Lambda

    { "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "apigateway.amazonaws.com" }, "Action": "sts:AssumeRole" } ]}

    API Gateway

    API Gateway API IAM API 4 AWS CLI AWS SDK Amazon API Gateway API (p. 482)

    AWS IAM API Gateway API Gateway API API Gateway API (p. 10)

    API Gateway API API Gateway API Amazon APIGateway PetStore HTTP API API Swagger 2.0 API API Gateway API Gateway API API

    API http://petstore-demo-endpoint.execute-api.com/petstore/pets HTTP

    GET /: API API Gateway PetStore MOCK

    GET /pets: /pets API /pets PetStore HTTP

    10

    http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

  • Amazon API Gateway API

    URL http://petstore-demo-endpoint.execute-api.com/petstore/pets

    POST /pets: /petstore/pets API /pets PetStore HTTP

    GET /pets/{petId}: URL petId HTTP PetStore HTTP URL http://petstore-demo-endpoint.execute-api.com/petstore/pets/n n

    API MOCK OPTIONS CORS API GatewayCORS

    API Gateway API (p. 11) (p. 18) (p. 18)

    API Gateway API API Gateway API

    API

    1. https://console.aws.amazon.com/apigateway API Gateway 2.

    a. API API Gateway [Get Started]

    [OK] b. API API Gateway [API] [Create API ]

    3. [Create new API] [Examples API] [Import] API API API Gateway

    11

    https://console.aws.amazon.com/apigateway

  • Amazon API Gateway API

    [Import] Swagger API

    4. API

    [Resources] API API [Methods] URL API

    5. POST /pets

    12

  • Amazon API Gateway API

    (POST /pets) [Method Execution] [Method Request] [Method Response]API () API [Integration Request] [Integration Response] (http://petstore-demo-endpoint.execute-api.com/petstore/pets) API API [Method Request] API Gateway [Integration Request] [Integration Response] API Gateway API Gateway [Method Response] API Gateway

    API POST

    GET / MOCK [Integration Response] HTML API Gateway [Method Response] Mock API

    API API API HTTP API (p. 45)

    13

  • Amazon API Gateway API

    API

    6. ( )[Client] [Test] POST /pets ({"type": "dog","price": 249.99}) [Request Body][Test]

    14

  • Amazon API Gateway API

    15

  • Amazon API Gateway API

    PetStore 7.

    [Logs]

    16

  • Amazon API Gateway API

    API Gateway test-invoke-request API API

    8. API [PetStore] API [Actions] [Deploy API]

    API [Deploy API] [Deployment stage] [[New Stage]] [Stage name] (test )[Stage description] [Deployment description] [Deploy]

    17

  • Amazon API Gateway

    [Stage Editor] [Invoke URL] API GET / URL

    9. [Stage Editor] [Invoke URL] GET /

    10. [Stages] [test] [/pets/{petId}] [GET] [Invoke URL] [https://api-id.execute-api.region.amazonaws.com/test/pets/{petId}] {petId}

    () [Invoke URL] {petId} 1Enter 200 OK JSON

    { "id": 1, "type": "dog", "price": 249.99}

    API Authorization NONE AWS_IAM 4 HTTP API (p. 45)

    API API Gateway API

    1. AWS API RestApi 2. Resource API 3. Resource Method API API Gateway

    4. API Gateway

    API API Gateway HTTPMethod Request Integration Request API Gateway Integration Response Method Response API MOCK API Gateway

    (p. 134) API

    API Gateway Lambda (p. 290)Amazon API Gateway API (p. 393)

    18

    http://docs.aws.amazon.com/apigateway/api-reference/resource/rest-api/http://docs.aws.amazon.com/apigateway/api-reference/resource/resource/http://docs.aws.amazon.com/apigateway/api-reference/resource/method/

  • Amazon API Gateway Lambda API

    Lambda API Gateway API Lambda API Lambda Lambda API Lambda Lambda API Gateway Lambda Lambda Lambda

    Lambda Lambda Lambda APIAPI Lambda Lambda API Gateway API Lambda

    Lambda Lambda API Lambda

    Lambda API Gateway API (p. 19) Lambda API Gateway API (p. 28) Lambda API Gateway API (p. 29)

    Lambda API Gateway API API Gateway Lambda API Lambda () API Gateway (p. 130)

    AWS Lambda GetStartedLambdaProxyIntegration Node.js APIGateway GetStartedLambdaProxyIntegrationAPI Lambda API

    Lambda API Lambda (p. 20) Lambda API (p. 24) Lambda API (p. 25) Lambda API (p. 26)

    19

  • Amazon API Gateway Lambda API

    Lambda API Lambda JSON Lambda

    { "greeting": "Good {time}, {name} of {city}.[ Happy {day}]"}

    {time} morningafternoon day {name} you {city} World {day} null {day} null Happy {day} Lambda

    API Gateway Lambda event Lambda response

    Lambda Node.jsPythonJava C# Node.js Java Node.js Python Java C#

    Lambda API Node.js (p. 20) Lambda API Python (p. 22) Lambda API C# (p. 22) Lambda API Java (p. 22)

    Lambda API Node.js Node.js Lambda "Hello World!" API Gateway event Lambda Lambda API Gateway HTTP Lambda Lambda (p. 140) Lambda (p. 142)

    'use strict';console.log('Loading hello world function'); exports.handler = function(event, context, callback) { let name = "you"; let city = 'World'; let time = 'day'; let day = ''; let responseCode = 200; console.log("request: " + JSON.stringify(event)); // This is a simple illustration of app-specific logic to return the response. // Although only 'event.queryStringParameters' are used here, other request data, // such as 'event.headers', 'event.pathParameters', 'event.body', 'event.stageVariables', // and 'event.requestContext' can be used to determine what response to return. // if (event.queryStringParameters !== null && event.queryStringParameters !== undefined) { if (event.queryStringParameters.name !== undefined &&

    20

  • Amazon API Gateway Lambda API

    event.queryStringParameters.name !== null && event.queryStringParameters.name !== "") { console.log("Received name: " + event.queryStringParameters.name); name = event.queryStringParameters.name; } } if (event.pathParameters !== null && event.pathParameters !== undefined) { if (event.pathParameters.proxy !== undefined && event.pathParameters.proxy !== null && event.pathParameters.proxy !== "") { console.log("Received proxy: " + event.pathParameters.proxy); city = event.pathParameters.proxy; } } if (event.headers !== null && event.headers !== undefined) { if (event.headers['day'] !== undefined && event.headers['day'] !== null && event.headers['day'] !== "") { console.log("Received day: " + event.headers.day); day = event.headers.day; } } if (event.body !== null && event.body !== undefined) { let body = JSON.parse(event.body) if (body.time) time = body.time; } let greeting = 'Good ' + time + ', ' + name + ' of ' + city + '. '; if (day) greeting += 'Happy ' + day + '!';

    var responseBody = { message: greeting, input: event }; // The output from a Lambda proxy integration must be // of the following JSON object. The 'headers' property // is for custom response headers in addition to standard // ones. The 'body' property must be a JSON string. For // base64-encoded payload, you must also set the 'isBase64Encoded' // property to 'true'. var response = { statusCode: responseCode, headers: { "x-custom-header" : "my custom header value" }, body: JSON.stringify(responseBody) }; console.log("response: " + JSON.stringify(response)) callback(null, response);};

    API Gateway event API Gateway JSON API HTTP (httpMethod) (path pathParameters)(queryStringParameters) (headers) (body) (requestContext) (stageVariables)

    Lambda event name proxy day time

    21

  • Amazon API Gateway Lambda API

    responseBody message API Gateway event input

    statusCode headers body JSON HTTP API Gateway

    Lambda API Python Python Lambda Python Lambda Node.js Lambda

    Lambda API C# C# Lambda C# Lambda Java Lambda

    Lambda API Java Java Lambda "Hello World!" Node.js (p. 20) API Gateway InputStream Lambda context LambdaLogger Java API Gateway Lambda HTTP OutputStream Lambda Lambda (p. 140) Lambda (p. 142)

    package examples;

    import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.BufferedReader;import java.io.Writer;

    import com.amazonaws.services.lambda.runtime.RequestStreamHandler;import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger;

    import org.json.simple.JSONObject;import org.json.simple.JSONArray;import org.json.simple.parser.ParseException;import org.json.simple.parser.JSONParser;

    public class ProxyWithStream implements RequestStreamHandler { JSONParser parser = new JSONParser();

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

    LambdaLogger logger = context.getLogger(); logger.log("Loading Java Lambda handler of ProxyWithStream");

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    22

    http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model.htmlhttp://docs.aws.amazon.com/lambda/latest/dg/dotnet-programming-model.htmlhttp://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.htmlhttp://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html

  • Amazon API Gateway Lambda API

    JSONObject responseJson = new JSONObject(); String name = "you"; String city = "World"; String time = "day"; String day = null; String responseCode = "200";

    try { JSONObject event = (JSONObject)parser.parse(reader); if (event.get("queryStringParameters") != null) { JSONObject qps = (JSONObject)event.get("queryStringParameters"); if ( qps.get("name") != null) { name = (String)qps.get("name"); } }

    if (event.get("pathParameters") != null) { JSONObject pps = (JSONObject)event.get("pathParameters"); if ( pps.get("proxy") != null) { city = (String)pps.get("proxy"); } }

    if (event.get("headers") != null) { JSONObject hps = (JSONObject)event.get("headers"); if ( hps.get("day") != null) { day = (String)hps.get("day"); } } if (event.get("body") != null) { JSONObject body = (JSONObject)parser.parse((String)event.get("body")); if ( body.get("time") != null) { time = (String)body.get("time"); } }

    String greeting = "Good " + time + ", " + name + " of " + city + ". "; if (day!=null && day != "") greeting += "Happy " + day + "!";

    JSONObject responseBody = new JSONObject(); responseBody.put("input", event.toJSONString()); responseBody.put("message", greeting);

    JSONObject headerJson = new JSONObject(); headerJson.put("x-custom-header", "my custom header value");

    responseJson.put("isBase64Encoded", false); responseJson.put("statusCode", responseCode); responseJson.put("headers", headerJson); responseJson.put("body", responseBody.toString());

    } catch(ParseException pex) { responseJson.put("statusCode", "400"); responseJson.put("exception", pex); }

    logger.log(responseJson.toJSONString()); OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); writer.write(responseJson.toJSONString()); writer.close(); }}

    23

  • Amazon API Gateway Lambda API

    API Gateway API Gateway JSON API HTTP (httpMethod) (path pathParameters) (queryStringParameters) (headers) (body) (requestContext) (stageVariables)

    Lambda inputStream name proxy day time context LambdaLogger

    responseBody message API Gateway (event)

    statusCode headers body JSON HTTP API Gateway

    Lambda Lambda AWS Lambda

    Lambda API Lambda API Gateway Lambda

    Lambda API Lambda

    1. https://console.aws.amazon.com/lambda Lambda 2. Lambda 3. [Functions]

    4. [Create function] [Author from scratch] [Blueprints]

    5. [Author from scratch]

    a. [Name] b. [Runtime]

    Node.js 4.3 c. [Role] [Choose an existing role][Create new role from template(s)]

    [Create a custom role] d. [Create function]

    [Designer] [Function code]

    6. Node Python Amazon S3 Lambda Java C# Amazon S3the section called Lambda API Lambda (p. 20)

    7. [Save] Lambda 8. () [Test] Lambda

    (p. 140)

    24

    http://docs.aws.amazon.com/lambda/latest/dg/lambda-java-how-to-create-deployment-package.htmlhttp://docs.aws.amazon.com/lambda/latest/dg/lambda-java-how-to-create-deployment-package.htmlhttps://console.aws.amazon.com/lambda

  • Amazon API Gateway Lambda API

    Note

    Lambda API

    Lambda API API Gateway Lambda API

    Lambda API

    1. https://console.aws.amazon.com/apigateway API Gateway 2. API [Create new API] API [Create API]

    API

    a. [New API] b. [API Name] c. [Description] d. [Create API]

    API LambdaSimpleProxy 3. [Resources] [Actions]

    [Create Resource] [New Child Resource]

    a. [Configure as proxy resource]

    b. [Resource Name*] c. [Resource Path*]

    d. [Create Resource] e. [Enable API Gateway CORS]

    (/) [Configure as proxyresource] [Resource Name] proxy [Resource Path] /{proxy+} [Enable API Gateway CORS]

    4. Lambda ANY

    a. [Actions] [Create Method]

    b. HTTP [ANY]

    c. [Integration type] [Lambda Function Proxy] d. [Lambda Region] e. [Lambda Function] Lambda f. [Save] g. [Add Permission to Lambda Function] [OK]

    [Lambda Function] GetStartedLambdaProxyIntegration (p. 20)

    25

    https://console.aws.amazon.com/apigateway

  • Amazon API Gateway Lambda API

    Lambda API API Gateway Lambda

    Lambda API

    Lambda GetStartedLambdaProxyIntegration (p. 20)

    API GET

    a. API [Actions] [Deploy API] ()API [Stage Editor] [Invoke URL] API URL

    b. GET [InvokeURL] URL Enter

    API test API URL (:https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test)

    API URL GET API URL POSTMANcURL REST API

    cURL API

    1. 2. POST /Seattle?time=evening

    cURL

    curl -v -X POST \ 'https://r275xc9bmd.execute-api.us-west-2.amazonaws.com/test/Seattle?time=evening' \ -H 'content-type: application/json' \ -H 'day: Thursday' \ -H 'x-amz-docs-region: us-west-2' \ -d '{ "callerName": "John"}'

    { "message": "Good day, John of Seattle. Happy Friday!", "input": { "resource": "/{proxy+}", "path": "/Seattle", "httpMethod": "POST", "headers": { "day": "Friday"

    26

    https://www.getpostman.com/https://curl.haxx.se/

  • Amazon API Gateway Lambda API

    }, "queryStringParameters": { "time": "morning" }, "pathParameters": { "proxy": "Seattle" }, "stageVariables": null, "requestContext": { "path": "/{proxy+}", "accountId": "123456789012", "resourceId": "nl9h80", "stage": "test-invoke-stage", "requestId": "test-invoke-request", "identity": { "cognitoIdentityPoolId": null, "accountId": "123456789012", "cognitoIdentityId": null, "caller": "AIDXXX...XXVJZG", "apiKey": "test-invoke-api-key", "sourceIp": "test-invoke-source-ip", "accessKey": "ASIXXX...XXDQ5A", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::123456789012:user/kdeding", "userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_131)", "user": "AIDXXX...XXVJZG" }, "resourcePath": "/{proxy+}", "httpMethod": "POST", "apiId": "r275xc9bmd" }, "body": "{ \"callerName\": \"John\" }", "isBase64Encoded": false }}

    POST PUT 3. GET /Boston?time=morning

    cURL

    curl -X GET \ 'https://r275xc9bmd.execute-api.us-west-2.amazonaws.com/test/Seattle?time=evening' \ -H 'content-type: application/json' \ -H 'day: Thursday' \ -H 'x-amz-docs-region: us-west-2'

    POST 200 OK Request GET body null

    Note

    requestContext $context (p. 205) API API Gateway Lambda (p. 140)

    27

  • Amazon API Gateway Lambda API

    Lambda APIGateway API AWS Lambda API AWS Amazon API Gateway API Lambda

    Amazon API Gateway Lambda

    the section called API (p. 10) API Lambda Lambda 2 Lambda API

    API Gateway Lambda API the section called API (p. 10) API

    API

    1. API Gateway 2. API Gateway [API] [Create API] (API ) 3. [Create new API] ( API ) [Examples API] ( API) 4. [] [Edge optimized] () 5. [] API

    Lambda API Lambda

    Lambda

    1. API Gateway API Lambda 2. [Create function] 3. [Author from scratch] 4. [Author from scratch]

    a. [Name] b. [Runtime]

    Node.js 6.10 c. [Role] [Choose an existing role][Create new role from template(s)]

    [Create a custom role] d. [Create function]

    [Designer] [Function code]

    5. [Function code] () 6. API Gateway Node.js (p. 20)Java (p. 22)

    7. []

    28

  • Amazon API Gateway Lambda API

    8. [Save] 9. ARN (Lambda )

    Lambda

    Lambda Lambda API Gateway API

    Lambda

    1. API Gateway API 2. [] 3. [] GET 4. [Method Execution] [Integration Request] 5. [Integration Type] () [Lambda ] 6. [Use Lambda Proxy integration] (Lambda ) 7. [Lambda ] 8. [Lambda ] 2 Lambda ARN

    9. [Add Permission to Lambda Function: You have selected a Lambda function from another

    account.Please ensure that you have the appropriate Function Policy on this function. You can do thisby running the following AWS CLI command from account 123456789012:] aws lambda add-permission

    10. aws lambda add-permission 2 AWS CLI2 Lambda

    11. Lambda [OK] 12. Lambda

    a. b. [Designer] ()

    [Function policy] () AWS:SourceArn API GET ARN Condition Allow

    Lambda API Gateway API

    Note

    Lambda Lambda API Lambda thesection called Lambda API (p. 19)

    API Gateway API Lambda Lambda AWS Lambda Lambda AWS Lambda Developer Guide

    API Lambda Lambda API Gateway API

    29

    http://docs.aws.amazon.com/lambda/latest/dg/

  • Amazon API Gateway Lambda API

    Lambda AWS Lambda API GatewayAPI (p. 534)

    API Lambda AWS Lambda Lambda

    Lambda Lambda (p. 30) Lambda API (p. 33) API (p. 36) API (p. 37) API (p. 38) (p. 38)

    Lambda Lambda Note

    Lambda AWS

    Hello World! Lambda Lambda GetStartedLambdaIntegration Lambda GetStartedLambdaProxyIntegration (p. 20)

    Lambda GetStartedLambdaIntegration Node.js

    'use strict';var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var times = ['morning', 'afternoon', 'evening', 'night', 'day'];

    console.log('Loading function');

    exports.handler = function(event, context, callback) { // Parse the input for the name, city, time and day property values let name = event.name === undefined ? 'you' : event.name; let city = event.city === undefined ? 'World' : event.city; let time = times.indexOf(event.time)

  • Amazon API Gateway Lambda API

    "Good [morning|afternoon|day], [name|you] in [city|World].Happy day!" Lambda JSON

    { "city": "...", "time": "...", "day": "...", "name" : "..."}

    AWS Lambda Developer Guide

    console.log(...) Amazon CloudWatch GetStartedLambdaIntegration Lambda IAM CloudWatch Lambda IAM

    Swagger API API Gateway API Lambda API Gateway /Lambda API Gateway API IAM API (p. 270)

    Lambda Lambda GetStartedLambdaProxyIntegation (p. 20) Lambda Lambda GetStartedLambdaIntegration APIGateway API JSON BLOB Lambda Lambda JSON Lambda GetStartedLambdaIntegration API API Gateway API API API API

    Lambda GetStartedLambdaIntegration

    Lambda Lambda GetStartedLambdaIntegration

    1. https://console.aws.amazon.com/lambda/ AWS Lambda 2.

    [Get Started][Create a function] [Lambda > Functions] [Create a function]

    3. [Select blueprint] [Author from scratch] 4. [Configure triggers] [Next] 5. [Configure function]

    a. [Basic information]:

    [Name] Lambda GetStartedLambdaIntegration [Description] Lambda

    [Runtime] [Node.js 6.10]

    b. [Lambda function code]:

    31

    http://docs.aws.amazon.com/lambda/latest/dg/welcome.htmlhttps://github.com/awslabs/api-gateway-secure-pet-store/blob/master/src/main/resources/swagger.yaml#L39https://console.aws.amazon.com/lambda/

  • Amazon API Gateway Lambda API

    [Content entry type] [Edit code inline] Lambda

    c. [Lambda function handler and role]:

    [Handler] index.handler [Role] Create new role from template(s) [Role name] (: GetStartedLambdaIntegrationRole) [Policy templates] Simple Microservice permissions

    Tip

    IAM [Role] Choose an existing roleCreate a Custom Role

    d. [Tags] e. [Advanced settings] f. [] g. [Create function] AWS

    6. [Actions][Configure test event]

    a. [Input test event] [Save and test]

    { "name": "Jonny", "city": "Seattle", "time": "morning", "day": "Wednesday"}

    b. [Test] [Execution result: succeeded] [Detail]

    { "greeting": "Good morning, Jonny of Seattle. Happy Wednesday!"}

    CloudWatch

    IAM Lambda IAM (GetStartedLambdaIntegrationRole) IAM 2 1 Lambda Lambda CloudWatch CloudWatch CreateLogGroup Lambda HelloWorldForLambdaIntegration CloudWatch

    { "Version": "2012-10-17",

    32

  • Amazon API Gateway Lambda API

    "Statement": [ { "Effect": "Allow", "Action": "logs:CreateLogGroup", "Resource": "arn:aws:logs:region:account-id:*" }, { "Effect": "Allow", "Action": [ "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": [ "arn:aws:logs:region:account-id:log-group:/aws/lambda/GetStartedLambdaIntegration:*" ] } ]}

    AWS

    IAM (lambda.amazonaws.com)

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ]}

    Lambda CloudWatchLogs console.log()

    AWS Lambda IAM

    Lambda API Lambda (GetStartedLambdaIntegration) API Gateway API HTTP Lambda URL API API Gateway API Gateway Lambda

    API GetStartedLambdaIntegrationAPI

    Lambda Lambda API

    1. API Gateway

    33

  • Amazon API Gateway Lambda API

    2. [Create new API]

    a. [API name] GetStartedLambdaIntegrationAPIb. [Description] API c. [Create API]

    3. [Resources] (/) [Actions] [Create Resource]

    a. [Resource Name] cityb. [Resource Path] {city}

    Lambda

    c. [Enable API Gateway Cors] d. [Create Resource]

    4. /{city} [Actions] [Create Method]

    a. [HTTP ] ANY ANY HTTP HTTP ANY Lambda Lambda

    b. 5. [Method Execution] [ANY /{city}]

    a. Integration type [Lambda Function] b. Lambda [Use Lambda Proxy integration]

    c. Lambda (: us-west-2) d. [Lambda Function] Lambda (: GetStartedLambdaIntegration)

    e. [Save] f. [Add Permission to Lambda Function] [OK] API Lambda

    API Gateway 6. [Method Execution] [Method Request]

    (time) (day) (callerName)

    /{city}

    a. [Settings] [Request Validator] Validate body, query string parameters, and headers API Gateway Lambda

    b. [URL Query String Parameters] [Add query string] [Name] time[Required] [Caching]

    c. [HTTP Request Headers] [Add header] [Name] day[Required]

    34

  • Amazon API Gateway Lambda API

    [Caching]

    d.

    i. API Gateway API [Models] [Create]

    ii. [Model name] GetStartedLambdaIntegrationUserInputiii. [Content type] application/jsoniv. [Model description] v. [Model schema]

    { "$schema": "http://json-schema.org/draft-04/schema#", "title": "GetStartedLambdaIntegrationInputModel", "type": "object", "properties": { "callerName": { "type": "string" } }}

    vi. [Save] vii. [Method Request] [Request body] [Add model]

    [Content type] application/json[Model name] GetStartedLambdaIntegrationInput

    7. ANY /{city} [Method Execution] [Integration Request] (nameQuery nameHeader) Lambda JSON

    a. [] [Add mapping template] [Content-Type] [application/json]

    b. VTL [Save]

    8. [Integration Request] Lambda

    a. [Body mapping templates] b. [Request body passthrough] When there are no templates defined

    c. [Add mapping template] d. [Content-type] application/jsone. f. [Generate template] GetStartedLambaIntegrationUserInput

    g.

    #set($inputRoot = $input.path('$')){ "city": "$input.params('city')", "time": "$input.params('time')", "day": "$input.params('day')", "name": "$inputRoot.callerName"

    35

  • Amazon API Gateway Lambda API

    }

    API API API Gateway API

    POST /Seattle?time=morningday:Wednesday

    { "callerName": "John"}

    ANY POST{city} Seattle Wednesday day "John" callerName

    ANY /{city}

    1. [Method Execution] [Test] 2. [Method] POST 3. {city} Seattle 4. day morning 5. [Request Body] { "callerName":"John" } 6. [Test] 7.

    { "greeting": "Good morning, John of Seattle. Happy Wednesday!"}

    8. API Gateway

    Execution log for request test-requestThu Aug 31 01:07:25 UTC 2017 : Starting execution for request: test-invoke-requestThu Aug 31 01:07:25 UTC 2017 : HTTP Method: POST, Resource Path: /SeattleThu Aug 31 01:07:25 UTC 2017 : Method request path: {city=Seattle}Thu Aug 31 01:07:25 UTC 2017 : Method request query string: {time=morning}Thu Aug 31 01:07:25 UTC 2017 : Method request headers: {day=Wednesday}Thu Aug 31 01:07:25 UTC 2017 : Method request body before transformations: { "callerName": "John" }Thu Aug 31 01:07:25 UTC 2017 : Request validation succeeded for content type application/jsonThu Aug 31 01:07:25 UTC 2017 : Endpoint request URI: https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:GetStartedLambdaIntegration/invocationsThu Aug 31 01:07:25 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=****************************************************************************************************************************************************************************************************************************************************************************************************************************************338c72, X-Amz-Date=20170831T010725Z, x-amzn-apigateway-api-id=beags1mnid, X-Amz-Source-Arn=arn:aws:execute-api:us-west-2:123456789012:beags1mnid/null/POST/{city}, Accept=application/json, User-Agent=AmazonAPIGateway_beags1mnid, X-Amz-Security-Token=FQoDYXdzELL//////////wEaDMHGzEdEOT/VvGhabiK3AzgKrJw+3zLqJZG4PhOq12K6W21+QotY2rrZyOzqhLoiuRg3CAYNQ2eqgL5D54+63ey9bIdtwHGoyBdq8ecWxJK/YUnT2Rau0L9HCG5p7FC05h3IvwlFfvcidQNXeYvsKJTLXI05/

    36

  • Amazon API Gateway Lambda API

    yEnY3ttIAnpNYLOezD9Es8rBfyruHfJfOqextKlsC8DymCcqlGkig8qLKcZ0hWJWVwiPJiFgL7laabXs++ZhCa4hdZo4iqlG729DE4gaV1mJVdoAagIUwLMo+y4NxFDu0r7I0/EO5nYcCrppGVVBYiGk7H4T6sXuhTkbNNqVmXtV3ch5bOlh7 [TRUNCATED]Thu Aug 31 01:07:25 UTC 2017 : Endpoint request body after transformations: { "city": "Seattle", "time": "morning", "day": "Wednesday", "name" : "John"}Thu Aug 31 01:07:25 UTC 2017 : Sending request to https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:GetStartedLambdaIntegration/invocationsThu Aug 31 01:07:25 UTC 2017 : Received response. Integration latency: 328 msThu Aug 31 01:07:25 UTC 2017 : Endpoint response body before transformations: {"greeting":"Good morning, John of Seattle. Happy Wednesday!"}Thu Aug 31 01:07:25 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=c0475a28-8de8-11e7-8d3f-4183da788f0f, Connection=keep-alive, Content-Length=62, Date=Thu, 31 Aug 2017 01:07:25 GMT, X-Amzn-Trace-Id=root=1-59a7614d-373151b01b0713127e646635;sampled=0, Content-Type=application/json}Thu Aug 31 01:07:25 UTC 2017 : Method response body after transformations: {"greeting":"Good morning, John of Seattle. Happy Wednesday!"}Thu Aug 31 01:07:25 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-59a7614d-373151b01b0713127e646635, Content-Type=application/json}Thu Aug 31 01:07:25 UTC 2017 : Successfully completed executionThu Aug 31 01:07:25 UTC 2017 : Method completed with status: 200

    API API API API API API API APIAPI

    API

    1. [APIs] API [Resources] [Actions] [Deploy API]

    2. [Deployment stage] [New Stage] 3. [Stage name] (: test)

    Note

    UTF-8 ()

    4. [Stage description] 5. [Deployment description] 6. [Deploy] API [Stage Editor] [Invoke URL]

    API URL () URL https://api-id.region.amazonaws.com/stageName us-west-2 test API (beags1mnid) URL https://beags1mnid.execute-api.us-west-2.amazonaws.com/test

    37

  • Amazon API Gateway Lambda API

    API API URL GET API URL POSTMANcURL REST API

    cURL API

    1. 2. POST /Seattle?time=evening

    cURL

    curl -v -X POST \ 'https://beags1mnid.execute-api.us-west-2.amazonaws.com/test/Seattle?time=evening' \ -H 'content-type: application/json' \ -H 'day: Thursday' \ -H 'x-amz-docs-region: us-west-2' \ -d '{ "callerName": "John"}'

    {"greeting":"Good evening, John of Seattle. Happy Thursday!"}

    POST PUT 3. GET /Boston?time=morning

    cURL

    curl -X GET \ 'https://beags1mnid.execute-api.us-west-2.amazonaws.com/test/Boston?time=morning' \ -H 'content-type: application/json' \ -H 'day: Thursday' \ -H 'x-amz-docs-region: us-west-2' \ -d '{ "callerName": "John"}'

    400 Bad Request

    {"message": "Invalid request body"}

    GET

    Lambda IAM

    38

    https://www.getpostman.com/https://curl.haxx.se/

  • Amazon API Gateway HTTP API

    Warning

    Lambda Lambda API Lambda API Lambda Lambda Lambda IAM Lambda API IAM IAM

    Lambda

    1. AWS https://console.aws.amazon.com/lambda/ AWS Lambda

    2. [GetHelloWorld] [Actions][Delete function] [Delete]

    3. [GetHelloWithName] [Actions][Delete function] [Delete]

    IAM

    1. https://console.aws.amazon.com/iam/ IAM 2. [Details] [Roles] 3. [APIGatewayLambdaExecRole] [Role Actions][Delete Role]

    [Yes, Delete] 4. [Details] [Policies] 5. [APIGatewayLambdaExecPolicy] [Policy Actions][Delete]

    [Delete]

    HTTP API Gateway API HTTP API HTTP HTTP API HTTP HTTP

    HTTP API (p. 39) HTTP API (p. 45)

    HTTP API API Gateway HTTP API API HTTP (: ) HTTP API Gateway URL HTTP

    39

    https://console.aws.amazon.com/lambda/https://console.aws.amazon.com/iam/

  • Amazon API Gateway HTTP API

    HTTP API API Gateway

    ({proxy+}) HTTP ANY HTTP API API HTTP API

    HTTP API Gateway API ({proxy+}) PetStore ANY HTTP

    API Gateway HTTP API (p. 40) HTTP API (p. 42)

    API Gateway HTTP APIAPI Gateway HTTP API HTTP HTTP API (p. 45) PetStore (http://petstore-demo-endpoint.execute-api.com/petstore/pets) API Gateway UI API Gateway API

    PetStore HTTP API

    1. https://console.aws.amazon.com/apigateway API Gateway 2. API [Create new API] API [Create API]

    API

    a. [New API] b. [API Name] c. [Description] d. [Create API]

    API ProxyResourceForPetStore 3. [Resources] [Actions]

    [Create Resource] [New Child Resource]

    a. [Configure as proxy resource]

    b. [Resource Name*] c. [Resource Path*]

    d. [Create Resource] e. [Enable API Gateway CORS]

    40

    https://console.aws.amazon.com/apigateway

  • Amazon API Gateway HTTP API

    [Configure as proxy resource] [Resource Name] proxy [Resource Path] /{proxy+} [Enable API GatewayCORS]

    4. HTTP ANY

    a. [Actions] [Create Method]

    b. HTTP [ANY]

    c. [Integration type] [HTTP Proxy] d. [Endpoint URL] HTTP URL e. f. [Save] ANY

    [Endpoint URL] http://petstore-demo-endpoint.execute-api.com/{proxy}

    41

  • Amazon API Gateway HTTP API

    API API ({proxy+}) http://petstore-demo-endpoint.execute-api.com/ petstorepetstore/petspetstore/pets/{petId} ANY HTTP

    HTTP API

    PetStore API /petstore (404 Resource Not Found) (Cannot GET /petstore)

    API Gateway

    HTTP PetStore API

    1. API Gateway API

    a. [Resources] [ANY] b. [Method Execution] [Test] c. [Method] HTTP d. [Path] e. [Query Strings] f. [Headers] 1

    42

  • Amazon API Gateway HTTP API

    g. [Stage Variables]

    h. [Client Certificate] API Gateway

    i. [Request Body]

    j. [Test]

    [Method] ANY GET[Path] ({proxy}) petstore/pets[Query Strings] type=fish

    43

  • Amazon API Gateway HTTP API

    44

  • Amazon API Gateway HTTP API

    GET /petstore/pets?type=fish

    [ { "id": 1, "type": "fish", "price": 249.99 }, { "id": 2, "type": "fish", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 }]

    GET /petstore 404 Cannot GET /petstore GET /petstore/pets/1 PetStore 200 OK

    { "id": 1, "type": "dog", "price": 249.99}

    2. API GET

    a. API [Actions] [Deploy API] ()API [Stage Editor] [Invoke URL] API URL

    b. GET [InvokeURL] URL Enter

    API test petstore/pets?type=fish API URL https://4z9giyi2c1.execute-api.us-west-2.amazonaws.com/test/petstore/pets?type=fish URL

    API Gateway TestInvoke

    HTTP API Amazon API Gateway API API API API API

    45

  • Amazon API Gateway HTTP API

    HTTP API (p. 46) API Gateway API (p. 54) (p. 62)

    HTTP API API API

    1. https://console.aws.amazon.com/apigateway API Gateway 2. API [Create new API] API [Create API]

    API

    a. [New API] b. [API Name] c. [Description] d. [Create API]

    API [] (/) PetStore (http://petstore-demo-endpoint.execute-api.com/petstore/pets.) HTTP API /pets PetStore Pets GET

    3. /pets [Actions][Create Resource]

    [Resource Name ()] Pets[Resource Path ()] [Enable API Gateway CORS (API Gateway CORS )][Create Resource ()]

    46

    https://console.aws.amazon.com/apigateway

  • Amazon API Gateway HTTP API

    4. /pets GET [Actions] [Create Method]

    /pets [GET]

    Note

    API

    [POST]: PUT (

    ) DELETE: [PATCH]; [HEAD]: GET

    OPTIONS

    47

  • Amazon API Gateway HTTP API

    5. [Setup] [Integration type] [HTTP] [HTTP method] [GET] [Endpoint URL] http://petstore-demo-endpoint.execute-api.com/petstore/pets[Save]

    Note

    [HTTP method] HTTP Mock integration HTTP HTTP Lambda POST Lambda HTTP

    [Method Execution]

    PetStore ("Dog" "Cat" ) Pet type page

    48

  • Amazon API Gateway HTTP API

    6. GET [Method Execution] [Method Request] [Authorization] [AWS_IAM] [URL Query String Parameters] [Add querystring] type page 2

    PetStore AWS_IAM

    7. [Integration Request] [URL Query String Parameters] API [Mapped from]

    49

  • Amazon API Gateway HTTP API

    [Add query string]

    API API 8. API Gateway API GET /pets [Method

    Execution] [Test] [Method Test] [type] [page] Dog2[Test]

    50

  • Amazon API Gateway HTTP API

    ()

    51

  • Amazon API Gateway HTTP API

    API 9. API API [Actions] [Deploy API]

    52

  • Amazon API Gateway HTTP API

    [Deploy API] (API [[New Stage]]) (: "test""prod""dev" ) [Stage name] [Stage description] [Deployment description] () [Deploy]

    API URL (Invoke URL)

    53

  • Amazon API Gateway HTTP API

    GET ( NONE ) [URL ] URL AWS_IAM ID IAM AWS 4(SigV4) AWS SDK Postman cURL POSTPUTPATCH

    Postman API URL () URL

    https://api-id.execute-api.region.amazonaws.com/test/pets?type=Dog&page=2

    URL HTTP [GET] [AWS] [] [}

    [] AWS IAM AWS

    [] AWS IAM AWS

    [AWS ] URL API AWS

    [] API Gateway execute-api

    SDK SDK AWS SDK

    Note

    API URL API

    API Gateway API API Gateway API HTTP API APIGateway HTTP

    http://petstore-demo-endpoint.execute-api.com/petstore/pets

    URL Enter Return JSON

    [ { "id": 1, "type": "dog", "price": 249.99 },

    54

    http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.htmlhttp://docs.aws.amazon.com/general/latest/gr/sigv4_signing.htmlhttps://www.getpostman.com/http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.htmlhttps://aws.amazon.com/tools/

  • Amazon API Gateway HTTP API

    { "id": 2, "type": "cat", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 }]

    type page 2 URL

    http://petstore-demo-endpoint.execute-api.com/petstore/pets?type=cat&page=2

    2 JSON

    [ { "id": 4, "type": "cat", "price": 999.99 }, { "id": 5, "type": "cat", "price": 249.99 }, { "id": 6, "type": "cat", "price": 49.97 }]

    URL ID :

    http://petstore-demo-endpoint.execute-api.com/petstore/pets/1

    JSON ID 1

    { "id": 1, "type": "dog", "price": 249.99}

    GET POST Postman POST

    http://petstore-demo-endpoint.execute-api.com/petstore/pets

    Content-type: application/json

    55

    https://www.getpostman.com/

  • Amazon API Gateway HTTP API

    { "type": "dog", "price": 249.99}

    JSON

    { "pet": { "type": "dog", "price": 249.99 }, "message": "success"}

    PetStore HTTP API Gateway API

    http://petstore-demo-endpoint.execute-api.com/petstore/pets HTTP https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets API

    API 2 (petType petsPage) type page HTTP

    API URL {petId} ID URL {id} HTTP

    JSON HTTP

    (p. 56) 1: (p. 57) 2: (p. 57) 3: API (p. 60) 4: API (p. 60) (p. 62)

    Note

    ()

    1. API GatewayAPI (p. 7) (API Gateway IAM)

    2. HTTP API (p. 45)APIGateway MyDemoAPI API

    56

  • Amazon API Gateway HTTP API

    1: API HTTP 3

    1. [Resources] 1 (/) [Actions] [Create Resource]

    2. [Resource Name] petstorewalkthrough3. [Resource Path] [/petstorewalkthrough] [Create Resource]

    2

    1. [Resources] [/petstorewalkthrough] [Create Resource] 2. [Resource Name] pets3. [Resource Path] [/petstorewalkthrough/pets] [Create Resource]

    3

    1. [Resources] [/petstorewalkthrough/pets] [Create Resource] 2. [Resource Name] petIdHTTP ID

    3. [Resource Path] [petid] {petId}petId ({ }) /

    petstorewalkthrough/pets/{petId}[Create Resource]

    HTTP /petstore/pets/my-item-id

    2: HTTP GET

    GET

    GET /petstorewalkthrough/pets GET http://petstore-demo-endpoint.execute-api.com/petstore/pets

    petType petsPage type page

    1. [Resources] [/petstorewalkthrough/pets] [Actions] [CreateMethod] [/pets] [GET]

    2. [/petstorewalkthrough/pets - GET - Setup] [Integration type] [HTTP] [HTTPmethod] [GET]

    3. [Endpoint URL] http://petstore-demo-endpoint.execute-api.com/petstore/pets

    4. [Save] 5. [Method Execution] [Method Request] [URL Query String Parameters]

    57

  • Amazon API Gateway HTTP API

    6. [Add query string] 7. [Name] petType

    API petType 8. URL

    9. [Add query string] 10. [Name] petsPage

    API petsPage 11. URL

    12. [Method Execution][Integration Request] [URL Query String Parameters]

    13. [method.request.querystring.petType] [petType]

    [method.request.querystring.petsPage] [petsPage] URL type page

    14. [Add query string] 15. [Name] type URL

    16. [Mapped from] method.request.querystring.petType

    petType type

    17. URL

    18. [Add query string] 19. [Name] page URL

    20. [Mapped from] method.request.querystring.petsPage

    petsPage page

    21. URL

    22. [Method Execution] [Client] [TEST] [Query Strings] [petType] cat[petsPage] 2

    23. [Test] [Response Body]

    [ { "id": 4, "type": "cat", "price": 999.99 }, { "id": 5, "type": "cat", "price": 249.99 }, { "id": 6, "type": "cat", "price": 49.97

    58

  • Amazon API Gateway HTTP API

    }]

    2 GET

    GET /petstorewalkthrough/pets/{petId} GET http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}

    petId id

    1. [Resources] [/petstorewalkthrough/pets/{petId}] [Actions] [Create Method] HTTP [GET]

    2. [Setup] [Integration type] [HTTP] [HTTP method] [GET] 3. [Endpoint URL] [http://petstore-demo-endpoint.execute-api.com/petstore/pets/

    {id}] 4. [Save] 5. [Method Execution] [Integration Request] [URL Path Parameters]

    6. [Add path] 7. [Name] id 8. [Mapped from] method.request.path.petId

    petId id

    9. URL 10. [Method Execution] [Client] [TEST] [Path] [petId]

    111. [Test] [Response Body]

    { "id": 1, "type": "dog", "price": 249.99}

    POST

    POST /petstorewalkthrough/pets POST http://petstore-demo-endpoint.execute-api.com/petstore/pets

    JSON

    1. [Resources] [/petstorewalkthrough/pets] [Actions] [Create Method] HTTP [POST]

    2. [Setup] [Integration type] [HTTP] [HTTP method] [POST] 3. [Endpoint URL] http://petstore-demo-endpoint.execute-api.com/petstore/pets

    59

  • Amazon API Gateway HTTP API

    4. [Save] 5. [Method Execution] [Client] [TEST] [Request Body]

    { "type": "dog", "price": 249.99}

    6. [Test] [Response Body]

    { "pet": { "type": "dog", "price": 249.99 }, "message": "success"}

    3: API

    API API Gateway

    API

    1. [Resources] [Deploy API] 2. [Deployment stage] [test]

    Note

    UTF-8 ()

    3. [Deployment description] HTTP 4. [Deploy]

    4: API

    API Gateway API HTTP

    1. [Stage Editor] [Invoke URL] URL

    https://my-api-id.execute-api.region-id.amazonaws.com/test

    2. URL 3. /petstorewalkthrough/pets

    https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets

    URL

    [ { "id": 1,

    60

  • Amazon API Gateway HTTP API

    "type": "dog", "price": 249.99 }, { "id": 2, "type": "cat", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 }]

    4. petstorewalkthrough/pets ?petType=cat&petsPage=2

    https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets?petType=cat&petsPage=2

    5. URL

    [ { "id": 4, "type": "cat", "price": 999.99 }, { "id": 5, "type": "cat", "price": 249.99 }, { "id": 6, "type": "cat", "price": 49.97 }]

    6. petstorewalkthrough/pets ?petType=cat&petsPage=2/1

    https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets/1

    7. URL

    { "id": 1, "type": "dog", "price": 249.99}

    8. cURL POST URL /petstorewalkthrough/pets

    https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets

    61

  • Amazon API Gateway HTTP API

    Content-Type: application/json

    { "type": "dog", "price": 249.99}

    cURL

    curl -H "Content-Type: application/json" -X POST -d "{\"type\": \"dog\",\"price\": 249.99}" https://my-api-id.execute-api.region-id.amazonaws.com/test/petstorewalkthrough/pets

    { "pet": { "type": "dog", "price": 249.99 }, "message": "success"}

    API Gateway API (p. 62)

    API Gateway API Amazon APIGateway (p. 7)API Gateway API (p. 54)

    API Gateway HTTP AWS Lambda HTTP Lambda

    [ { "id": 1, "type": "dog", "price": 249.99 }, { "id": 2, "type": "cat", "price": 124.99 }, {

    62

  • Amazon API Gateway HTTP API

    "id": 3, "type": "fish", "price": 0.99 }]

    1 API Gateway API Gateway (p. 173)

    id numbertype classprice salesPrice

    [ { "number": 1, "class": "dog", "salesPrice": 249.99 }, { "number": 2, "class": "cat", "salesPrice": 124.99 }, { "number": 3, "class": "fish", "salesPrice": 0.99 }]

    2 id type description price askingPrice

    [ { "description": "Item 1 is a dog.", "askingPrice": 249.99 }, { "description": "Item 2 is a cat.", "askingPrice": 124.99 }, { "description": "Item 3 is a fish.", "askingPrice": 0.99 }]

    3 idtype price listings

    { "listings": [ "Item 1 is a dog. The asking price is 249.99.", "Item 2 is a cat. The asking price is 124.99.", "Item 3 is a fish. The asking price is 0.99." ]}

    63

  • Amazon API Gateway HTTP API

    1: (p. 64) 2: (p. 66) 3: GET (p. 66) 4: Lambda (p. 67) 5: (p. 68) 6: API (p. 72) 7: API (p. 72) 8: (p. 73) (p. 74)

    1: 4 3 HTTP Lambda Lambda

    1. https://console.aws.amazon.com/apigateway API Gateway 2. [MyDemoAPI] [Models] 3. [Create] 4. [Model name] PetsModelNoFlatten5. [Content type] application/json6. [Model description] ID 7. [Model schema] JSON

    { "$schema": "http://json-schema.org/draft-04/schema#", "title": "PetsModelNoFlatten", "type": "array", "items": { "type": "object", "properties": { "number": { "type": "integer" }, "class": { "type": "string" }, "salesPrice": { "type": "number" } } }}

    8. [Create model]

    2

    1. [Create] 2. [Model name] PetsModelFlattenSome3. [Content type] application/json4. [Model description] ID 5. [Model schema]

    { "$schema": "http://json-schema.org/draft-04/schema#",

    64

    https://console.aws.amazon.com/apigateway

  • Amazon API Gateway HTTP API

    "title": "PetsModelFlattenSome", "type": "array", "items": { "type": "object", "properties": { "description": { "type": "string" }, "askingPrice": { "type": "number" } } }}

    6. [Create model]

    3

    1. [Create] 2. [Model name] PetsModelFlattenAll3. [Content type] application/json4. [Model description] ID5. [Model schema]

    { "$schema": "http://json-schema.org/draft-04/schema#", "title": "PetsModelFlattenAll", "type": "object", "properties": { "listings": { "type": "array", "items": { "type": "string" } } }}

    6. [Create model]

    1. [Create] 2. [Model name] PetsLambdaModel3. [Content type] application/json4. [Model description] GetPetsInfo 5. [Model schema]

    { "$schema": "http://json-schema.org/draft-04/schema#", "title": "PetsLambdaModel", "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "type": { "type": "string" }, "price": { "type": "number" } } }}

    65

  • Amazon API Gateway HTTP API

    6. [Create model]

    2:

    4 3 HTTP 3 id type description price askingPrice Lambda

    1. [Resources] 2. [Resources] [/petstorewalkthrough] [Create Resource] 3. [Resource Name] NoFlatten4. [Resource Path] [/petstorewalkthrough/noflatten] [Create Resource]

    2

    1. [Resources] [/petstorewalkthrough] [Create Resource]

    2. [Resource Name] FlattenSome3. [Resource Path] [/petstorewalkthrough/flattensome] [Create Resource]

    3

    1. [Resources] [/petstorewalkthrough] [Create Resource]

    2. [Resource Name] FlattenAll3. [Resource Path] [/petstorewalkthrough/flattenall] [Create Resource]

    4

    1. [Resources] [/petstorewalkthrough] [Create Resource]

    2. [Resource Name] LambdaFlattenSome3. [Resource Path] [/petstorewalkthrough/lambdaflattensome] [Create

    Resource]

    3: GET

    GET

    GET

    1. [Resources] [/petstorewalkthrough/flattenall] [Create Method]

    2. [GET]

    66

  • Amazon API Gateway HTTP API

    3. [Integration type] [HTTP][HTTP method] [GET] [EndpointURL] http://petstore-demo-endpoint.execute-api.com/petstore/pets [Save]

    2 GET

    1. [Resources] [/petstorewalkthrough/lambdaflattensome] [Create Method]

    2. [GET] 3. [Integration type] [Lambda Function] [Lambda Region]

    GetPetsInfoLambda (p. 67)[Lambda Function] [GetPetsInfo] [Save] Lambda [OK]

    3 GET

    1. [Resources] [/petstorewalkthrough/flattensome] [Create Method]

    2. [GET]

    3. [Integration type] [HTTP][HTTP method] [GET] [EndpointURL] http://petstore-demo-endpoint.execute-api.com/petstore/pets [Save]

    4 GET

    1. [Resources] [/petstorewalkthrough/noflatten] [Actions][Create Method]

    2. [GET]

    3. [Integration type] [HTTP][HTTP method] [GET] [EndpointURL] http://petstore-demo-endpoint.execute-api.com/petstore/pets [Save]

    4: Lambda

    Lambda

    Lambda

    1. https://console.aws.amazon.com/lambda/ AWS Lambda 2.

    [Get Started Now] [Lambda: Function list] [Create a Lambda function]

    3. [Name] GetPetsInfo4. [Description] 5. [Code template] [None] 6.

    console.log('Loading event');

    67

    https://console.aws.amazon.com/lambda/

  • Amazon API Gateway HTTP API

    exports.handler = function(event, context, callback) { callback(null, [{"id": 1, "type": "dog", "price": 249.99}, {"id": 2, "type": "cat", "price": 124.99}, {"id": 3, "type": "fish", "price": 0.99}]); // SUCCESS with message};

    Tip

    Node.js console.log Amazon CloudWatch event Lambda context callback (Node.js 4.3 )Lambda AWS Lambda: How it Works AWS Lambda

    7. [Handler name] index.handler 8. [Role] Lambda API Gateway API (p. 19) Lambda

    APIGatewayLambdaExecRole 9. [C