Guide: Set Up Lambda APIs Using a CDK Template
• ByAWS Lambda is a great way to get an API up and running quickly. However, it can be unclear what the best strategy is for initially deploy and making changes to lambda code. This guide tries to simplify lambda management by using a AWS CDK template I created for deploying one for more APIs through AWS Lambda.
Steps
- Clone the template repository from https://github.com/azumbro/AWSLambdaAPICDKTemplate.
- Navigate to the cloned repository and install Node modules by running
npm install
. - In
lib/lambda_api-stack.js
, add the target AWS region and account ID to theACCOUNT_SETTINGS
object. - In
lib/lambda_api-stack.js
, add an entry toLAMBDA_FUNCTIONS
for each lambda function/API to create as part of the stack.- The
name
field specified the name of the lambda/API. - The
handler
field points to the handler file and function that will serve as the entry point to the lambda/API; these handles should be located in JS files within theresources
directory. For example, a value ofapi1_handler.main
would look for an exportedmain
function in theresources/api1_handler.js
file. - The
allowedOrigins
field specifies a list of domain origins that can call the lambda/API. An entry of “*” allows for all domains to call the lambda/API.
- The
- Write your lambda/API business logic in the handler functions. A basic example has been provided in
api1_handler.main
. - When ready to deploy your lambda/API, run
npm run cdk deploy
. This will create the relevant CFN stack and lambda functions in your AWS account. - Get the URL(s) for calling your lambda/API(s). These can be found in the AWS console by navigating the the lambda function list, selection a lambda function, and looking for the value under “Function URL” in the overview panel.
That’s it! If you make changes to your lambda/API code, re-running npm run cdk deploy
will deploy the changes to AWS. Running the command npm run cdk destroy
will delete the CFN stack and cleanup the associated AWS resources.
Feel free to reach out with any questions/comments at hello@zumbro.me.