-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): add amplify, monitoring, signing methods
- Loading branch information
1 parent
b3ef63e
commit 550aa9a
Showing
7 changed files
with
1,065 additions
and
819 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as Amplify from '@aws-cdk/aws-amplify-alpha'; | ||
import { RemovalPolicy } from 'aws-cdk-lib'; | ||
import { Asset } from 'aws-cdk-lib/aws-s3-assets'; | ||
|
||
import { DotStack } from '../constructs/Stack'; | ||
|
||
interface AddAmplifyAppOptions { | ||
distPath: string; | ||
domainName?: string; | ||
environmentVariables?: { [key: string]: string }; | ||
name: string; | ||
pwaRedirect?: boolean; | ||
scope: DotStack; | ||
subdomain?: string; | ||
} | ||
|
||
interface AddAmplifyAppResult { | ||
app: Amplify.App; | ||
} | ||
|
||
export const addAmplifyApp = (options: AddAmplifyAppOptions): AddAmplifyAppResult => { | ||
const { distPath, domainName, environmentVariables = {}, name, pwaRedirect, scope } = options; | ||
const subdomain = options.subdomain ?? scope.env; | ||
const baseName = DotStack.baseName(name, '-app'); | ||
const appName = scope.resourceName(baseName); | ||
|
||
const asset = new Asset(scope, `${appName}-asset`, { path: distPath }); | ||
const app = new Amplify.App(scope, appName, { | ||
appName | ||
}); | ||
const branch = app.addBranch(scope.env, { asset, environmentVariables }); | ||
|
||
app.applyRemovalPolicy(RemovalPolicy.DESTROY); | ||
|
||
// Note: needed for PWA routing | ||
if (pwaRedirect) { | ||
app.addCustomRule({ | ||
source: '/<*>', | ||
status: Amplify.RedirectStatus.NOT_FOUND_REWRITE, | ||
target: '/index.html' | ||
}); | ||
} | ||
|
||
if (domainName) { | ||
const domain = app.addDomain(domainName, { | ||
enableAutoSubdomain: false | ||
}); | ||
domain.mapSubDomain(branch, subdomain); | ||
} | ||
|
||
return { app }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { ApplicationLoadBalancedFargateService } from 'aws-cdk-lib/aws-ecs-patterns'; | ||
import { MonitoringFacade, SnsAlarmActionStrategy } from 'cdk-monitoring-constructs'; | ||
|
||
import type { DotStack } from '../constructs/Stack'; | ||
|
||
import { addTopic } from './sns'; | ||
|
||
interface AddMonitoringOptions { | ||
emailAddress: string; | ||
fargateService: ApplicationLoadBalancedFargateService; | ||
scope: DotStack; | ||
} | ||
|
||
export const addFargateMonitoring = (options: AddMonitoringOptions) => { | ||
const { emailAddress, fargateService, scope } = options; | ||
|
||
const { topic: onAlarmTopic } = addTopic({ emailAddress, name: 'alarm', scope }); | ||
|
||
const monitoring = new MonitoringFacade(scope, scope.resourceName('monitor'), { | ||
alarmFactoryDefaults: { | ||
action: new SnsAlarmActionStrategy({ onAlarmTopic }), | ||
actionsEnabled: true, | ||
alarmNamePrefix: scope.resourceName('alarm') | ||
} | ||
}); | ||
|
||
monitoring.monitorFargateService({ | ||
addCpuUsageAlarm: { | ||
Warning: { | ||
maxUsagePercent: 80 | ||
} | ||
}, | ||
addHealthyTaskPercentAlarm: { | ||
Warning: { | ||
minHealthyTaskPercent: 75 | ||
} | ||
}, | ||
addMemoryUsageAlarm: { | ||
Warning: { | ||
maxUsagePercent: 80 | ||
} | ||
}, | ||
addToAlarmDashboard: true, | ||
addToDetailDashboard: true, | ||
addToSummaryDashboard: true, | ||
fargateService | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { generateKeyPairSync } from 'crypto'; | ||
|
||
import { PublicKey } from 'aws-cdk-lib/aws-cloudfront'; | ||
|
||
import { type DotStack } from '../constructs/Stack'; | ||
|
||
import { addSecret } from './secret'; | ||
import { addParam } from './ssm'; | ||
|
||
const generateRsaKeyPair = () => { | ||
const { privateKey, publicKey } = generateKeyPairSync('rsa', { | ||
modulusLength: 2048, | ||
privateKeyEncoding: { | ||
format: 'pem', | ||
type: 'pkcs8' | ||
}, | ||
publicKeyEncoding: { | ||
format: 'pem', | ||
type: 'spki' | ||
} | ||
}); | ||
return { privateKey, publicKey }; | ||
}; | ||
|
||
export const addSigningKey = (scope: DotStack) => { | ||
// FIXME: We have to not run this for additional deploys to prod | ||
// because for some reason it fails if the public key exists already | ||
// https://github.com/aws/aws-cdk/issues/15301 | ||
const keyPair = generateRsaKeyPair(); | ||
|
||
addSecret({ | ||
name: `${scope.env}-signing-key-pair`, | ||
scope, | ||
secretName: `${scope.ssmPrefix}/key/signing`, | ||
value: JSON.stringify(keyPair) | ||
}); | ||
|
||
const baseName = 'signing-pubkey'; | ||
const publicKeyName = scope.resourceName(baseName); | ||
const cfKey = new PublicKey(scope, publicKeyName, { | ||
encodedKey: keyPair.publicKey, | ||
publicKeyName | ||
}); | ||
|
||
scope.overrideId(cfKey, publicKeyName); | ||
|
||
addParam({ | ||
id: `${publicKeyName}-id`, | ||
name: `${scope.ssmPrefix}/id/${baseName}`, | ||
scope, | ||
value: cfKey.publicKeyId | ||
}); | ||
}; |
Oops, something went wrong.