Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLambda.py
More file actions
Latest commit
64 lines (51 loc) · 1.88 KB
/
Copy pathSimpleLambda.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
importboto3
importjson
importos
fromopentelemetryimporttrace
fromopentelemetry.sdk.resourcesimportSERVICE_NAME, Resource, DEPLOYMENT_ENVIRONMENT
fromopentelemetry.sdk.traceimportTracerProvider
fromopentelemetry.exporter.otlp.proto.http.trace_exporterimportOTLPSpanExporter
fromopentelemetry.sdk.trace.exportimportBatchSpanProcessor
# 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")
defecho(payload):
returnpayload
deflist_buckets(payload):
client=boto3.client("s3")
buckets=client.list_buckets()
bucket_names=list(map(lambdab: 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,
}
deflambda_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
'''
withtracer.start_as_current_span(f"op-{event['operation']}") asspan:
operation=event['operation']
payload=event['payload']
ifoperationinoperations:
returnoperations[operation](payload)
else:
raiseValueError(f'Unrecognized operation "{operation}"')