-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLambda.py
More file actions
64 lines (51 loc) · 1.88 KB
/
SimpleLambda.py
File metadata and controls
64 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import boto3
import json
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import SERVICE_NAME, Resource, DEPLOYMENT_ENVIRONMENT
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Set up the OpenTelemetry tracer provider with a resource name.
resource = Resource(attributes={
SERVICE_NAME: "python-lambda-example",
DEPLOYMENT_ENVIRONMENT: "dev",
})
provider = TracerProvider(resource=resource)
span_processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
provider.add_span_processor(span_processor)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("python-lambda.tracer")
def echo(payload):
return payload
def list_buckets(payload):
client = boto3.client("s3")
buckets = client.list_buckets()
bucket_names = list(map(lambda b: b['Name'], buckets['Buckets']))
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Buckets": bucket_names,
"Region ": os.environ['AWS_REGION']
})
}
operations = {
'echo': echo,
'list_buckets': list_buckets,
}
def lambda_handler(event, context):
'''Provide an event that contains the following keys:
- operation: one of the operations in the operations dict below
- payload: a JSON object containing parameters to pass to the
operation being performed
'''
with tracer.start_as_current_span(f"op-{event['operation']}") as span:
operation = event['operation']
payload = event['payload']
if operation in operations:
return operations[operation](payload)
else:
raise ValueError(f'Unrecognized operation "{operation}"')