How to use Amplify to Leverage AWS CDK with Cognito, Lambda, and API Gateway — Part 3

Amchelle C.
2 min readJun 20, 2021

This article is part of a series, check out Part 1 and Part 2 to set up your environment for CDK.

Alright, so far we have our CDK project set up and our Cognito User Pool for authentication. Next step is creating a Lambda function that will be integrated with our API Gateway REST API.

We will create a basic Lambda function that can be used as a template for more advanced scenarios.

First, create a directory called “lambda” in the root of your project next to “bin” and “lib”. Then create a file called “lambda/example.js”.

Inside the file add the following code:

This Lambda function will return “Way to go! You are all set to start your project!”. I am also including an HTTP status code and HTTP headers that will be used by API Gateway to create the HTTP response when triggered by a call to the REST API.

We now need to add this Lambda function to our stack and create an AWS Lambda resource.

Go to the file “lib/example-app.ts” or the main file of your project where the code for the Cognito User Pool was implemented.

npm install @aws-cdk/aws-lambda

At the top of the file add an import statement:

import * as lambda from '@aws-cdk/aws-lambda';

Next, define an AWS Lambda resource with the following code:

You can take a look at the AWS CDK README to further customize your lambda function.

We are now ready to run cdk diff and then cdk deploy if we are happy with the changes.

Once the deploy is successful, we can check that the Lambda function has been deployed properly by going to our AWS Management Console and searching for “Lambda”. Your Lambda function name should be displayed along with its specifications.

In part 4 we will create a REST API in API Gateway that will be integrated with our newly created Lambda function.

--

--