Skip to content

Commit

Permalink
feat(esm): updating terraform modules to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed Jan 3, 2025
1 parent c3d7783 commit 1c44a54
Show file tree
Hide file tree
Showing 66 changed files with 2,810 additions and 2,629 deletions.
2 changes: 1 addition & 1 deletion infrastructure/otel-collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "otel-collector-cdk",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "dist/main.js",
"types": "src/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/parser-graphql-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "src/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/pocket-event-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/push-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "push-server-cdk",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "dist/main.js",
"types": "src/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
1 change: 1 addition & 0 deletions infrastructure/push-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class PushServer extends TerraformStack {
];

return new PocketECSApplication(this, 'application', {
shortName: 'push',
tags: config.tags,
prefix: config.prefix,
containerConfigs: [
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/sendgrid-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "src/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/shareable-lists-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/shared-snowplow-consumer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/shares-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/transactional-emails/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/user-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "user-api-cdk",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "dist/src/main.js",
"types": "src/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/v3-proxy-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"license": "MPL-2.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.ts",
"type": "module",
"scripts": {
"build": "rm -rf dist && tsc",
"compile": "tsc --pretty",
Expand Down
2 changes: 1 addition & 1 deletion packages/event-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"package.json"
],
"scripts": {
"prebuild": "pnpm ts-json-schema-generator --additional-properties --validation-keywords removeEmptyObject --path './src/events/types/index.ts' > ./src/events/generated/schema.json",
"build": "tsup src/index.ts --format cjs,esm --dts",
"dev": "pnpm run build --watch",
"format": "eslint --fix",
"lint": "eslint --fix-dry-run",
"prebuild": "pnpm ts-json-schema-generator --additional-properties --validation-keywords removeEmptyObject --path './src/events/types/index.ts' > ./src/events/generated/schema.json",
"presemantic-release": "pnpm run build",
"semantic-release": "semantic-release",
"test": "jest",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest/presets/default-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
Expand All @@ -9,6 +11,8 @@ module.exports = {
clearMocks: true,
restoreMocks: true,
coverageProvider: 'v8',
setupFilesAfterEnv: ['./setup.js'],
setupFilesAfterEnv: ['./jest.setup.ts'],
workerIdleMemoryLimit: 0.5,
};

export default config;
2 changes: 2 additions & 0 deletions packages/terraform-modules/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as cdktf from 'cdktf'
cdktf.Testing.setupJest();
3 changes: 3 additions & 0 deletions packages/terraform-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"url": "git+https://github.com/Pocket/pocket-monorepo.git"
},
"license": "MPL-2.0",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -85,13 +86,15 @@
"cdktf": "0.20.10",
"cdktf-cli": "0.20.10",
"constructs": "10.4.2",
"lodash": "4.17.21",
"parse-domain": "5.0.0",
"tslib": "2.8.0"
},
"devDependencies": {
"@pocket-tools/eslint-config": "workspace:*",
"@semantic-release/exec": "6.0.3",
"@types/jest": "29.5.14",
"@types/lodash": "4.17.13",
"@types/node": "^22.8.2",
"conventional-changelog-conventionalcommits": "8.0.0",
"jest": "29.7.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/terraform-modules/setup.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TestResource } from '../testHelpers.ts';
import {
ApplicationAutoscaling,
ApplicationAutoscalingProps,
} from './ApplicationAutoscaling';
} from './ApplicationAutoscaling.ts';

describe('ApplicationAutoscaling', () => {
const props: ApplicationAutoscalingProps = {
Expand Down
24 changes: 18 additions & 6 deletions packages/terraform-modules/src/base/ApplicationAutoscaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ import {
} from '@cdktf/provider-aws';
import { Construct } from 'constructs';

export interface ApplicationAutoscalingProps extends TerraformMetaArguments {
interface ApplicationAutoscalingPropsBase extends TerraformMetaArguments {
ecsClusterName: string;
ecsServiceName: string;
prefix: string;
scalableDimension: string;
scaleInThreshold: number;
scaleOutThreshold: number;
stepScaleInAdjustment: number;
stepScaleOutAdjustment: number;
tags?: { [key: string]: string };
targetMaxCapacity: number;
targetMinCapacity: number;
}

interface ApplicationAutoscalingOut extends ApplicationAutoscalingPropsBase {
stepScaleOutAdjustment: number;
}

interface ApplicationAutoscalingIn extends ApplicationAutoscalingPropsBase {
stepScaleInAdjustment: number;
}

export type ApplicationAutoscalingProps =
| ApplicationAutoscalingOut
| ApplicationAutoscalingIn;

/*
* Generates an AutoScaling group
*/
Expand Down Expand Up @@ -115,22 +125,24 @@ export class ApplicationAutoscaling extends Construct {
target: appautoscalingTarget.AppautoscalingTarget,
type: 'In' | 'Out',
): appautoscalingPolicy.AppautoscalingPolicy {
let stepAdjustment;
let stepAdjustment: appautoscalingPolicy.AppautoscalingPolicyStepScalingPolicyConfigurationStepAdjustment[];

if (type === 'In') {
if (type === 'In' && 'stepScaleInAdjustment' in config) {
stepAdjustment = [
{
metricIntervalUpperBound: '0',
scalingAdjustment: config.stepScaleInAdjustment,
},
];
} else {
} else if (type === 'Out' && 'stepScaleOutAdjustment' in config) {
stepAdjustment = [
{
metricIntervalLowerBound: '0',
scalingAdjustment: config.stepScaleOutAdjustment,
},
];
} else {
throw new Error('Step scaling adjustment is required');
}

const appAutoscaling = new appautoscalingPolicy.AppautoscalingPolicy(
Expand Down
4 changes: 4 additions & 0 deletions packages/terraform-modules/src/base/ApplicationCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class ApplicationCertificate extends Construct {
config.provider,
);

if (config.zoneId === undefined) {
throw new Error('No zone id provided or found');
}

const certificateRecord = ApplicationCertificate.generateRoute53Record(
this,
config.zoneId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ApplicationDynamoDBProps,
ApplicationDynamoDBTableCapacityMode,
ApplicationDynamoDBTableStreamViewType,
} from './ApplicationDynamoDBTable';
} from './ApplicationDynamoDBTable.ts';

describe('ApplicationDynamoDBTable', () => {
let BASE_CONFIG: ApplicationDynamoDBProps;
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('ApplicationDynamoDBTable', () => {
);

expect(
applicationDynamoDBTable.dynamodb.lifecycle.preventDestroy,
applicationDynamoDBTable.dynamodb.lifecycle?.preventDestroy,
).toEqual(false);
});
expect(synthed).toMatchSnapshot();
Expand Down
21 changes: 13 additions & 8 deletions packages/terraform-modules/src/base/ApplicationDynamoDBTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
iamRolePolicyAttachment,
} from '@cdktf/provider-aws';
import { DynamodbTableTtl } from '@cdktf/provider-aws/lib/dynamodb-table';
import { IResolvable, TerraformMetaArguments, TerraformProvider } from 'cdktf';
import { TerraformMetaArguments, TerraformProvider } from 'cdktf';
import { Construct } from 'constructs';

/**
Expand Down Expand Up @@ -85,7 +85,9 @@ export class ApplicationDynamoDBTable extends Construct {
const ignoreChanges = [
'read_capacity',
'write_capacity',
...(config.lifecycle ? config.lifecycle.ignoreChanges : []),
...(Array.isArray(config.lifecycle?.ignoreChanges)
? config.lifecycle.ignoreChanges
: []),
].filter((value) => typeof value === 'string');

this.dynamodb = new dynamodbTable.DynamodbTable(this, `dynamodb_table`, {
Expand All @@ -102,7 +104,7 @@ export class ApplicationDynamoDBTable extends Construct {
provider: config.provider,
});

if (config.readCapacity) {
if (config.readCapacity !== undefined) {
ApplicationDynamoDBTable.setupAutoscaling(
this,
config.prefix,
Expand All @@ -115,7 +117,7 @@ export class ApplicationDynamoDBTable extends Construct {
);
}

if (config.writeCapacity) {
if (config.writeCapacity !== undefined) {
ApplicationDynamoDBTable.setupAutoscaling(
this,
config.prefix,
Expand All @@ -142,13 +144,11 @@ export class ApplicationDynamoDBTable extends Construct {
*/
private static setupAutoscaling(
scope: Construct,
prefix,
prefix: string,
config: ApplicationDynamoDBTableAutoScaleProps,
dynamoDB: dynamodbTable.DynamodbTable,
capacityType: ApplicationDynamoDBTableCapacityType,
globalSecondaryIndexes:
| dynamodbTable.DynamodbTableGlobalSecondaryIndex[]
| IResolvable,
globalSecondaryIndexes: dynamodbTable.DynamodbTableConfig['globalSecondaryIndex'],
tags?: { [key: string]: string },
provider?: TerraformProvider,
): void {
Expand Down Expand Up @@ -190,6 +190,11 @@ export class ApplicationDynamoDBTable extends Construct {
capacityType === ApplicationDynamoDBTableCapacityType.Read
? gsIndex.readCapacity
: gsIndex.writeCapacity;
if (!minCapacity) {
throw new Error(
'you must specify a min capacity for each global secondary index',
);
}

// create an auto scaling policy for each index
ApplicationDynamoDBTable.createAutoScalingPolicy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ApplicationECSContainerDefinitionProps,
buildDefinitionJSON,
} from './ApplicationECSContainerDefinition';
} from './ApplicationECSContainerDefinition.ts';

describe('ApplicationECSContainerDefinition', () => {
describe('buildDefinitionJSON', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Testing } from 'cdktf';
import {
ApplicationECSService,
ApplicationECSServiceProps,
} from './ApplicationECSService';
} from './ApplicationECSService.ts';

let BASE_CONFIG: ApplicationECSServiceProps;

Expand Down
Loading

0 comments on commit 1c44a54

Please sign in to comment.