Skip to content

franz101/serverless-plugin-elasticsearch-index

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless Plugin Elasticsearch Index

Serverless plugin to automate the creation of elasticsearch indices and mappings on deployment of your services. In the background it uses elasticsearch client.

Installation

Clone this repo into your node_modules.

So that you have a folder called serverless-plugin-elasticsearch-index with the contents of this repo (index.js,lib,...etc)

Usage

When installed in your service, update your serverless.yml file as follows:

Register the plugin in your service

For example:

service: foo-service

plugins:
  - serverless-plugin-elasticsearch-index

...

Order does not matter, the plugin can be added at any position in the list.

Configure the plugin

Use the custom section in serverless.yml to apply settings to the plugin:

custom:
  slsEsIndices:
    type: project
    index: projects
    mapping: ${file(./src/mappings/project.js)}
    region: ${opt:region, file(../../../config.json):region, 'eu-central-1'}
    endpointName: ${self:custom.eubfrEnvironment}:resources-elasticsearch:PublicEndpoint
    version: "6.4"
...

Variables can be used for extracting values from various parts of the project for your convenience.

As the elasticsearch domain endpoint address depends on a deployed service, the evaluated expression should match an exported named value from an existing CloudFormation stack. ListExports is used in the background for the evaluation.

Export domain address

From the service which creates the elasticsearch domain in AWS, add an export in the end of serverless.yml

Outputs:
  PublicEndpoint:
    Description: The API endpoint of elasticsearch domain.
    Value:
      Fn::GetAtt: ["PublicElasticSearchDomain", "DomainEndpoint"]
    Export:
      # Global varibale, uses eubfrEnvironment instead of stage
      Name: "${self:custom.eubfrEnvironment}:${self:service}:PublicEndpoint"

By adding this export, other plugins such as serverless-stack-output can also hook into the creation phase of the CloudFormation stack and export the result of the created elasticsearch service.