Hi all. I’m working on deploying multiple sites vi...
# help
a
Hi all. I’m working on deploying multiple sites via the
nextjsSite
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.
Copy code
// 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,
    });
  }
}
f
Hey @Adie Williams, let me take a look, I think we can expose the functions as the construct properties, so you can do:
Copy code
const site = new sst.NextjsSite(...);
site.apiFunction;
Would that work for you?
^ To add a bit of context, the
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.
a
Thanks for the clarification @Frank, unfortunately
site.apiFunction
is returning
undefined
j
@Adie Williams I think @Frank meant more that we could implement that property and cut a new release.
a
Thanks @Jay, that makes more sense 🤦 . That would be great 👍
j
No worries! @Frank just brining this up.