Skip to content

Commit

Permalink
Merge pull request #2 from ssennettau/sst-refactor
Browse files Browse the repository at this point in the history
Refactoring SST and including Lambda PowerTools instrumentation
  • Loading branch information
ssennettau authored Feb 4, 2024
2 parents 0bc19ef + 625682a commit 8716fee
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 14 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
},
"type": "module",
"dependencies": {
"@aws-lambda-powertools/logger": "^1.18.0",
"@aws-lambda-powertools/metrics": "^1.18.0",
"archiver": "^6.0.1",
"fs-extra": "^11.2.0"
}
Expand Down
64 changes: 55 additions & 9 deletions src/routes/api/build-app/builder.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
// Core node modules
import fs from 'fs';
import path from 'path';
import os from 'os';

// Utilities
import { createRequire } from 'module';
import { promisify } from 'util';
const require = createRequire(import.meta.url);
const writeFile = promisify(fs.writeFile);

// Third-party modules
import archiver from 'archiver';
import fse from 'fs-extra';

const mkdir = promisify(fs.mkdir);
const require = createRequire(import.meta.url);
const writeFile = promisify(fs.writeFile);
// Environmental Status
import { dev } from '$app/environment';
const checkLambda = !!process.env.LAMBDA_TASK_ROOT;

// PowerTools for AWS Lambda
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { Logger } from '@aws-lambda-powertools/logger';
const metrics = new Metrics({
namespace: "PartySmith-experimental",
serviceName: "builder",
defaultDimensions: {
environment: dev ? "development" : "production"
}
});
const logger = new Logger({
serviceName: "/PartySmith-experimental/builder",
})

// SST won't bundle the templates, so in Prod it relies on a Lambda Layer to host the templates.
const baseTemplateDir = !!process.env.LAMBDA_TASK_ROOT ? "/opt/templates" : "./src/templateLayer/templates";
const baseTemplateDir = checkLambda ? "/opt/templates" : "./src/templateLayer/templates";

export async function buildPartySmithApp(request: AppRequest) {
console.log("Parsing request...");
Expand All @@ -37,7 +56,7 @@ export async function buildPartySmithApp(request: AppRequest) {
//response = require('./sampleDefinition.json');
} catch (error) {
console.error(error);
return generateError(requestId, "FailureGetAppDetails");
return generateError(requestId, request, "FailureGetAppDetails");
}

try {
Expand All @@ -47,7 +66,7 @@ export async function buildPartySmithApp(request: AppRequest) {
await loadDefinitionFile(tempPath, response);
} catch (err) {
console.error(err);
return generateError(requestId, "FailureLoadTemplateFiles");
return generateError(requestId, request, "FailureLoadTemplateFiles");
}

try {
Expand All @@ -59,13 +78,26 @@ export async function buildPartySmithApp(request: AppRequest) {
await updatePackageJson(tempPath, request);
} catch (err) {
console.error(err);
return generateError(requestId, "FailureUpdateDynamics");
return generateError(requestId, request, "FailureUpdateDynamics");
}

try {
const zipFilePath = await createZipArchive(tempPath);
const zipFileContents = await readZipArchive(zipFilePath);

metrics.addMetric("BuildSuccess", MetricUnits.Count, 1);
metrics.publishStoredMetrics();

logger.info("Build Success", {
requestId: requestId,
environment: dev ? "development" : "production",
url: request.url,
options: {
stylesheet: request.optStylesheet,
sst: request.optSst,
}
});

return {
status: true,
body: {
Expand All @@ -75,7 +107,7 @@ export async function buildPartySmithApp(request: AppRequest) {
};
} catch (err) {
console.error(err);
return generateError(requestId, "FailureBuildZipArchive");
return generateError(requestId, request, "FailureBuildZipArchive");
}
}

Expand Down Expand Up @@ -306,7 +338,21 @@ async function readZipArchive(zipFilePath: string) {
}
}

function generateError(requestId: string, detail: string) {
function generateError(requestId: string, request: AppRequest, detail: string) {
metrics.addMetric('BuildFailure', MetricUnits.Count, 1);
metrics.publishStoredMetrics();

logger.error("Build Failed", {
requestId: requestId,
failureDetails: detail,
environment: dev ? "development" : "production",
url: request.url,
options: {
stylesheet: request.optStylesheet,
sst: request.optSst,
}
});

return {
status: false,
body: {
Expand Down
18 changes: 13 additions & 5 deletions sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SSTConfig } from "sst";
import { SvelteKitSite } from "sst/constructs";
import { Code, LayerVersion } from "aws-cdk-lib/aws-lambda";
import { Code, Function, LayerVersion } from "aws-cdk-lib/aws-lambda";
import { RetentionDays } from "aws-cdk-lib/aws-logs";

export default {
config(_input) {
Expand All @@ -11,19 +12,26 @@ export default {
},
stacks(app) {
app.stack(function Site({ stack }) {
const templateLayer = new LayerVersion(stack, "templateLayer", {
code: Code.fromAsset("src/templateLayer"),
});

const site = new SvelteKitSite(stack, "site", {
customDomain: stack.stage == "prod" ? {
domainName: "partysmith.ssennett.net",
hostedZone: "ssennett.net",
} : undefined,
});
const templateLayer = new LayerVersion(stack, "templateLayer", {
code: Code.fromAsset("src/templateLayer"),
cdk: {
server: {
logRetention: RetentionDays.INFINITE,
layers: [templateLayer]
}
}
});

stack.addOutputs({
url: site.url,
templateLayerArn: templateLayer.layerVersionArn,
distributionId: site.cdk?.distribution.distributionId,
});
});
},
Expand Down

0 comments on commit 8716fee

Please sign in to comment.