1.14.0 • Published 10 months ago

sls-helper v1.14.0

Weekly downloads
23
License
MIT
Repository
github
Last release
10 months ago

Serverless Helper

A framework to implement serverless framework config file with ease and standarized resources.

Installation

npm install sls-helper

Usage

// serverless.js

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [

		['bucket', {
			resourceName: 'ServiceBucket',
			name: 'my-bucket'
		}],

		'custom.myHelperWithoutConfigs'
	]
});

Plugins

In order to implement a plugin for the framework, you must publish a package with the following pattern: sls-helper-plugin-{plugin-name}.

The plugin-name must then be used as a prefix to use a helper of that plugin, for example: plugin-name.helperName

The package must export an object mapping helper names to helper implementations.

Each helper is a function that receives the following arguments:

  • serviceConfig: The current service config object
  • helperParams: The (optional) configuration for the helper.

It also has to return the new service config object.

Plugin list:

Core Helpers

S3 Bucket (bucket)

Used to implement a bucket with blocked public access

OptionTypeDescriptionAttributesDefault value
resourceNamestringThe logical name of the bucketRequired
namestringThe bucket nameRequired
aclstringThe bucket aclPrivate
corsboolean | object | arrayThe bucket CORS configuration. If set to true, default configuration is set (every origin, every header)
cors.id, cors[].idstringThe CORS rule ID
cors.origin, cors[].originarray | string | booleanThe CORS rule origin(s) (if value is true, it's set as every origin)
cors.methods, cors[].methodsarray | stringThe CORS rule method(s)
cors.headers, cors[].headersarray | stringThe CORS rule headers(s)
cors.exposedHeaders, cors[].exposedHeadersarray | stringThe CORS rule exposed headers(s)
cors.maxAge, cors[].maxAgenumberThe CORS rule max age
tagsobjectA key-value object of tags to associate to the bucket
rawPropsobjectExtra raw propertiesSee the official documentation

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['bucket', {
			resourceName: 'ServiceBucket',
			name: 'my-bucket'
		}]
	]
});

IAM Role Statement (iamStatement)

(since 1.2.0)

Used to implement an IAM Role statement for your service

OptionTypeDescriptionAttributesDefault value
effectstringThe IAM statement effectEnum('Allow', 'Deny')'Allow'
actionstring | array\<string>The IAM statement actionRequired
resourcestring | array\<string>The IAM statement resourceRequired

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['iamStatement', {
			action: [
				's3:PutObject',
				's3:GetObject'
			],
			resource: 'arn:aws:s3:::my-bucket/*'
		}]
	]
});

API Lambda Proxy (apiLambdaProxy)

Used to implement Lambda Proxy APIs

OptionTypeDescriptionAttributesDefault value
functionNamestringThe function nameRequired
handlerstringThe function handlerRequired
descriptionstringThe function description
pathstringThe API pathRequired
methodstringThe API HTTP methodRequired
useApiKeybooleanWhether the API requires API key or notfalse
queryParametersobjectA key value to map query string parameters to a boolean indicating if it's required or not
requestHeadersobjectA key value to map headers to a boolean indicating if it's required or not
authorizerstringThe authorizer configSee the official documentation
corsobject | booleanSee the official documentation
asyncbooleanWhether the API will execute as an async lambda or notfalse

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['apiLambdaProxy', {
			functionName: 'MyFunctionName',
			handler: 'path/to/my.handler',
			path: '/hello-world',
			method: 'get'
		}]
	]
});

Lambda Function (function)

(since 1.1.0)

Used to implement Lambda Functions

OptionTypeDescriptionAttributesDefault value
functionNamestringThe function nameRequired
handlerstringThe function handlerRequired
descriptionstringThe function description
eventsarrayobjectThe function events
layersarrayobjectAn array of function-level layers. This will override any provider-level layers (since 1.14.0)
addLayersarrayobjectAn array of function-level layers. This will be appended to any provider-level layers (since 1.14.0)
timeoutnumberThe function timeout
memorySizenumberThe function memorySize in MB (since 1.10.0)
reservedConcurrencynumberReserved concurrency limit for the function. By default, AWS uses account concurrency limit (since 1.11.0)
package.includearraystringThe List of paths of files to include
urlbooleanSet as true to create a Lambda URL resourcefalse
rawPropertiesobjectRaw properties to be setup in the function configuration object

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['function', {
			functionName: 'MyFunctionName',
			handler: 'path/to/my.handler',
			events: [
				{
					schedule: 'rate(1 hour)',
				},
				{
					s3: {
						bucket: 'myBucket',
						event: 's3:ObjectCreated:*',
						rules: [
							{ prefix: 'somePrefix' },
							{ suffix: 'someSuffix' }
						]
					}
				}
			],
			url: true,
			rawProperties: {
				rawProperties: 1
			}
		}]
	]
});

Environment variables (envVars)

(since 1.3.0)

Used to implement environment variables

Configuration options are the environment variables key-value object

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['envVars', {
			MY_VAR: 'and the value',
			SOME_OTHER_VAR: 'and some other value'
		}]
	]
});

Resource (resource)

(since 1.8.0)

Used to implement custom resources

OptionTypeDescriptionAttributesDefault value
namestringThe resource logical nameRequired
resourceobjectThe resource configuration object for CloudformationRequired

Example

const { helper } = require('sls-helper');

module.exports = helper({
	hooks: [
		['resource', {
			name: 'MyQueue',
			resource: {
				Type: 'AWS::SQS::Queue',
				Properties: {
					QueueName: 'my-super-queue'
				}
			}
		}]
	]
});
1.14.0

10 months ago

1.13.0

10 months ago

1.12.0

1 year ago

1.11.0

1 year ago

1.10.0

2 years ago

2.0.0-debug.0

2 years ago

2.0.1-debug.0

2 years ago

1.10.0-debug.0

2 years ago

1.10.1-debug.0

2 years ago

1.9.0

4 years ago

1.9.0-beta.0

4 years ago

1.8.0

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago