The CloudFormation template

Now, let's create the API by using the CloudFormation template. We will not discuss the components that were already discussed in previous recipes, nor will we discuss the theory for commands that were already discussed within the CLI commands section. The complete code is available in the code files:

  1. Start by defining the template version, Description, RestApi, and a path-part lambdagreeting.
  2. Create a model for request validation, as follows:
MyRequestValidationModel:
Type: AWS::ApiGateway::Model
Properties:
ContentType: application/json
Description: Greeting Request Model
Name: GreetingRequestModel
RestApiId: !Ref MyRestAPI
Schema: '{"$schema": "http://json-schema.org/draft-04/schema#",
"title": "greetingModel",
"type": "object",
"properties": {
"user" : {"type": "object",
"properties": {
"name" : {"type" : "string"}
}
},
"greeting" : {"type": "object",
"properties" : {
"time" : {"type" : "string"}
}
}
},
"required" : ["user", "greeting"]
}'
  1. Create a request validator, as follows:
MyRequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: GreetingRequestValidator
RestApiId: !Ref MyRestAPI
ValidateRequestBody: true
ValidateRequestParameters: false

  1. Use the model and validator within the AWS::ApiGateway::Method resource, as follows:
MyMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
Type: AWS
IntegrationHttpMethod: POST
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: "{ "name" : $input.json('$.user.name'), "time": $input.json('$.greeting.time') }"
IntegrationResponses:
- StatusCode: 200
ResponseTemplates:
application/json: "{ "greeting" : $input.json('$.message')}"
Uri:
!Sub
- 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:${AWS::AccountId}:function:${LAMBDA_NAME}/invocations'
- LAMBDA_NAME: !ImportValue LambdaForApiGateway
RequestModels:
application/json: !Ref MyRequestValidationModel
RequestValidatorId: !Ref MyRequestValidator
ResourceId: !Ref GreetingResource
RestApiId: !Ref MyRestAPI
MethodResponses:
- StatusCode: 200
  1. Add deployment, add Lambda permissions, and add an Outputs section, similar to the previous recipes.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.140