async addLambdaInsightsToFunctions()

in index.js [139:180]


  async addLambdaInsightsToFunctions(globalLambdaInsights, layerVersion, attachPolicy) {
    if (typeof this.service.functions !== 'object') {
      return;
    }
    try {

      let policyToggle = false;
      const functions = Object.keys(this.service.functions);
      for (const functionName of functions) {
        const fn = this.service.functions[functionName];
        const layerARN = await this.generateLayerARN(layerVersion, fn.architecture);
        const localLambdaInsights = fn.hasOwnProperty('lambdaInsights') ?
          this.checkLambdaInsightsType(fn.lambdaInsights) :
          null;

        if (
          localLambdaInsights === false ||
          (localLambdaInsights === null && globalLambdaInsights === null)
        ) {
          return;
        }

        const fnLambdaInsights = localLambdaInsights || globalLambdaInsights;

        if (fnLambdaInsights) {
          // attach Lambda Layer
          fn.layers = fn.layers || [];
          fn.layers.push(layerARN);
          policyToggle = true;
        }
      };
      if (attachPolicy && policyToggle) {
      // attach CloudWatchLambdaInsightsExecutionRolePolicy
        this.service.provider.iamManagedPolicies =
        this.service.provider.iamManagedPolicies || [];
        this.service.provider.iamManagedPolicies.push(lambdaInsightsManagedPolicy);
      }
      return;
    } catch (err) {
      throw err;
    }
  }