Code Monkey home page Code Monkey logo

aws-cdk-items-app's Introduction

aws-cdk-items-app

This is a sample TypeScript CDK app to show how to set up AWS API Gateway JWT Authorizers using Keycloak as an OIDC provider.

The sample app comprises of the following components:

  • DynamoDB table items
  • Lambda function create-item - creates an item in DynamoDB table, parses JWT token from the request and uses email claim as a PK attribute (SK attribute is uuid)
  • Lambda function get-items - gets items from DynamoDB table, performs scan operation on the table
  • API Gateway v2 HTTP service with JWT authorizer
    • GET /items - invokes get-items lambda, publically available
    • POST /items - invokes create-items lambda, available to authorized users only

Setup

Make sure you have cdk installed and cdk has access to typescript and all nodejs packages are available:

# global setup
npm install -g cdk
npm install -g typescript
# project-specific setup
git clone [email protected]:lukaszbudnik/aws-cdk-items-app.git
cd aws-cdk-items-app
npm link typescript
npm install
# check if cdk can synthesize the stacks
cdk synth
# build create-item lambda - nothing special, but uses uuid to generate unique SK in DynamoDB
cd lib/lambda/create-item
npm install

Deploy

The cdk will deploy two stacks in your AWS account: Database and Service. cdk will take care of provisioning everything, including IAM policies.

In order to deploy JWT Authorizer the following two parameters are required and have to be valid: Service:jwtIssuer and Service:jwtAudience.

The JWT Authorizer is validated during the deploy and the deployment will fail if any of the parameters is missing or is invalid. If you don't have OIDC provider at hand, don't worry, I got you covered. See my video which shows how super easy it is to deploy Keycloak cluster to AWS EKS: https://youtu.be/BuNZ7bjbzOQ.

Once you have Keycloak (or any other OIDC provider), pass the issuer URL and audience parameters to the cdk deploy command:

cdk bootstrap
cdk deploy --all \
  --parameters Service:jwtIssuer=https://example.com/auth/realms/customer123 \
  --parameters Service:jwtAudience=account

Useful commands

The cdk.json file tells the CDK Toolkit how to execute your app.

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template
  • cdk destroy destroys this stack from your default AWS account/region

aws-cdk-items-app's People

Contributors

lukaszbudnik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.