Skip to content

Commit

Permalink
Set up project to manage infrastructure
Browse files Browse the repository at this point in the history
Rather than relying on the infrastructure to be managed externally
manage it with code.
  • Loading branch information
bryanjswift committed Aug 23, 2020
1 parent b6e5e2f commit 4ba5379
Show file tree
Hide file tree
Showing 33 changed files with 7,439 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.yarn/** linguist-vendored
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
/target
**/*.rs.bk

# Yarn - Not Zero Install
# https://yarnpkg.com/advanced/qa#which-files-should-be-gitignored
.yarn/*
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vim
.vscode
.yarn
src
3 changes: 3 additions & 0 deletions .vim/coc-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tsserver.tsdk": ".yarn/sdks/typescript/lib"
}
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"arcanis.vscode-zipfs",
"esbenp.prettier-vscode"
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"prettier.prettierPath": ".yarn/sdks/prettier/index.js"
}
86 changes: 86 additions & 0 deletions .yarn/releases/yarn-berry.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions .yarn/sdks/integrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file is automatically generated by PnPify.
# Manual changes will be lost!

integrations:
- vscode
- vim
20 changes: 20 additions & 0 deletions .yarn/sdks/prettier/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require prettier/index.js
require(absPnpApiPath).setup();
}
}

// Defer to the real prettier/index.js your application uses
module.exports = absRequire(`prettier/index.js`);
6 changes: 6 additions & 0 deletions .yarn/sdks/prettier/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "prettier",
"version": "2.0.5-pnpify",
"main": "./index.js",
"type": "commonjs"
}
20 changes: 20 additions & 0 deletions .yarn/sdks/typescript/bin/tsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/bin/tsc
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/bin/tsc your application uses
module.exports = absRequire(`typescript/bin/tsc`);
20 changes: 20 additions & 0 deletions .yarn/sdks/typescript/bin/tsserver
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/bin/tsserver
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/bin/tsserver your application uses
module.exports = absRequire(`typescript/bin/tsserver`);
20 changes: 20 additions & 0 deletions .yarn/sdks/typescript/lib/tsc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsc.js
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/lib/tsc.js your application uses
module.exports = absRequire(`typescript/lib/tsc.js`);
63 changes: 63 additions & 0 deletions .yarn/sdks/typescript/lib/tsserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

const moduleWrapper = tsserver => {
// VSCode sends the zip paths to TS using the "zip://" prefix, that TS
// doesn't understand. This layer makes sure to remove the protocol
// before forwarding it to TS, and to add it back on all returned paths.

const {isAbsolute} = require(`path`);

const Session = tsserver.server.Session;
const {onMessage: originalOnMessage, send: originalSend} = Session.prototype;

return Object.assign(Session.prototype, {
onMessage(/** @type {string} */ message) {
return originalOnMessage.call(this, JSON.stringify(JSON.parse(message), (key, value) => {
return typeof value === 'string' ? removeZipPrefix(value) : value;
}));
},

send(/** @type {any} */ msg) {
return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => {
return typeof value === 'string' ? addZipPrefix(value) : value;
})));
}
});

function addZipPrefix(str) {
// We add the `zip:` prefix to both `.zip/` paths and virtual paths
if (isAbsolute(str) && !str.match(/^zip:/) && (str.match(/\.zip\//) || str.match(/\$\$virtual\//))) {
// Absolute VSCode `Uri.fsPath`s need to start with a slash.
// VSCode only adds it automatically for supported schemes,
// so we have to do it manually for the `zip` scheme.
return `zip:${str.replace(/^\/?/, `/`)}`;
} else {
return str;
}
}

function removeZipPrefix(str) {
return process.platform === 'win32'
? str.replace(/^zip:\//, ``)
: str.replace(/^zip:/, ``);
}
};

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsserver.js
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/lib/tsserver.js your application uses
module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`));
20 changes: 20 additions & 0 deletions .yarn/sdks/typescript/lib/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.js";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/typescript.js
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/lib/typescript.js your application uses
module.exports = absRequire(`typescript/lib/typescript.js`);
6 changes: 6 additions & 0 deletions .yarn/sdks/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "typescript",
"version": "4.0.2-pnpify",
"main": "./lib/typescript.js",
"type": "commonjs"
}
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: ".yarn/releases/yarn-berry.js"
10 changes: 10 additions & 0 deletions infrastructure/cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.js
!jest.config.js
*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out

# Parcel default cache directory
.parcel-cache
6 changes: 6 additions & 0 deletions infrastructure/cdk/.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 infrastructure/cdk/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 TypeScript development with CDK.

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
17 changes: 17 additions & 0 deletions infrastructure/cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import 'source-map-support/register';
import {App} from '@aws-cdk/core';
import {ScalableEmail} from '../lib/scalable-email-stack';

export type StackMap = Record<string, ScalableEmail | undefined>;

export const STAGES = ['Local', 'Test', 'Staging', 'Production'];

const app = new App();
export const stacks = STAGES.reduce<StackMap>((map, stage) => {
const stack = new ScalableEmail(app, `ScalableEmail${stage}`, {
stage: stage.toLowerCase(),
});
map[stage] = stack;
return map;
}, {});
8 changes: 8 additions & 0 deletions infrastructure/cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"app": "npx ts-node bin/cdk.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"@aws-cdk/core:stackRelativeExports": "true"
}
}
7 changes: 7 additions & 0 deletions infrastructure/cdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
55 changes: 55 additions & 0 deletions infrastructure/cdk/lib/database-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {CfnOutput, Construct, RemovalPolicy} from '@aws-cdk/core';
import {
AttributeType,
BillingMode,
Table as DynamoTable,
TableEncryption,
} from '@aws-cdk/aws-dynamodb';
import {Alias, Key} from '@aws-cdk/aws-kms';
import {Parameters} from './parameters';

type Props = Parameters;

export class DatabaseStack extends Construct {
readonly encryptionKey: CfnOutput;
readonly tableName: CfnOutput;

constructor(scope: Construct, id: string, props: Props) {
super(scope, id);
// Get the "Stage" being deployed
const {stage} = props;
// Set up Dynamo
const databaseEncryptionKey = new Key(this, 'EncryptionKey', {
description: 'The key used to encrypt messages stored in the database.',
enabled: true,
enableKeyRotation: true,
removalPolicy: RemovalPolicy.DESTROY,
});
const databaseEncryptionAlias = new Alias(this, 'EncryptionKeyAlias', {
aliasName: `alias/bryanjswift/email_service/${stage}/db`,
targetKey: databaseEncryptionKey,
});
databaseEncryptionAlias.node.addDependency(databaseEncryptionKey);
const database = new DynamoTable(this, 'Messages', {
tableName: `email_db_${stage}`,
partitionKey: {
name: 'EmailId',
type: AttributeType.STRING,
},
billingMode: BillingMode.PAY_PER_REQUEST,
encryption: TableEncryption.CUSTOMER_MANAGED,
encryptionKey: databaseEncryptionAlias,
removalPolicy: RemovalPolicy.DESTROY,
});
database.node.addDependency(databaseEncryptionAlias);
this.tableName = new CfnOutput(this, 'DatabaseTable', {
value: database.tableName,
description: 'Name of the Dynamo Table storing message data.',
});
this.encryptionKey = new CfnOutput(this, 'DatabaseEncryptionKey', {
value: databaseEncryptionAlias.keyArn,
description:
'ARN of the key used to encrypted Email Messages in Database.',
});
}
}
3 changes: 3 additions & 0 deletions infrastructure/cdk/lib/parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Parameters {
stage: string;
}
Loading

0 comments on commit 4ba5379

Please sign in to comment.