A TypeScript AWS Lambda function for handling AWS Cognito user events. This lambda processes different types of Cognito triggers and generates custom email and SMS messages for user authentication flows.
- Prerequisites
- Installation
- Project Structure
- Local Development
- Testing
- Build & Deploy
- Configuration
- Architecture
- Contributing
Before setting up this project, ensure you have the following installed:
- Node.js: Version 22.x or later
- npm: Comes with Node.js
- Git: For cloning the repository
- AWS CLI: For deployment (optional, handled by GitHub Actions)
-
Clone the repository:
git clone https://github.com/Christian-Oleson/healio-user-lambda.git cd healio-user-lambda
-
Install dependencies:
npm install
healio-user-lambda/
├── interfaces/ # TypeScript interfaces
│ ├── cognitoEvent.ts # Cognito event type definitions
│ └── triggerStrategy.ts # Strategy interface
├── strategies/ # Event handling strategies
│ ├── adminCreateUserStrategy.ts
│ ├── forgotPasswordStrategy.ts
│ ├── preSignUpStrategy.ts
│ └── signUpStrategy.ts
├── __snapshots__/ # Test snapshots
├── scripts/ # Build and utility scripts
├── .github/workflows/ # GitHub Actions CI/CD
├── *.html # Email templates
├── index.ts # Main Lambda handler
├── config.ts # Configuration settings
├── emailTemplates.ts # Email template functions
├── test.ts # Local testing script
└── package.json # Project dependencies and scripts
-
Build the project:
npm run build
-
Run the test script:
npm start
This will compile TypeScript and run the local test script with sample Cognito events.
-
Run specific tests: You can modify the
testToRun
variable intest.ts
to test different trigger types:'signUp'
- CustomMessage_SignUp events'forgotPassword'
- CustomMessage_ForgotPassword events'adminCreateUser'
- CustomMessage_AdminCreateUser events'preSignUp'
- PreSignUp_SignUp events
- Make changes to TypeScript files
- Run
npm run build
to compile - Run
npm start
to test locally - Run
npm test
to execute the test suite - Commit and push changes (GitHub Actions will handle deployment)
npm test
This will run the Vitest test suite with coverage reporting.
- Unit tests: Located in
*.test.ts
files - Integration tests: The
test.ts
file provides integration testing with sample events - Coverage: Automatically generated with each test run
npm test
- Run tests with coveragenpm run build
- Compile TypeScriptnpm start
- Build and run local test script
npm run build
The project uses GitHub Actions for automated deployment:
-
Triggers: Pushes to
main
branch and pull requests -
Process:
- Install dependencies with
npm ci
- Build TypeScript with
npm run build
- Create deployment package with production dependencies
- Create zip file:
Healiospace.UserLambda.zip
- Upload to AWS S3
- Update Lambda function code
- Install dependencies with
-
Requirements for deployment:
- AWS credentials configured in GitHub Secrets
- Access to the specified S3 bucket and Lambda function
To build and deploy manually:
# Build the project
npm run build
# Create deployment structure
mkdir -p deployment
cp -r dist/* deployment/
cp package.json deployment/
cd deployment && npm install --omit=dev && cd ..
# Create deployment package
cd deployment && zip -r ../Healiospace.UserLambda.zip .
# Upload to AWS (requires AWS CLI configured)
aws s3 cp "./Healiospace.UserLambda.zip" "s3://infrastack-hsapibucketb0ffce48-q7fxihjt4x6e/lambda/"
aws lambda update-function-code --function-name "InfraStack-hsuserlambda7402A172-AQlkiNzXUDR2" --s3-bucket "infrastack-hsapibucketb0ffce48-q7fxihjt4x6e" --s3-key "lambda/Healiospace.UserLambda.zip"
The lambda uses the following configuration:
- Sentry DSN: Configured in
index.ts
for error tracking - AWS Region:
us-east-2
(configured in deployment workflow)
- S3 Bucket:
infrastack-hsapibucketb0ffce48-q7fxihjt4x6e
- Lambda Function:
InfraStack-hsuserlambda7402A172-AQlkiNzXUDR2
- Region:
us-east-2
The lambda uses the Strategy pattern to handle different Cognito trigger types:
- TriggerStrategyFactory: Routes events to appropriate strategies
- Strategy Classes: Each handles a specific trigger type
- Event Processing: Generates custom messages based on event type
CustomMessage_SignUp
: Welcome messages for new user registrationCustomMessage_ForgotPassword
: Password reset messagesCustomMessage_AdminCreateUser
: Admin-created user welcome messagesPreSignUp_SignUp
: Pre-signup processing (validation, auto-confirmation)
- HTML templates with responsive design
- Branded with Healiospace styling
- Dynamic content based on user attributes and codes
- Fork the repository
- Create a feature branch
- Make changes and add tests
- Run the test suite:
npm test
- Build and test locally:
npm start
- Submit a pull request
- TypeScript with strict type checking
- ES modules (type: "module")
- Prettier for code formatting
- Husky for git hooks