Skip to content

Commit 931de51

Browse files
committed
fix: remove apigw contract when using event-handler, apigw tracing
1 parent 9e9b2cd commit 931de51

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed
157 KB
Loading

docs/quickstart.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,13 @@ The first option could be to use the standard Python Logger, and use a specializ
397397
@app.get("/hello/<name>")
398398
def hello_name(name):
399399
logger.info(f"Request from {name} received")
400-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
400+
return {"message": f"hello {name}!"}
401401

402402

403403
@app.get("/hello")
404404
def hello():
405405
logger.info("Request from unknown received")
406-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
406+
return {"message": "hello unknown!"}
407407

408408

409409
def lambda_handler(event, context):
@@ -469,13 +469,13 @@ app = ApiGatewayResolver()
469469
@app.get("/hello/<name>")
470470
def hello_name(name):
471471
logger.info(f"Request from {name} received")
472-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
472+
return {"message": f"hello {name}!"}
473473

474474

475475
@app.get("/hello")
476476
def hello():
477477
logger.info("Request from unknown received")
478-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
478+
return {"message": "hello unknown!"}
479479

480480

481481
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
@@ -554,14 +554,14 @@ It's a [two-step process](https://docs.aws.amazon.com/lambda/latest/dg/services-
554554
@xray_recorder.capture('hello_name')
555555
def hello_name(name):
556556
logger.info(f"Request from {name} received")
557-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
557+
return {"message": f"hello {name}!"}
558558

559559

560560
@app.get("/hello")
561561
@xray_recorder.capture('hello')
562562
def hello():
563563
logger.info("Request from unknown received")
564-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
564+
return {"message": "hello unknown!"}
565565

566566

567567
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
@@ -572,13 +572,15 @@ It's a [two-step process](https://docs.aws.amazon.com/lambda/latest/dg/services-
572572

573573
=== "template.yaml"
574574

575-
```yaml hl_lines="14"
575+
```yaml hl_lines="7-8 14"
576576
AWSTemplateFormatVersion: "2010-09-09"
577577
Transform: AWS::Serverless-2016-10-31
578578
Description: Sample SAM Template for powertools-quickstart
579579
Globals:
580580
Function:
581581
Timeout: 3
582+
Api:
583+
TracingEnabled: true
582584
Resources:
583585
HelloWorldFunction:
584586
Type: AWS::Serverless::Function
@@ -613,7 +615,10 @@ Let's break it down:
613615
???+ question
614616
But how do I enable tracing for the Lambda function and what permissions do I need?
615617

616-
Within `template.yaml` on line 14, we added a new Serverless Function property: `Tracing: Active`. This will enable tracing for the [Lambda function resource](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-cloudformation){target="_blank"}, and add a managed IAM Policy named [AWSXRayDaemonWriteAccess](https://console.aws.amazon.com/iam/home#/policies/arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess){target="_blank"} to allow Lambda to send traces to AWS X-Ray.
618+
We've made the following changes in `template.yaml` for this to work seamless:
619+
620+
* **L7-8**: Enables tracing for Amazon API Gateway
621+
* **L14**: Enables tracing for our Serverless Function. This will also add a managed IAM Policy named [AWSXRayDaemonWriteAccess](https://console.aws.amazon.com/iam/home#/policies/arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess){target="_blank"} to allow Lambda to send traces to AWS X-Ray.
617622

618623
!!! danger "TODO: Revisit to see if it's still necessary"
619624

@@ -623,6 +628,7 @@ Within `template.yaml` on line 14, we added a new Serverless Function property:
623628

624629
### Enriching our generates traces
625630

631+
What we've done helps bring an initial visibility
626632

627633
cold start invocations
628634

@@ -704,15 +710,15 @@ Now, let's try to simplify it with Lambda Powertools:
704710
def hello_name(name):
705711
tracer.put_annotation("User", name)
706712
logger.info(f"Request from {name} received")
707-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
713+
return {"message": f"hello {name}!"}
708714

709715

710716
@app.get("/hello")
711717
@tracer.capture_method
712718
def hello():
713719
tracer.put_annotation("User", "unknown")
714720
logger.info("Request from unknown received")
715-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
721+
return {"message": "hello unknown!"}
716722

717723

718724
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
@@ -793,7 +799,7 @@ Let's expand our application with custom metrics without Powertools to see how i
793799
logger.info(f"Request from {name} received")
794800
put_metric_data(service=service, method="/hello/<name>")
795801
tracer.put_annotation("User", name)
796-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
802+
return {"message": f"hello {name}!"}
797803

798804

799805
@app.get("/hello")
@@ -802,7 +808,7 @@ Let's expand our application with custom metrics without Powertools to see how i
802808
tracer.put_annotation("User", "unknown")
803809
logger.info("Request from unknown received")
804810
put_metric_data(service=service, method="/hello")
805-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
811+
return {"message": "hello unknown!"}
806812

807813

808814
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
@@ -878,7 +884,7 @@ To add custom metric in **CloudWatch** we add the `boto3` cloudwatch client. Nex
878884
logger.info(f"Request from {name} received")
879885
metrics.add_dimension(name="method", value="/hello/<name>")
880886
metrics.add_metric(name="AppMethodsInvocations", unit=MetricUnit.Count, value=1)
881-
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
887+
return {"message": f"hello {name}!"}
882888

883889

884890
@app.get("/hello")
@@ -888,7 +894,7 @@ To add custom metric in **CloudWatch** we add the `boto3` cloudwatch client. Nex
888894
logger.info("Request from unknown received")
889895
metrics.add_dimension(name="method", value="/hello/<name>")
890896
metrics.add_metric(name="AppMethodsInvocations", unit=MetricUnit.Count, value=1)
891-
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
897+
return {"message": "hello unknown!"}
892898

893899

894900
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)

0 commit comments

Comments
 (0)