Adie Williams
01/26/2022, 1:36 PMnextjsSite construct and it works like a charm, the problem I’m facing is adding Cloudwatch alarms to the lambda’s this construct creates. My assumption was that getAllFunctions would help here but the construct doesn’t return this method and using it as I have below doesn’t return any functions. Any suggestions would be great.
// js
import * as sst from "@serverless-stack/resources";
import * as CDK from "aws-cdk-lib";
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
import * as cw_actions from "aws-cdk-lib/aws-cloudwatch-actions";
import * as sns from "aws-cdk-lib/aws-sns";
import * as chatbot from "aws-cdk-lib/aws-chatbot";
export default class BrandStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const site = new sst.NextjsSite(this, props.brand, {
path: "./",
environment: {
NEXT_PUBLIC_BRAND: props.brand,
},
});
// Get all functions created by the NextjsSite construct
const functions = this.getAllFunctions();
// Add metric and alarm for each lambda to catch errors
functions.forEach((fn) => {
const fnName = fn.functionName || "lambda";
// Configure metric for errors
const fnErrors = fn.metricErrors({
period: CDK.Duration.minutes(1),
});
// configure alarm
new cloudwatch.Alarm(this, `${fnName}-errors-alarm"}`, {
metric: fnErrors,
threshold: 1,
comparisonOperator:
cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
evaluationPeriods: 1,
alarmDescription: `Errors for ${fnName}`,
});
// Send alert
const topic = new sns.Topic(this, `${fnName}-Topic`);
cloudwatch.Alarm.addAlarmAction(new cw_actions.SnsAction(topic));
// Configure slack connection
new chatbot.SlackChannelConfiguration(this, `${fnName}-SlackChannel`, {
slackChannelConfigurationName: "sites-alerts-dev",
slackChannelId: "C02VD7K1X6G",
slackWorkspaceId: "T52H878E7",
notificationTopics: [topic],
loggingLevel: chatbot.LoggingLevel.ERROR,
});
});
this.addOutputs({
URL: site.url,
});
}
}Frank
const site = new sst.NextjsSite(...);
site.apiFunction;Frank
Frank
getAllFunctions returns all sst.Function you have in the stack. NextjsSite is a bit special where it requires Lambda@Edge, and they aren’t being returned.Adie Williams
01/26/2022, 4:37 PMsite.apiFunction is returning undefinedJay
Adie Williams
02/07/2022, 8:21 AMJay