Skip to content

HandshakesByDC/cdktf-ts-assignment

Repository files navigation

LocalStack Image Resizer Assignment

Important

This quickstart has been tested on Darwin/Linux only - recommended to use WSL if on Windows.

In this assignment we will focus on:

  • AWS Services (without requiring an AWS Account)
  • CDK for Terraform (Typescript) (in future: CDK Terrain)
  • GitHub Workflow Integration test

Overview

The task is to implement this Localstack: Quickstart in CDKTF!

Note

Handshakes is adopting CDK Terrain for longterm support since Hashicorp has sunset CDKTF.

Architecture

Pre-req

Please make sure that you have the following working on your machine:

  • AWS cli only (we will use localstack so no aws credentials required)

  • NodeJS installation - we use mise

    mise trust .
    # installs node, terraform, localstack, terraform-local, awscli-local, ...
    mise install
    
    # NodeJS shims for pnpm/yarn using corepack
    corepack enable
    pnpm install
  • Docker environment - we use colima on MacOS - not in repo dependencies, may use global install:

    mise use -g colima@latest
  • Terraform (i.e. managed through mise)

    # should show terraform version installed by mise from step above
    mise ls
  • standard utilities jq, zip & curl

See also this CDKTF getting started guides:

Set up

Pre-Req (automated with mise install above)

node --version        # 20.x pinned
terraform --version   # 1.7.5 pinned
localstack --version  # 4.12.0 pinned
tflocal --version     # localstack wrapper for terraform
awslocal --version    # localstack wrapper for aws cli

docker version        # colima glabal install

Ensure all required dependencies are installed

pnpm i

Explore Env

pnpm localstack:start

Note: To see SNS Emails, should be able to use DATA_DIR see: issue comment.

Assignment

This repo has been modified to be deployed using CDKTF IaC. The ultimate goal is to make the GH Workflow pass.

The exact shell commands to achieve a working configuration are in deploy.sh

However, you may only run cdktf commands in GH Runner to achieve the working configuration.

Important

Fix the ./main.ts file to implement the following TODOs across both stacks.

Backend stack

Backend Overview

  • images and resized S3 buckets
  • list, presign and resize Lambda Functions
  • S3 bucket event trigger for resize Lambda Function

Backend Todo

Note

Ask the AI Agent to write Unit Tests first before implementing as fast feedback loop (TDD).

  • Create S3 buckets
  • Create SSM Paramaters for Lambda handlers to work
  • Create SNS Topic to receive dead letter notifications when resize lambda fails
  • Create resize lambda
  • Create images bucket event trigger for resize lambda

Backend deploy

pnpm synth
TERRAFORM_BINARY_NAME=tflocal pnpm exec cdktf apply iac-assignment-backend

You may use --skip-synth to speed up apply.

Frontend stack

Frontend Overview

  • webapp S3 bucket
  • .env.local file with Env config

Frontend Todo

  • Create S3 bucket for webapp
  • Create webapp S3 Bucket website configuration and s3 policy

Frontend Deploy

TERRAFORM_BINARY_NAME=tflocal pnpx cdktf apply iac-assignment-frontend

Copy website to webapp S3 Bucket (uses website/.env.local config from cdktf apply):

pnpm --prefix website install
pnpm --prefix website run deploy

Integration test

  1. Run integration tests:

    pnpm test:integ
  2. Verify both tests pass:

    • Image resize test uploads and verifies smaller resized image
    • Failure test triggers error email via SNS/SES

Note: The GitHub Actions workflow won't run tests successfully until the CDKTF deployment TODOs are implemented.

Testing Todo

Make the GitHub Workflow pass

  • Set up CDKTF cli in GitHub Runner
  • Deploy Backend with CDKTF
  • Deploy Frontend with CDKTF

PR Result

Testing Bonus points

Terraform Unit Test docs

  • Implement CDKTF Unit tests in main.test.ts
  • Implement Unit tests for list, presign and resize functions
  • Refactor IaC to split backend and frontend stacks in separate code files
  • Run Unit tests in pre-commit

Debug Tips

LocalStack debug logs show lambda invocations and logs:

localstack logs -f

To check resize dead letter notifications:

curl -s http://localhost:4566/_aws/ses | jq -r '.messages[0] | {Destination, "Message": (.Body.text_part | fromjson)}' 

About

Take home assignment for CDKTF using TypeScript

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors