Creating the API with a CloudFormation template

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

  1. Start to create the template by defining AWSTemplateFormatVersion and a suitable Description.
  2. Create the REST API by using AWS::ApiGateway::RestApi.
  3. Create the path-part, lambdagreeting, by using AWS::ApiGateway::Resource.
  4. Define the method, with the http-method as POST, as follows:
MyMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
Type: AWS
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
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 GreetingResource
RestApiId: !Ref MyRestAPI
MethodResponses:
- StatusCode: 200
We have omitted the request-templates property, and we are now passing parameters within the body of our POST message.
  1. Deploy our API to a stage, dev, by using AWS::ApiGateway::Deployment.
  1. Add permission for the API to invoke the 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}/*/POST/lambdagreeting
- API_ID: !Ref MyRestAPI
  1. Add an Outputs section with the updated URI, as follows:
Outputs:
SampleEndpoint:
Description: 'POST Endpoint'
Value: !Sub
- https://${API_ID}.execute-api.${AWS::Region}.amazonaws.com/dev/lambdagreeting
- API_ID: !Ref MyRestAPI
..................Content has been hidden....................

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