Skip to content

AWS SAM

HOA PHAN edited this page Nov 21, 2020 · 1 revision

Sample Helloworld with Logging configs:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  helloworld

  Sample SAM Template for helloworld
  
Globals:
  Function:
    Timeout: 3

Resources:
  AccessLogs:
    Type: AWS::Logs::LogGroup

  HelloWorldApi: 
    Type: AWS::Serverless::Api 
    Properties: 
      StageName: Prod
      EndpointConfiguration: REGIONAL
      TracingEnabled: true
      MethodSettings:
        # Correspond to CloudWatch Settings under Logs/Tracing
        - ResourcePath: '/*'
          HttpMethod: '*'
          # Disable data trace in production to avoid logging customer sensitive information from requests and responses
          DataTraceEnabled: true
          LoggingLevel: INFO
          MetricsEnabled: true

      AccessLogSetting:
        DestinationArn: !GetAtt AccessLogs.Arn
        Format: >
          {"requestId":"$context.requestId",
          "waf-error":"$context.waf.error",
          "waf-status":"$context.waf.status",
          "waf-latency":"$context.waf.latency",
          "waf-response":"$context.wafResponseCode",
          "authenticate-error":"$context.authenticate.error",
          "authenticate-status":"$context.authenticate.status",
          "authenticate-latency":"$context.authenticate.latency",
          "authorize-error":"$context.authorize.error",
          "authorize-status":"$context.authorize.status",
          "authorize-latency":"$context.authorize.latency",
          "integration-error":"$context.integration.error",
          "integration-status":"$context.integration.status",
          "integration-latency":"$context.integration.latency",
          "integration-requestId":"$context.integration.requestId",
          "integration-integrationStatus":"$context.integration.integrationStatus",
          "response-latency":"$context.responseLatency",
          "status":"$context.status"}

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api
          Properties:
            RestApiId: !Ref HelloWorldApi
            Path: /hello
            Method: get


    
Outputs:
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${HelloWorldApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

eye

Clone this wiki locally