Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20
8 changes: 8 additions & 0 deletions challenge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions challenge/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
14 changes: 14 additions & 0 deletions challenge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

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

## Useful commands

* `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
10 changes: 10 additions & 0 deletions challenge/bin/challenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import "source-map-support/register";
import { env } from "../env";
import { ChallengeStack } from "../lib/challenge-stack";

const app = new cdk.App();
new ChallengeStack(app, "ChallengeStack", {
env,
});
57 changes: 57 additions & 0 deletions challenge/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"app": "npx ts-node --prefer-ts-exts bin/challenge.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
],
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
"@aws-cdk/aws-redshift:columnId": true,
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
"@aws-cdk/aws-kms:aliasNameRef": true,
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true
}
}
8 changes: 8 additions & 0 deletions challenge/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Environment } from "aws-cdk-lib";
import { parseEnv, z } from "znv";

export const env = parseEnv(process.env, {
AWS_REGION: z.string(),
}) as Environment;

export type ChallengeEnv = typeof env;
8 changes: 8 additions & 0 deletions challenge/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
31 changes: 31 additions & 0 deletions challenge/lib/challenge-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import { ApiGatewayRestApi } from "./infrastructure/api/api-gateway-rest-api";
import DynamoDBTables from "./infrastructure/tables/dynamodb-tables";
import PaymentsService from "./services/payments";
import { TransactionsService } from "./services/transactions";

export class ChallengeStack extends cdk.Stack {
readonly paymentService: PaymentsService;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const {
activity: activityTable,
transactions: transactionsTable,
users: usersTable,
} = new DynamoDBTables(this, "DynamoDBTables");

const { restApi } = new ApiGatewayRestApi(this, "RestApi");

this.paymentService = new PaymentsService(this, "PaymentsService", {
transactionsTable,
usersTable,
activityTable,
restApi,
});

new TransactionsService(this, "TransactionsService", { restApi, transactionsTable });
}
}
26 changes: 26 additions & 0 deletions challenge/lib/infrastructure/api/api-gateway-rest-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Cors, Deployment, RestApi } from "aws-cdk-lib/aws-apigateway";
import { Construct } from "constructs";

export class ApiGatewayRestApi extends Construct {
readonly restApi: RestApi;

constructor(scope: Construct, id: string) {
super(scope, id);

this.restApi = new RestApi(this, "RestApi", {
restApiName: "challenge-api",
description: "This is the API Gateway for the challenge",
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
allowMethods: Cors.ALL_METHODS,
allowHeaders: ["*"],
allowCredentials: true,
},
});

new Deployment(this, "Deployment", {
api: this.restApi,
description: "Deployment for the API Gateway",
});
}
}
30 changes: 30 additions & 0 deletions challenge/lib/infrastructure/tables/dynamodb-tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AttributeType, StreamViewType, Table } from "aws-cdk-lib/aws-dynamodb";
import { Construct } from "constructs";

class DynamoDBTables extends Construct {
readonly users: Table;
readonly transactions: Table;
readonly activity: Table;

constructor(scope: Construct, id: string) {
super(scope, id);

this.users = new Table(this, "users", {
tableName: "users",
partitionKey: { name: "userId", type: AttributeType.STRING },
});

this.transactions = new Table(this, "transactions", {
tableName: "transactions",
partitionKey: { name: "transactionId", type: AttributeType.STRING },
stream: StreamViewType.NEW_IMAGE,
});

this.activity = new Table(this, "activity", {
tableName: "activity",
partitionKey: { name: "activityId", type: AttributeType.STRING },
});
}
}

export default DynamoDBTables;
60 changes: 60 additions & 0 deletions challenge/lib/services/payments/constructs/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Duration } from "aws-cdk-lib";
import { Table } from "aws-cdk-lib/aws-dynamodb";
import { Architecture, StartingPosition } from "aws-cdk-lib/aws-lambda";
import { DynamoEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import path from "path";

interface PaymentsServiceFunctionsProps {
activityTable: Table;
transactionsTable: Table;
}

export class PaymentsServiceFunctions extends Construct {
readonly executePayments: NodejsFunction;
readonly registerActivity: NodejsFunction;

constructor(scope: Construct, id: string, props: PaymentsServiceFunctionsProps) {
super(scope, id);

this.executePayments = new NodejsFunction(this, "ExecutePayments", {
architecture: Architecture.ARM_64,
bundling: {
minify: true,
sourceMap: true,
externalModules: ["@aws-sdk/*"],
},
entry: path.join(__dirname, "../lambdas/execute-payments.ts"),
memorySize: 512,
timeout: Duration.seconds(30),
description: "Executes payment lambda",
});

this.registerActivity = new NodejsFunction(this, "RegisterActivity", {
architecture: Architecture.ARM_64,
bundling: {
minify: true,
sourceMap: true,
externalModules: ["@aws-sdk/*"],
},
entry: path.join(__dirname, "../lambdas/register-activity.ts"),
memorySize: 512,
timeout: Duration.seconds(30),
description: "Register activity lambda",
environment: {
ACTIVITY_TABLE_NAME: props.activityTable.tableName,
},
});

this.registerActivity.addEventSource(
new DynamoEventSource(props.transactionsTable, {
startingPosition: StartingPosition.LATEST,
batchSize: 1,
reportBatchItemFailures: true,
})
);
props.activityTable.grantWriteData(this.registerActivity);
props.transactionsTable.grantStreamRead(this.registerActivity);
}
}
Loading