Snippets

Spenser Filler API Gateway/Lambda CFT example

Created by Spenser Filler last modified
Resources:
  LogEventLambda:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: 'index.lambda_handler'
      FunctionName: 'log-event-example'
      Role: !Join
        - ''
        - - 'arn:aws:iam::'
          - !Ref 'AWS::AccountId'
          - ':role/LambdaBasicExecution'
      Code:
        ZipFile: |
          def lambda_handler(event, context):
            print(event)
            return { 
                "status" : "success"
            }  
      Runtime: python3.6
      Timeout: '25'
      TracingConfig:
        Mode: Active
      MemorySize: 128
  LogEventAPI:
      Type: 'AWS::ApiGateway::RestApi'
      Properties:
        Name: 'log-event-api'
  LogEventResource:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      RestApiId: !Ref LogEventAPI
      PathPart: logerror
      ParentId: !GetAtt 
        - LogEventAPI
        - RootResourceId
  LogEventPOST:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      AuthorizationType: NONE
      HttpMethod: POST
      Integration:
        Type: AWS
        IntegrationHttpMethod: POST
        Uri: !Join 
          - ''
          - - 'arn:aws:apigateway:'
            - !Ref 'AWS::Region'
            - >-
              :lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:
            - !Ref 'AWS::AccountId'
            - :function:log-event-example
            - /invocations
        IntegrationResponses:
          - StatusCode: 200
            ResponseTemplates:
              application/json: ''
            ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: >-
                'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
              method.response.header.Access-Control-Allow-Methods: '''POST,OPTIONS'''
              method.response.header.Access-Control-Allow-Origin: '''*'''
        PassthroughBehavior: WHEN_NO_MATCH
      ResourceId: !Ref LogEventResource
      RestApiId: !Ref LogEventAPI
      MethodResponses:
        - StatusCode: 200
          ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: true
            method.response.header.Access-Control-Allow-Methods: true
            method.response.header.Access-Control-Allow-Origin: true
          ResponseModels:
            application/json: Empty
  LogEventOPTIONS:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      ResourceId: !Ref LogEventResource
      RestApiId: !Ref LogEventAPI
      AuthorizationType: NONE
      HttpMethod: OPTIONS
      Integration:
        Type: MOCK
        IntegrationResponses:
          - ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: >-
                'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
              method.response.header.Access-Control-Allow-Methods: '''POST,OPTIONS'''
              method.response.header.Access-Control-Allow-Origin: '''*'''
            ResponseTemplates:
              application/json: ''
            StatusCode: '200'
        PassthroughBehavior: NEVER
        RequestTemplates:
          application/json: '{"statusCode": 200}'
      MethodResponses:
        - ResponseModels:
            application/json: Empty
          ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: true
            method.response.header.Access-Control-Allow-Methods: true
            method.response.header.Access-Control-Allow-Origin: true
          StatusCode: '200'
  LogEventPermission:
    DependsOn: LogEventLambda
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:invokeFunction'
      FunctionName: 'log-event-example'
      Principal: apigateway.amazonaws.com
      SourceArn: !Join 
        - ''
        - - 'arn:aws:execute-api:'
          - !Ref 'AWS::Region'
          - ':'
          - !Ref 'AWS::AccountId'
          - ':'
          - !Ref LogEventAPI
          - /*

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.