const respond = function()

in source/lambda/services/customhelper/lib/index.js [29:268]


const respond = function(event, context, callback) {
  //handle CREATE for custom resource CreateUUID
  if (
    event.LogicalResourceId === 'CreateUUID' &&
    event.RequestType === 'Create'
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );

    let _responseData = {
      UUID: uuidv4(),
      Method: `${event.LogicalResourceId}:${event.RequestType}`,
    };

    sendResponse(
      event,
      callback,
      context.logStreamName,
      'SUCCESS',
      _responseData
    );
    return;

    //handle CREATE for SSMParameter
  } else if (
    event.LogicalResourceId === 'SSMParameter' &&
    (event.RequestType === 'Create' || event.RequestType === 'Update')
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );

    let _responseData = {
      Method: `${event.LogicalResourceId}:${event.RequestType}`,
    };


    const slackHookKey = event.ResourceProperties.SLACK_HOOK_KEY;
    const slackChannelKey = event.ResourceProperties.SLACK_CHANNEL_KEY;

    createSSMParameter(slackChannelKey, slackHookKey, function(data) {
      LOGGER.log('INFO', `SSM Status: ${JSON.stringify(data)}`);
      sendResponse(
        event,
        callback,
        context.logStreamName,
        'SUCCESS',
        _responseData
      );
      return;
    });

    //handle CREATE for DeploymentData
  } else if (
    event.LogicalResourceId === 'DeploymentData' &&
    event.RequestType === 'Create'
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );

    let _metricData = {
      Version: event.ResourceProperties.VERSION,
      AnonymousData: event.ResourceProperties.ANONYMOUS_DATA,
    };
    //call metric helper
    sendMetrics(_metricData, event, function(data) {
      LOGGER.log('INFO', `Metrics Status: ${JSON.stringify(data)}`);
      let _responseData = {
        Method: `${event.LogicalResourceId}:${event.RequestType}`,
      };

      sendResponse(
        event,
        callback,
        context.logStreamName,
        'SUCCESS',
        _responseData
      );
      return;
    });

    //handle CREATE/UPDATE for custom resource AccountAnonymousData
  } else if (
    event.LogicalResourceId === 'AccountAnonymousData' &&
    (event.RequestType === 'Create' || event.RequestType === 'Update')
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );
    let _awsAccounts = event.ResourceProperties.SUB_ACCOUNTS.split(',');

    let _spokecount;
    if (_awsAccounts[0])
      _spokecount = event.ResourceProperties.SUB_ACCOUNTS.split(',').length;
    else _spokecount = 0;

    let _metricData = {
      Version: event.ResourceProperties.VERSION,
      SNSEvents: event.ResourceProperties.SNS_EVENTS,
      SlackEvents: event.ResourceProperties.SLACK_EVENTS,
      SpokeCount: _spokecount,
      TARefreshRate: event.ResourceProperties.TA_REFRESH_RATE,
    };
    //call metric helper
    sendMetrics(_metricData, event, function(data) {
      LOGGER.log('INFO', `Metrics Status: ${JSON.stringify(data)}`);
      let _responseData = {
        Method: `${event.LogicalResourceId}:${event.RequestType}`,
      };

      sendResponse(
        event,
        callback,
        context.logStreamName,
        'SUCCESS',
        _responseData
      );
      return;
    });

    //handle CREATE for custom resource EstablishTrust
  } else if (
    event.LogicalResourceId === 'EstablishTrust' &&
    event.RequestType === 'Create'
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );
    let awsAccounts = event.ResourceProperties.SUB_ACCOUNTS.replace(/"/g, '');
    let _awsAccounts = awsAccounts.split(',');

    createTrust(_awsAccounts, function(data) {
      LOGGER.log('INFO', data);
      let _responseData = {
        Method: `${event.LogicalResourceId}:${event.RequestType}`,
      };

      sendResponse(
        event,
        callback,
        context.logStreamName,
        'SUCCESS',
        _responseData
      );
      return;
    });

    //handle UPDATE for custom resource EstablishTrust
  } else if (
    event.LogicalResourceId === 'EstablishTrust' &&
    event.RequestType === 'Update'
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );
    let oldAccounts = event.OldResourceProperties.SUB_ACCOUNTS.replace(
      /"/g,
      ''
    );
    let _oldAccounts = oldAccounts.split(',');
    let newAccounts = event.ResourceProperties.SUB_ACCOUNTS.replace(/"/g, '');
    let _newAccounts = newAccounts.split(',');

    //remove trust first
    removeTrust(_oldAccounts, function(data) {
      LOGGER.log('INFO', data);
      //now establish trust for updated account list
      createTrust(_newAccounts, function(data) {
        LOGGER.log('INFO', data);
        let _responseData = {
          Method: `${event.LogicalResourceId}:${event.RequestType}`,
        };

        sendResponse(
          event,
          callback,
          context.logStreamName,
          'SUCCESS',
          _responseData
        );
        return;
      });
    });

    //handle DELETE for custom resource EstablishTrust
  } else if (
    event.LogicalResourceId === 'EstablishTrust' &&
    event.RequestType === 'Delete'
  ) {
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );
    let awsAccounts = event.ResourceProperties.SUB_ACCOUNTS.replace(/"/g, '');
    let _awsAccounts = awsAccounts.split(',');

    removeTrust(_awsAccounts, function(data) {
      LOGGER.log('INFO', data);
      let _responseData = {
        Method: `${event.LogicalResourceId}:${event.RequestType}`,
      };

      sendResponse(
        event,
        callback,
        context.logStreamName,
        'SUCCESS',
        _responseData
      );
      return;
    });

    //always send response to custom resource
  } else {
    let _responseData = {
      Method: `${event.LogicalResourceId}:${event.RequestType}`,
    };
    LOGGER.log(
      'DEBUG',
      `event details: ${event.LogicalResourceId}:${event.RequestType}`
    );

    sendResponse(
      event,
      callback,
      context.logStreamName,
      'SUCCESS',
      _responseData
    );
    return;
  }
};