@0xc/serverless-offline-aws-sqs 中文文档教程

发布于 4年前 浏览 19 项目主页 更新于 3年前

builds.sr.ht status< img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT">

serverless-offline-sqs

这个无服务器离线插件在你的本地模拟 AWS λ 和 SQS 队列使用 ElasticMQ 的机器。 为此,它会侦听 SQS 队列并调用您的处理程序。

Installation

首先,将 @0xc/serverless-offline-aws-sqs 添加到您的项目中:

yarn add @0xc/serverless-offline-aws-sqs

然后在您项目的 serverless.yml 文件中,将以下条目添加到 serverless-offline(如果存在,则在 serverless-webpack 之后):@0xc/serverless-offline-aws-sqs

plugins:
  - serverless-webpack
  - "@0xc/serverless-offline-aws-sqs"
  - serverless-offline

Configure

Functions

该插件的功能配置遵循 serverless 文档

functions:
  mySQSHandler:
    handler: handler.default
    events:
      - sqs:
          queueName: MyQueue
          arn:
            Fn::GetAtt:
              - MyQueue
              - Arn
resources:
  Resources:
    MyQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: MyQueue

在您的函数中,您可以使用包装器在本地和 (aws) 生产环境之间切换。

const AWS = require("aws-sdk");

let options = {};

// connect to local ElasticMQ if running offline
if (process.env.IS_OFFLINE) {
  options = {
    apiVersion: "2012-11-05",
    region: "localhost",
    endpoint: "http://0.0.0.0:9324",
    sslEnabled: false
  };
}

const client = new AWS.SQS(options);

export default client;

Local SQS (ElasticMQ)

aws.SQS客户端的配置 是通过使用您的特定配置在 serverless.yml 中定义一个 custom: serverless-offline-sqs 对象来完成的。

您可以使用具有以下配置的 ElasticMQ

custom:
  "@0xc/serverless-offline-aws-sqs":
    apiVersion: "2012-11-05"
    endpoint: http://0.0.0.0:9324
    region: eu-west-1
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false

在启动无服务器功能之前,您需要运行 ElasticMQ。

docker run -it -p 9324:9324 s12v/elasticmq:latest

队列将自动创建。

Roadmap

  • install ElasticMQ automatically or start docker automatically

请帮我完成这件事! 欢迎每一个 PR。

Credits

这是来自 @meuschke/serverless-offline-aws-sqs 项目的自定义分支

builds.sr.ht statusLicense: MIT

serverless-offline-sqs

This Serverless-offline plugin emulates AWS λ and SQS queue on your local machine by using ElasticMQ. To do so, it listens SQS queue and invokes your handlers.

Installation

First, add @0xc/serverless-offline-aws-sqs to your project:

yarn add @0xc/serverless-offline-aws-sqs

Then inside your project's serverless.yml file, add following entry to the plugins section before serverless-offline (and after serverless-webpack if presents): @0xc/serverless-offline-aws-sqs.

plugins:
  - serverless-webpack
  - "@0xc/serverless-offline-aws-sqs"
  - serverless-offline

Configure

Functions

Ths configuration of function of the plugin follows the serverless documentation.

functions:
  mySQSHandler:
    handler: handler.default
    events:
      - sqs:
          queueName: MyQueue
          arn:
            Fn::GetAtt:
              - MyQueue
              - Arn
resources:
  Resources:
    MyQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: MyQueue

Inside your functions you could use a wrapper to switch between local and (aws) production environment.

const AWS = require("aws-sdk");

let options = {};

// connect to local ElasticMQ if running offline
if (process.env.IS_OFFLINE) {
  options = {
    apiVersion: "2012-11-05",
    region: "localhost",
    endpoint: "http://0.0.0.0:9324",
    sslEnabled: false
  };
}

const client = new AWS.SQS(options);

export default client;

Local SQS (ElasticMQ)

The configuration of aws.SQS's client of the plugin is done by defining a custom: serverless-offline-sqs object in your serverless.yml with your specific configuration.

You could use ElasticMQ with the following configuration:

custom:
  "@0xc/serverless-offline-aws-sqs":
    apiVersion: "2012-11-05"
    endpoint: http://0.0.0.0:9324
    region: eu-west-1
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false

Before you start your serverless functions you ElasticMQ needs to run.

docker run -it -p 9324:9324 s12v/elasticmq:latest

Queues will be automatically created.

Roadmap

  • install ElasticMQ automatically or start docker automatically

PLEASE HELP ME TO GET THIS DONE! EVERY PR IS WELCOME.

Credits

This is a custom fork from @meuschke/serverless-offline-aws-sqs Project

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文