How to use serverless-offline - 10 common examples

To help you get started, we’ve selected a few serverless-offline examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tradle / serverless-iot-local / index.js View on Github external
select,
            payload: message,
            context: {
              topic: () => topic,
              clientid: () => clientId,
              principal: () => {}
            }
          })

          let handler // The lambda function
          try {
            process.env = _.extend({}, this.service.provider.environment, this.service.functions[name].environment, this.originalEnvironment)
            process.env.SERVERLESS_OFFLINE_PORT = apiGWPort
            process.env.AWS_LAMBDA_FUNCTION_NAME = this.service.service + '-' + this.service.provider.stage
            process.env.AWS_REGION = this.service.provider.region
            handler = functionHelper.createHandler(options, this.options)
          } catch (err) {
            this.log(`Error while loading ${name}: ${err.stack}, ${requestId}`)
            return
          }

          const lambdaContext = createLambdaContext(fn)
          try {
            handler(event, lambdaContext, lambdaContext.done)
          } catch (error) {
            this.log(`Uncaught error in your '${name}' handler: ${error.stack}, ${requestId}`)
          }
        })
      })
github CoorpAcademy / serverless-plugins / packages / serverless-offline-kinesis / index.js View on Github external
eventHandler(streamEvent, functionName, shardId, chunk, cb) {
    const streamName = streamEvent.arn.split('/')[1];
    this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

    const {location = '.'} = this.service.custom['serverless-offline'];

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: chunk.map(({SequenceNumber, ApproximateArrivalTimestamp, Data, PartitionKey}) => ({
        kinesis: {
          partitionKey: PartitionKey,
          kinesisSchemaVersion: '1.0',
          data: Data.toString('base64'),
          sequenceNumber: SequenceNumber
github ar90n / serverless-s3-local / index.js View on Github external
const func = (s3Event) => {
        const baseEnvironment = {
          IS_LOCAL: true,
          IS_OFFLINE: true,
        };

        try {
          Object.assign(
            process.env,
            baseEnvironment,
            this.service.provider.environment,
            serviceFunction.environment || {},
          );

          const handler = functionHelper.createHandler(
            funOptions,
            this.options,
          );
          const callback = (error, response) => {
            console.log(`serverless-s3-local: callback is called with ${error} and ${response}`)
          }
          handler(s3Event, lambdaContext, callback);
        } catch (e) {
          console.error('Error while running handler', e);
        }
      };
github CoorpAcademy / serverless-plugins / packages / serverless-offline-kinesis / index.js View on Github external
eventHandler(streamEvent, functionName, shardId, chunk, cb) {
    const streamName = streamEvent.arn.split('/')[1];
    this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

    const {location = '.'} = this.service.custom['serverless-offline'];

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: chunk.map(({SequenceNumber, ApproximateArrivalTimestamp, Data, PartitionKey}) => ({
        kinesis: {
          partitionKey: PartitionKey,
          kinesisSchemaVersion: '1.0',
          data: Data.toString('base64'),
          sequenceNumber: SequenceNumber
        },
github msfidelis / serverless-offline-sqs-esmq / index.js View on Github external
eventHandler(queueEvent, functionName, messages, cb) {
    if (!messages) return cb();

    const streamName = this.getQueueName(queueEvent);
    this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

    const {location = '.'} = getConfig(this.service, 'serverless-offline');

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: messages.map(
        ({
          MessageId: messageId,
          ReceiptHandle: receiptHandle,
          Body: body,
          Attributes: attributes,
github tradle / serverless-iot-local / index.js View on Github external
Object.keys(this.service.functions).forEach(key => {
      const fun = this._getFunction(key)
      const funName = key
      const servicePath = path.join(this.serverless.config.servicePath, location)
      const funOptions = functionHelper.getFunctionOptions(fun, key, servicePath)
      this.debug(`funOptions ${JSON.stringify(funOptions, null, 2)} `)

      if (!fun.environment) {
        fun.environment = {}
      }

      fun.environment.AWS_LAMBDA_FUNCTION_NAME = `${this.service.service}-${this.service.provider.stage}-${funName}`

      this.debug('')
      this.debug(funName, 'runtime', runtime, funOptions.babelOptions || '')
      this.debug(`events for ${funName}:`)

      if (!(fun.events && fun.events.length)) {
        this.debug('(none)')
        return
      }
github mitipi / serverless-iot-offline / ruleHandler.js View on Github external
Object.keys(slsService.functions).forEach(key => {
    const fun = getFunction(key, slsService)
    const servicePath = path.join(serverless.config.servicePath, location)
    const funOptions = functionHelper.getFunctionOptions(fun, key, servicePath)

    if (!fun.environment) {
      fun.environment = {}
    }

    if (!(fun.events && fun.events.length)) {
      return
    }

    fun.events.forEach(event => {
      if (!event.iot) return

      const {iot} = event
      const {sql, actions} = iot

      const parsed = parseSelect(sql)
github mitipi / serverless-iot-offline / ruleHandler.js View on Github external
ruleConf.Actions.forEach((action) => {
        if (action.Lambda) {
          const awsFunName = action.Lambda.FunctionArn['Fn::GetAtt'][0]
          funName = _.lowerFirst(awsFunName).replace('LambdaFunction', '')
          fun = getFunction(funName, slsService)
          const servicePath = path.join(serverless.config.servicePath, location)
          funOptions = functionHelper.getFunctionOptions(fun, key, servicePath)

          if (!fun.environment) {
            fun.environment = {}
          }
        } else {
          actions.push(action)
        }
      })
github CoorpAcademy / serverless-plugins / packages / serverless-offline-kinesis / src / index.js View on Github external
const {location = '.'} = this.getConfig();

    const __function = this.service.getFunction(functionName);

    const {env} = process;
    const functionEnv = assignAll([
      {AWS_REGION: get('service.provider.region', this)},
      env,
      get('service.provider.environment', this),
      get('environment', __function)
    ]);
    process.env = functionEnv;

    const serviceRuntime = this.service.provider.runtime;
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = functionHelper.getFunctionOptions(
      __function,
      functionName,
      servicePath,
      serviceRuntime
    );
    const handler = functionHelper.createHandler(funOptions, this.getConfig());
    const lambdaContext = new LambdaContext(__function, this.service.provider, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${functionName} ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: chunk.map(({SequenceNumber, ApproximateArrivalTimestamp, Data, PartitionKey}) => ({
        kinesis: {
github CoorpAcademy / serverless-plugins / packages / serverless-offline-sqs / src / index.js View on Github external
const __function = this.service.getFunction(functionName);

    const {env} = process;
    const functionEnv = assignAll([
      {AWS_REGION: get('service.provider.region', this)},
      env,
      get('service.provider.environment', this),
      get('environment', __function)
    ]);
    process.env = functionEnv;

    const serviceRuntime = this.service.provider.runtime;
    const servicePath = join(this.serverless.config.servicePath, location);

    const funOptions = functionHelper.getFunctionOptions(
      __function,
      functionName,
      servicePath,
      serviceRuntime
    );
    const handler = functionHelper.createHandler(funOptions, config);

    const lambdaContext = new LambdaContext(__function, this.service.provider, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${functionName} ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const awsRegion = config.region || 'us-west-2';
    const awsAccountId = config.accountId || '000000000000';

serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project

MIT
Latest version published 17 days ago

Package Health Score

95 / 100
Full package analysis