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

Amchelle C.
2 min readFeb 12, 2021

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

So in Part 1 we created an AWS account and configured our environment. We also created a CDK project to begin writing code for deployment. In this part of the series we will create a Cognito User Pool that we will use for authentication.

If you explore your project directory you will see a lib directory. Inside of that you will find the name of your CDK project (in my case from Part 1 it is lib/example-app.ts). This is where we will be writing our code for deployment.

You will see some default sample code. We won’t be using most of this so strip it back to look like below:

Make sure that you install the following before we create our Cognito User Pool:

npm install @aws-cdk/core
npm install @aws-cdk/aws-cognito

Creating your Cognito User Pool depends on your own project requirements. This link will help you tailor it to your needs:

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cognito-readme.html

I have created a basic Cognito User Pool below in the CDK, it includes some general requirements. From the link above you can make your User Pool exactly how you need it.

cdk.CfnOutput will print out the values of the clientId and userID created which will be helpful to have readily available once we integrate this with our frontend.

Now we are ready to deploy. If you run cdk diff in your console, you will be able to see the difference between your CDK app and what is deployed to your AWS account. This is good practice to make sure you are happy with what you are about to deploy.

cdk diff

If all looks good, next run cdk deploy to deploy to your AWS account.

cdk deploy

Once the command has finished running, you should see a printout of your ‘UserPoolId’ and ‘UserPoolClientId’.

You can now check that everything has been deployed properly by going to your AWS Management Console: https://aws.amazon.com/console/.

Search for Cognito and then click ‘Manage User Pools’

You should see your newly created User Pool and can verify the accuracy of the general settings.

Once we create our REST API with the CDK, we will be able to control access to it by having the Cognito User Pool as an authorizer.

In part 3 we will create a Lambda function that is integrated with our API Gateway REST API

--

--