Skip to main content

AWS Lambda logging

This projects provides a function to log AWS Lambda executions to cloudwatch.

Requirements

  • python

Usage

from common_services import create_log_message

def handle_request(event,context):
    log = create_log_message.CreateAuditMessage(context=context)
    log.createauditmessage(business_key="test",business_value="test",log_level="INFO",trace_id="12334",information="this is a info message",message="received")

Structuring AWS Lambda Logs

  1. Use the CreateAuditMessage_class.py to structure your logs as a json

  2. Below are the logs structured when used the CreateAuditMessage_Class.

    Received:

    {
      "timestamp": "2020-03-05T22:40:51",
      "logEvent": "Received",
      "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
      "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
      "logLevel": "INFO",
      "componentName": "ship-gr-idoc-to-fusion-connectivity",
      "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
      "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKey": {
          "InboundDelivery": "533197700-533197700",
          "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
          "PlantCode": "1062"
       },
      "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
    }
    

    Delivered:

    {
          "timestamp": "2020-03-05T22:40:51",
          "logEvent": "Delivered",
          "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
          "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
          "logLevel": "INFO",
          "componentName": "ship-gr-idoc-to-fusion-connectivity",
          "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
          "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKey": {
              "InboundDelivery": "533197700-533197700",
              "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
              "PlantCode": "1062"
           },
          "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
      }
    

    Discarded:

    {
          "timestamp": "2020-03-05T22:40:51",
          "logEvent": "Discarded",
          "message" : "Invalid Carton number",
          "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
          "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
          "logLevel": "INFO",
          "componentName": "ship-gr-idoc-to-fusion-connectivity",
          "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
          "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKey": {
              "InboundDelivery": "533197700-533197700",
              "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
              "PlantCode": "1062"
           },
          "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
      }
    

    Failure:

    {
      "timestamp": "2020-03-05T22:40:53",
      "message": "An exception of type Exception occurred. Arguments:('Tansient Error while posting to fusion. Message will be retried OSB Service Callout action received SOAP Fault response',)",
      "logEvent": "Failure",
      "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
      "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
      "logLevel": "ERROR",
      "componentName": "ship-gr-idoc-to-fusion-connectivity",
      "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
      "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKey": {
          "InboundDelivery": "533197700-533197700",
          "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
          "PlantCode": "1062",
          "TrackingId": "a158f731-c424-44af-84fb-aca07fb9a0f7"
      },
      "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
    }
    
  3. It is recommended to write a Received log when the lambda is invoked and Delivered audit when the lambda execution is completed. This helps us to track the transaction status in the lambda. The business keys are unique attributes that can identify an event. Chose the business keys as per your business needs. When need be, we can create reports and stats using these business keys

  4. When the logs are structured as outlined above, we can create a Splunk dashboard using the below method

Release files for common-services 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for common-services 1.0.0
File Size Uploaded
common-services-1.0.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for common-services 1.0.0
File Interpreter ABI Platform
common_services-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / common-services-1.0.0.tar.gz

Download URL common-services-1.0.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0e7d7cddd727e4dc2dbef474ffa26dac6de0926853442ff9dfbb458d7198a721
BLAKE2b-256 checksum
How to use checksums
7650cdd96b204b6181a01b1a8da01fbf3b50d268e2a81f27eb895ed4fa72bc76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.2

Release files / common_services-1.0.0-py3-none-any.whl

Download URL common_services-1.0.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6de121f7cb775e38e1a6b50579b8423c087ddb66a6a82955c24eb0d8aa0dbcde
BLAKE2b-256 checksum
How to use checksums
ec83d469ae863fbfd747c80e21c018008e7b3cb436d83e0d54259d7b15549372
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page