You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/cdk/README.md
+63-7Lines changed: 63 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,16 @@
2
2
3
3
This is a deployable CDK app that deploys AWS Lambda functions as part of a CloudFormation stack. These Lambda functions use the utilities made available as part of AWS Lambda Powertools for TypeScript to demonstrate their usage.
4
4
5
-
You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. The cost from **some services** are covered by the [AWS Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) but not all of them. If you don't have an AWS Account follow [these instructions to create one](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).
5
+
> **Note**
6
+
> You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. The cost from **some services** are covered by the [AWS Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) but not all of them. If you don't have an AWS Account follow [these instructions to create one](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).
6
7
7
-
The example functions, located in the `src` folder, are invoked automatically, twice, when deployed using the CDK construct defined in `src/example-function.ts`. The first invocation demonstrates the effect on logs/metrics/annotations when the Lambda function has a cold start, and the latter without a cold start.
8
+
The example functions, located in the `functions` folder, are frontend by a REST API that is deployed using AWS API Gateway.
9
+
10
+
The API has three endpoints:
11
+
12
+
*`POST /` - Adds an item to the DynamoDB table
13
+
*`GET /` - Retrieves all items from the DynamoDB table
14
+
*`GET /{id}` - Retrieves a specific item from the DynamoDB table
8
15
9
16
## Deploying the stack
10
17
@@ -14,10 +21,59 @@ The example functions, located in the `src` folder, are invoked automatically, t
14
21
15
22
Note: Prior to deploying you may need to run `cdk bootstrap aws://<YOU_AWS_ACCOUNT_ID>/<AWS_REGION> --profile <YOUR_AWS_PROFILE>` if you have not already bootstrapped your account for CDK.
16
23
17
-
## Viewing Utility Outputs
24
+
> **Note**
25
+
> You can find your API Gateway Endpoint URL in the output values displayed after deployment.
26
+
27
+
## Execute the functions via API Gateway
28
+
29
+
Use the API Gateway Endpoint URL from the output values to execute the functions. First, let's add two items to the DynamoDB Table by running:
30
+
31
+
```bash
32
+
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myfirstitem","name":"Some Name for the first item"}' https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/
33
+
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myseconditem","name":"Some Name for the second item"}' https://randomid1245.execute-api.eu-central-1.amazonaws.com/prod/
By having structured logs like this, we can easily search and analyse them in [CloudWatch Logs Insight](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html). Run the following query to get all messages for a specific `awsRequestId`:
As we have enabled tracing for our Lambda-Funtions, you can visit [AWS CloudWatch Console](https://console.aws.amazon.com/cloudwatch/) and see [Traces](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces) and a [Service Map](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-using-xray-maps.html) for our application.
72
+
73
+
## Cleanup
18
74
19
-
All utility outputs can be viewed in the Amazon CloudWatch console.
75
+
To delete the sample application that you created, run the command below while in the `examples/sam` directory:
20
76
21
-
*`Logger` output can be found in Logs > Log groups
22
-
*`Metrics` output can be found in Metrics > All metrics > CdkExample
23
-
*`Tracer` output can be found in X-Ray traces > Traces
// Use the middleware by passing the Tracer instance as a parameter
98
+
.use(captureLambdaHandler(tracer,{captureResponse: false}));// by default the tracer would add the response as metadata on the segment, but there is a chance to hit the 64kb segment size limit. Therefore set captureResponse: false
* @returns {Promise<APIGatewayProxyResult>} object - API Gateway Lambda Proxy Output Format
21
+
*
22
+
*/
23
+
24
+
classLambdaimplementsLambdaInterface{
25
+
26
+
@tracer.captureMethod()
27
+
publicasyncgetUuid(): Promise<string>{
28
+
// Request a sample random uuid from a webservice
29
+
constres=awaitrequest<{uuid: string}>({
30
+
url: 'https://httpbin.org/uuid',
31
+
parse: 'json',
32
+
});
33
+
const{ uuid }=res.body;
34
+
35
+
returnuuid;
36
+
}
37
+
38
+
@tracer.captureLambdaHandler({captureResponse: false})// by default the tracer would add the response as metadata on the segment, but there is a chance to hit the 64kb segment size limit. Therefore set captureResponse: false
0 commit comments