Creating a REST API with a CloudFormation template

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

  1. Start to create the template by defining AWSTemplateFormatVersion and a suitable description (for example, API with Lambda Integration).
  2. Define our REST API with a FailOnWarnings setting of true, using AWS::ApiGateway::RestApi.
  3. Define a resource with the PathPart as lambdagreeting, using AWS::ApiGateway::Resource.
  4. Define a path parameter (PathPart) of '{name}'under the resource for the PathPart of lambdagreeting, by using AWS::ApiGateway::Resource.
  5. Define an http-method GET with an AWS integration type and corresponding AWS (Lambda) integration URI, by using AWS::ApiGateway::Method (similar to what we did in the section Creating a REST API with AWS CLI commands):
MyMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: GET
Integration:
Type: AWS
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
RequestTemplates:
application/json: "{"name": "$input.params('name')" , "time": "$input.params('time')"}"
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

ResourceId: !Ref NamePathParamResource
RestApiId: !Ref MyRestAPI
MethodResponses:
- StatusCode: 200

We use the intrinsic function ImportValue to refer to an exported Lambda name. We had exported the name of our Lambda from a previous Lambda in the section Creating the Lambda
  1. Deploy our API to a stage, dev, by using AWS::ApiGateway::Deployment.
  2. Add a permission for the API to invoke our Lambda, as follows:
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !ImportValue LambdaForApiGateway
Action: 'lambda:InvokeFunction'
Principal: apigateway.amazonaws.com
SourceArn: !Sub
- arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${API_ID}/*/GET/lambdagreeting/{name}
- API_ID: !Ref MyRestAPI
  1. Add an Outputs section that will return our final API endpoint of the form https://<rest-api-id>.execute-api.<aws-region>.amazonaws.com/dev/lambdagreeting/Heartin.
  2. Create the stack by using the create-stack sub-command, and verify the stack by using the describe-stacks sub-command. The complete template and commands are available in the code files.
  3. Test the API by running the URL from a browser, with and without the query parameters (see Creating a REST API with AWS CLI commands section for the expected output).
..................Content has been hidden....................

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