Skip to content

A very simple application to demo APIG, Lambda Proxy Integration and DDB Crud.

License

Notifications You must be signed in to change notification settings

jmiumv/AwsCdkApigLambdaDdbCrudExample

Repository files navigation

AwsCdkApiLambdaDdbCrudExample

What is this application about ?


What are we deploying ?

  • A simple backend, where we set up an API (non-Auth) that helps you interact with a message store based on DynamoDb as seen below through REST.
MessageId (String) Message (String)
MessageId1 Message-1
MessageId2 Message-2
MessageIdn Message-n

Instructions

Note

I highly encourage you to read and understand the code first to see what is happening under the hood. I'm also assuming that you did your due diligence in understanding the costs involved for setting up this infrastructure.

That said lets get going....

Pre-Requisites

  • You have your favorite IDE installed for exploring code. (cough IntelliJ or VSCode)
  • You have node and npm installed. I highly encourage using nvm for easy management of node versions.
  • You have installed and configured aws-cdk. If not you can find the configuration instructions here.
    • Important: This application refers to env vars from cdk CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION. More information on this can be found here.
    • If you don't want the above env vars to be referred, replace accordingly in bin/index.ts.
    • The AWS profile you would use for deployment has necessary IAM permissions to create, modify or even delete AWS resources.
  • You have docker installed, up and running. (cdk synth and cdk deploy spin up a docker container to synthesize the application template and build your lambdas for deployment)
  • Configure an env var POOR_MANS_API_KEY with a random string, IRL you would use API gateway's API keys combined with Sigv4 or Cognito for AuthZ to protect your API (YMMV), but to keep this example simple and prevent abuse, we do this. Alternatively you can also configure your shell with an export such as export POOR_MANS_API_KEY=<YOUUR_RANDOM_STRING>, more detail of this implementation in lib/lambda/assets/utils.ts and lib/lambda/application-lambdas.ts. If this is configured incorrectly your APIs will return a HTTP Status 400.

Steps

Navigate to the workspace you cloned this project to and do the following:

  • cdk bootstrap aws://<ACCOUNT NUMBER>/CDK_DEFAULT_REGION/YOUR_CONFIGURED_REGION>
  • cdk synth
  • cdk deploy

If everything went through fine, you will see a CLI output from cdk deploy something similar to below once its done deploying.

 ✅  ApplicationStack
✨  Deployment time: 75.08s

That's it....😊! Congrats!

Testing Your API With Postman

You can use cURL to test your API as well, but just for context and convenience, I prefer postman as its easy to share with the exported file as you will see below. If you are cURLer, skip to next section 😉

  • Download Postman.
  • Get the API ID from API Gateway.
  • Import this file into your postman application and alter the below for each URL (OR) alternatively edit the file prior to importing.
    • Replace YOUR_API_ID with the ID you copied from above.
    • Replace your-aws-region with the aws region your application has been deployed to.
    • Replace POOR_MANS_API_KEY with the key you used from pre-requisites.
    • Note: You will need to clear out some UUIDs (for get/update/delete) which will not apply to you as they are random. Replace them with your MessageId's accordingly.

Testing Your API with cURL

Note: You need to replace YOUR_API_ID, your-aws-region, POOR_MANS_API_KEY and {MessageId} for cURLs.

POST (Create)

curl --location --request POST 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages?validationKey=POOR_MANS_API_KEY' \
--header 'Content-Type: text/plain' \
--data-raw '{
    "Message": "Hello from the other side - Adele"
}'

GET (Read)

curl --location --request GET 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages/{MessageId}?validationKey=POOR_MANS_API_KEY'

PUT (Update)

curl --location --request PUT 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages?validationKey=POOR_MANS_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "MessageId": "27d2fe03-2d24-48fa-a80c-434af7deba2c",
    "Message": "Update from post master"
}'

DELETE (Delete)

curl --location --request DELETE 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages/{MessageId}?validationKey=POOR_MANS_API_KEY'

GET (HealthCheck)

curl --location --request GET 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/ping'

To-Do (PRs welcome!!!)

  • More updated to readme with dir structure and explanations.
  • Build instructions for interested readers.
  • Write unit tests for CDK and lambdas (typescript and golang)
  • Setup CD Pipeline with CodeCommit and CodePipeline

About

A very simple application to demo APIG, Lambda Proxy Integration and DDB Crud.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published