1.7.0 • Published 1 year ago

cdk-sample v1.7.0

Weekly downloads
-
License
MIT-0
Repository
-
Last release
1 year ago

AWS Lambda Powertools for TypeScript examples in CDK

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.

Note 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 but not all of them. If you don't have an AWS Account follow these instructions to create one.

The example functions, located in the functions folder, are frontend by a REST API that is deployed using AWS API Gateway.

The API has three endpoints:

  • POST / - Adds an item to the DynamoDB table
  • GET / - Retrieves all items from the DynamoDB table
  • GET /{id} - Retrieves a specific item from the DynamoDB table

Deploying the stack

  • Navigate to this location of the repo in your terminal (examples/cdk)
  • npm ci
  • npm run cdk deploy --all --profile <YOUR_AWS_PROFILE>

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.

Note You can find your API Gateway Endpoint URL in the output values displayed after deployment.

Execute the functions via API Gateway

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:

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/
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/

Now, let's retrieve all items by running:

curl -XGET https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/

And finally, let's retrieve a specific item by running:

curl -XGET https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/myseconditem/

Observe the outputs in AWS CloudWatch & X-Ray

CloudWatch

If we check the logs in CloudWatch, we can see that the logs are structured like this

2022-04-26T17:00:23.808Z	e8a51294-6c6a-414c-9777-6b0f24d8739b	DEBUG	
{
    "level": "DEBUG",
    "message": "retrieved items: 0",
    "service": "getAllItems",
    "timestamp": "2022-04-26T17:00:23.808Z",
    "awsRequestId": "e8a51294-6c6a-414c-9777-6b0f24d8739b"
}

By having structured logs like this, we can easily search and analyse them in CloudWatch Logs Insight. Run the following query to get all messages for a specific awsRequestId:

filter awsRequestId="bcd50969-3a55-49b6-a997-91798b3f133a"
 | fields timestamp, message

AWS X-Ray

As we have enabled tracing for our Lambda-Funtions, you can visit AWS CloudWatch Console and see Traces and a Service Map for our application.

Cleanup

To delete the sample application that you created, run the command below while in the examples/cdk directory:

cdk destroy