Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
aws-elasticsearch-model
Advanced tools
Simplifies AWS ElasticSearch Service integration into serverless applications built with AWS Lambda
A small library that simplifies AWS Elasticsearch Service integration into serverless applications build with AWS Lambda
npm i aws-elasticsearch-model --save
or
yarn add aws-elasticsearch-model
import AWS from 'aws-sdk';
import {ElasticModel} from 'aws-elasticsearch-model';
/*
ElasticModel uses aws-sdk's default behaviour to obtain region + credentials from your environment.
If you would like to set these manually, you can set them on aws-sdk:
*/
AWS.config.update({region: 'eu-west-1'});
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users'
});
ElasticModel will automatically create elasticsearch index if missing
const users = [
{
id: '554d9d95-b40b-4ddc-9fa9-ed3eb8b5c591',
email: 'Vidal45@gmail.com',
name: 'Beverly_Mayer18'
},
{
id: 'cf39921a-41ce-49cc-b034-13fb1508c726',
email: 'Marcel16@yahoo.com',
name: 'Petra70'
},
{
id: '81ab442a-b693-47e5-b9e1-6807cbb7978e',
email: 'Howell73@gmail.com',
name: 'Carlotta_Kuhic37'
}
];
const result = await elasticModel.bulkIndex(users);
Quite often DynamoDB table is used as a source of truth to store data and Elasticsearch is used to provide advanced search capabilities. In this case a Lambda function is required to sync data between DynamoDB table and Elasticsearch index
Attach this lambda handler to your DynamoDB table and it will sync all the data changes with Elasticsearch
import {DynamoDBStreamEvent} from 'aws-lambda';
import {ElasticModel} from 'aws-elasticsearch-model';
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users'
});
export const handler = async (event: DynamoDBStreamEvent) => {
return elasticModel.indexFromDynamoDBStream(event);
};
Your lambda function needs to have a role attached to it that allows to access your Elasticsearch instance
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"es:ESHttpPost",
"es:ESHttpPut",
"es:ESHttpDelete"
],
"Resource": "arn:aws:es:AWS_REGION:AWS_ACCOUNT_ID:domain/my-aws-elasticsearch-domain/*",
"Effect": "Allow"
}
]
}
queryBuilder method will return a chainable function that allows easily build complex queries for elasticsearch with a simple, predictable api.
It uses bodybuilder package by Daniel Paz-Soldan
To get full builder api check the docs here
To execute the query call query.exec()
at the end
const query = elasticModel
.queryBuilder()
.orFilter('term', 'email', 'Vidal45@gmail.com')
.orFilter('term', 'email', 'Marcel16@yahoo.com');
const result = await query.exec();
{
items: [
{
id: '554d9d95-b40b-4ddc-9fa9-ed3eb8b5c591',
email: 'Vidal45@gmail.com',
name: 'Beverly_Mayer18'
},
{
id: 'cf39921a-41ce-49cc-b034-13fb1508c726',
email: 'Marcel16@yahoo.com',
name: 'Petra70'
}
],
total: 2,
raw: {...} // raw elasticsearch response
}
You can also run a search query directly. The query above is equivalent to:
const result = await elasticModel.search({
query: {
bool: {
filter: {
bool: {
should: [
{
term: {
email: 'Vidal45@gmail.com'
}
},
{
term: {
email: 'Marcel16@yahoo.com'
}
}
]
}
}
}
}
});
By default Elasticsearch will try to guess your data structure but you can provide your own index mapping to improve search performance.
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users',
settings: {
analysis: {
analyzer: {
email: {
type: 'custom',
tokenizer: 'uax_url_email'
}
}
}
},
mapping: {
id: {
type: 'keyword'
},
email: {
type: 'text',
analyzer: 'email'
},
name: {
type: 'text'
}
}
});
Read more about mappings and settings
id
fieldBy default ElasticModel will use id
field in your data to provide unique id
to Elasticsearch but it can be customized.
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users',
idField: 'userId'
});
When you sync your data with DynamoDB DynamoDBStreamEvent
event provides Keys
object that will contain a composite hash key of your item. For example if you have hashKey: 'userId', rangeKey: 'createdAt'
by default only userId
filed will be selected as id
(if it is specified in config).
This behaviour can be customized:
export const handler = async (event: DynamoDBStreamEvent) => {
return elasticModel.indexFromDynamoDBStream(event, keys => {
// keys: {userId, createdAt}
// use base64 encoded userId
return new Buffer(keys.userId).toString('base64');
});
};
To completely exclude fields from Elasticsearch you can provide excludedFields
option. This option will remove the field before data is submitted.
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users',
excludedFields: ['email']
});
If you want field value to be stored but not indexed or available for search you can set index: false
parameter in mapping
const elasticModel = new ElasticModel({
host: 'https://my-aws-elasticsearch-domain.eu-west-1.es.amazonaws.com',
index: 'users',
mapping: {
id: {
type: 'keyword'
},
email: {
index: false,
type: 'text'
},
name: {
type: 'text'
}
}
});
ElasticModel provides direct access to elasticsearch client. You can access client instance as elasticModel.client
You can find all config options here
FAQs
Simplifies AWS ElasticSearch Service integration into serverless applications built with AWS Lambda
We found that aws-elasticsearch-model demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.