- A very simple application in typescript and golang that demonstrates a backend using the following components from AWS
- 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 |
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....
- 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
andCDK_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.
- Important: This application refers to env vars from cdk
- You have docker installed, up and running. (
cdk synth
andcdk 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 asexport POOR_MANS_API_KEY=<YOUUR_RANDOM_STRING>
, more detail of this implementation inlib/lambda/assets/utils.ts
andlib/lambda/application-lambdas.ts
. If this is configured incorrectly your APIs will return a HTTP Status 400.
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!
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 cURL
er, skip to next section 😉
- Download Postman.
- Get the API ID from API Gateway.
- Navigate to your API Gateway console. For example if you deployed to
us-west-2
, you would go to https://us-west-2.console.aws.amazon.com/apigateway/main/apis?region=us-west-2. - Find the API with name
Application Service
, copy down theID
.
- Navigate to your API Gateway console. For example if you deployed to
- 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 theID
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.
- Replace
Note: You need to replace YOUR_API_ID
, your-aws-region
, POOR_MANS_API_KEY
and {MessageId}
for cURL
s.
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"
}'
curl --location --request GET 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages/{MessageId}?validationKey=POOR_MANS_API_KEY'
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"
}'
curl --location --request DELETE 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/messages/{MessageId}?validationKey=POOR_MANS_API_KEY'
curl --location --request GET 'https://YOUR_API_ID.execute-api.your-aws-region.amazonaws.com/prod/ping'
- 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
andCodePipeline