Introduction#

lambda adot trace

Lambda Function#

  • python 3.11
  • enable tracing
  • add adot layer
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS Lambda Function'
Resources:
# create a role for lambda function
LambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: 'LambdaRole'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service:
- 'lambda.amazonaws.com'
Action:
- 'sts:AssumeRole'
# policies
Policies:
- PolicyName: 'LambdaPolicyLogs'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
- PolicyName: 'LambdaPolicyXRay'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
Resource: '*'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
# create a lambda function
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: 'LambdaFunction'
Handler: 'index.handler'
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import json
def handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Runtime: 'python3.11'
Timeout: 30
MemorySize: 1024
# layer
Layers:
- 'arn:aws:lambda:us-west-2:901920570463:layer:aws-otel-python-amd64-ver-1-24-0:1'
# enable tracing
TracingConfig:
Mode: 'Active'
# environment variables
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: '/opt/otel-instrument'
# outputs
Outputs:
LambdaFunctionArn:
Value: !GetAtt LambdaFunction.Arn
Description: 'Lambda function ARN'
Export:
Name:
Fn::Sub: '${AWS::StackName}-LambdaFunctionArn'

Reference#