Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cdk-sample

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cdk-sample

This project contains source code and supporting files for a serverless application that you can deploy with CDK.

  • 1.6.0
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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/sam directory:

cdk delete

FAQs

Package last updated on 02 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc