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.
(Figure source: AWS X \ -Ray Console: -AWS X \ -Ray)
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
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):
...
-Lambda Layer and X \ -Ray -Qiita (repost) -Lambda Layers can now be built with AWS SAM CLI -Qiita (Repost)
Recommended Posts