LIST

Typescript: Catch error types

result.body = error.message compile error, because the type of the object ’error’ is unknown result.body = (error as AWSError).message force casting hides the error, but gives no guarantee that message actually exists! there is no guarantee that error is of type AWSError and actually has that field! And non of the ‘as’ casting remains at runtime So it will just act like message = ‘undefined’ if it is not there...

September 12, 2022

Typescript: Type Guards

type information is only available at compile time, not at runtime Type guards are ways to check at runtime if an object matches. use of keywords typeof instanceof in instanceof if(x instanceof SomeObjectConstructor) typeof if(typeof a === 'string') typeof only works with “string” | “number” | “bigint” | “boolean” | “symbol” | “undefined” | “object” | “function” in if('firstName' in obj) checks if attribute exists (src: TypeScript Type Guards, AWS & Typescript Masterclass - Typescript recap)

September 12, 2022

AWS & Typescript Masterclass - 2. CDK & CloudFormation

(6) intro (7) CDK (8) CloudFormation (9) install CDK (10) base project deployment (11) project exploration (12) CDK types and commands (13) CDK Outputs (cf terraform output) (14) CDK Deployment Parameters (15) CDK core - recap (6) intro (7) CDK abstraction of aws resources reusable components use AWS CDK to: create and deploy AWS resources configure those resources link together resources into constructs uses JSII (javascript interop interface) (8) CloudFormation...

September 9, 2022

AWS & Typescript Masterclass - 3. Serverless project

API gateway Lambda DynamoDb Cognito https://github.com/barosanuemailtest/space-finder-backend.git https://github.com/barosanuemailtest/space-finder-frontend.git git init npm init -y npm i -D aws-cdk aws-cdk-lib constructs ts-node typescript mkdir infra touch infra/Launcher.ts touch infra/SpaceStack.ts # echo '{"app":"npx infra/Launcher.ts"}' > cdk.json echo '{"app": "npx ts-node --prefer-ts-exts infra/Launcher.ts"}' > cdk.json cdk synth # error related to tsconfig target npx tsc --init # change tsconfig.json "target": "es2016", -> "target": "ES2018", # copy tsconfig.json from generated project infra/Launcher.ts import {App} from "aws-cdk-lib"; import {SpaceStack} from "....

September 9, 2022

AWS & Typescript Masterclass - 4. Lambda - bundling, testing and debugging

(23) Section Intro (24) Why Bundling? (25) Bundling with CDK Node Lambda (26)Webpack intro (27)Webpack setup (23) Section Intro (24) Why Bundling? options deploy all node_modules NOPE Node Lambda - with esbuild (integrated with CDK) YES webpack (hard to configure) NOPE why? growing list of dependencies typescript needs compilation to node_js (25) Bundling with CDK Node Lambda integrated with CDK uses esbuild npm install --save-dev esbuild@0 example dependency npm i uuid @types/uuid services/node-lambda/hello....

September 9, 2022

AWS & Typescript Masterclass - 5. Testing and debugging Lambdas

(28) section intro (29) CloudWatch logs (30) using AWS SDK (30) using AWS SDK npm i aws-sdk services/node-lambda/hello.ts import {S3} from "aws-sdk"; const s3Client = new S3() ... const buckets = await s3Client.listBuckets().promise(); ... body: 'here are your buckets' + JSON.stringify(buckets.Buckets) infra/SpaceStack.ts const s3PolicyStatement = new PolicyStatement(); s3PolicyStatement.addActions('s3:ListAllMyBuckets'); s3PolicyStatement.addResources('*'); // anti-pattern ; use specific! helloLambdaNodeJs.addToRolePolicy(s3PolicyStatement); (31) run lambda locally in debug mode run config (in VSCode) { "version": "0.2.0", "configurations":[ { "type": "node" "request": "launch" "name":"Debug local file", "runtimeArgs":["-r", "ts-node/register"] "args":"${relativeFile}" "env":{"AWS_REGION":"eu-west-2"} } ] } (you can add AWS credentials to env if you’re not logged in locally)...

September 9, 2022

Debug typescript file with breakpoints (credentials via 1password)

because: aws credentials - don’t store plaintext op run -- node inspect -r ts-node/register hello.test.ts (src: Debugger _ Node.js v18.9.0 Documentation & AWS & Typescript Masterclass - Testing and debugging Lambdas #31) From the terminal itself (src: How To Debug Node.js with the Built-In Debugger and Chrome DevTools _ DigitalOcean) from intellij (Run_Debug Configuration_ Attach to Node.js_Chrome _ IntelliJ IDEA) inspired by the AWS Toolkit for IntelliJ...

September 9, 2022

Course: AWS & Typescript Masterclass - CDK, Serverless, React

(AWS & Typescript Masterclass - CDK, Serverless, React) Section 1: Introduction Section 2: AWS CDK & CloudFormation CDK commands Backend Section 3: Serverless project with CDK and Typescript Section 4: Serverless: AWS Lambda - bundling, testing and debugging Section 5: Testing and debugging Lambdas cdk run local lambda with sam-cli Debug typescript file with breakpoints (credentials via 1password) Section 6: AWS DynamoDb with CDK and Lambda Type Guards Section 7: [[aws-typescript-masterclass-7....

September 7, 2022