/cdk-cognito-idp

Primary LanguageTypeScriptMIT No AttributionMIT-0

A web site and REST API with Cognito authentication (user pools) using the Facebook identity provider

The example will show you how to create the following:

  • A single-page app hosted by S3 and CloudFront
  • A REST API that uses Cognito for authentication
  • Integration of Facebook as an identity provider

It also demonstrates a somewhat opinionated way to organize your lambda functions and test them.

Prerequisites

There is a bit of setup required before you can deploy this stack.

Environment Variables

Create file called config/env-local.json using the following json as a template. Replace those values with your own. (Some of the values, starting with cognitoPoolId, are only used for integration testing and can be left blank until after your first deployment)

{ 
    "env": {
        "account": "012345678901",
        "region": "us-east-1"
    }, 
    "webDomainName": "www.example.com",
    "webCertificateArn": "arn:aws:acm:us-east-1:0123456789012:certificate/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
    "facebookSecretArn": "arn:aws:secretsmanager:us-east-1:012345678901:secret:facebook_app_secret-Abcdef",
    "facebookAppId": "111111111111111",
    "facebookApiVersion": "v7.0",
    "apiDomainName": "api.example.com",
    "apiCertificateArn": "arn:aws:acm:us-east-1:0123456789012:certificate/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
    "cognitoRedirectUri": "https://www.example.com", 


    "cognitoPoolId": "us-east-1_0123456789", 
    "cognitoDomainPrefix": "www-example-com", 
    "cognitoAppClientId": "A1A1A1A1A1A1A1A1A1A1A1A1A1", 
    "cognitoRegion:": "us-east-1", 
    "userTable": "CognitoIdpStack-UsersTableAAAAAAAA-000000000000", 
    "jwt": ""
}

Build and Deploy

# Install lambda dependencies
cd lambda
npm install

cd ..
npm install
npm run build
npm run unit-test
npm run deploy

# Get values from the deployment output to complete env-local.json.
# Then you can run integration tests.
node build/test/create-admin-user.js
# After that, manually add is_super_admin=true to the record in DynamoDB
npm run database-test
npm run handler-test
npm run api-test