[PYTHON] Easy X-Ray with Lambda Layer and CloudFormation / sam-cli

Introduction

This article Lambda Layer and X \ -Ray -Qiita shows how to incorporate X-Ray with Lambda Layer. When deploying multiple lambdas, it is quite nice because it can be attached and detached in one shot without incorporating X-Ray individually.

It feels pretty good, but I was wondering if it could be converted to CloudFormation because it would be a hassle to hit a few commands. [Lambda Layers can now be built with AWS SAM CLI. I found the article .com / hayao_k / items / f8c7ad5e35e29d590957).

So, if you grab the contents of the two articles, it will be like this

** Easy to make X-Ray LambdaLayer with CloudFormation! **

It is an article called.

X-Ray is a tracing tool that can be used with AWS Lambda. By using X-Ray, "Which part of Lambda is taking time?" "Which resource (DynamoDB, S3, etc) call is taking time?" Is as shown in the figure below. You will be able to check.

sample1

sample2

(Figure source: AWS X \ -Ray Console: -AWS X \ -Ray)

X-Ray Lambda Layer Template Definition

In addition to template.yaml, you only need ** xray-layer-src / requirements.txt **.

template.yaml

template.yaml


AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: CloufFormation Template X-Ray lambda layer sample

Resources:
  # X-Ray's Lambda Layer definition
  XRayLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      Description: Lambda Layer for XRay
      ContentUri: xray-layer-src
      CompatibleRuntimes:
        - python3.8  #Specify the Python runtime you are using here
    Metadata:
      BuildMethod: python3.8  # sam-Must be specified at build time with cli

  #Example of lambda definition using XRayLayer
  SomeFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: lambda/src/
      Handler: lambda_handler.lambda_handler
      Runtime: python3.8
      Tracing: Active
      Layers:
        - !Ref XRayLayer  #Reference of the created Layer

xray-layer-src/requirements.txt

xray-layer-src/requirements.txt


aws-xray-sdk

It is OK to specify only aws-xray-sdk here in the specification of the library to be included in Lambda Layer.

You can now create an X-Ray Lambda Layer resource. Deployment is the same as using regular sam.

sam build
sam deploy

Lambda example using X-Ray

The source code that uses X-Ray is as follows, for example.

lambda-src/lamda_handler.py


import ...

#Import the XRay SDK. Must be imported last as in the official documentation
from aws_xray_sdk.core import patch_all

#Collectively in related libraries such as boto3 X-Apply Ray patch
patch_all()

def lambda_handler(event, context):
    ...

Reference link

-Lambda Layer and X \ -Ray -Qiita (repost) -Lambda Layers can now be built with AWS SAM CLI -Qiita (Repost)

Recommended Posts

Easy X-Ray with Lambda Layer and CloudFormation / sam-cli
Easy Slackbot with Docker and Errbot
Easy modeling with Blender and Python
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
[AWS] Link Lambda and S3 with boto3
Easy! Use gensim and word2vec with MAMP.
Easy web scraping with Python and Ruby
Easy REST API with API Gateway / Lambda / DynamoDB
Play with Lambda layer (python) for about 5 minutes
[AWS] Try tracing API Gateway + Lambda with X-Ray
Dynamic HTML pages made with AWS Lambda and Python
Easy IoT to start with Raspberry Pi and MESH
Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
Easy machine learning with scikit-learn and flask ✕ Web app
Easy partial download of mp4 with python and youtube-dl!
Create a Layer for AWS Lambda Python with Docker
Easy face recognition try with Jetson Nano and webcam
Make ordinary tweets fleet-like with AWS Lambda and Python