1.0.6 • Published 4 years ago

slm-aws-tooling v1.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

NPM Package containing utility functions for interaction with AWS services from within other AWS services.

If used on more than one lambda, consider adding this package to an AWS Layer and adding that layer to your Lambda function, instead of adding this package to each lambda individually. This will reduce bundlesize and make versioning much easier.


S3

listBuckets

Async function that returns a list of buckets belonging to the current account / organisation.

Execution

import { S3 } from 'slm-aws-tooling'

const data = await S3.listBuckets()
console.log(data)	// [{ bucketName: '<bucketName>', created: '<creationDate>' }]

listBucketObjects

Async function that returns a list of the objects stored in the specified bucket

Parameters

NameTypeExampleDescription
bucketNameStringmyBucketNameThe name of the target S3 Bucket
maxObjects (optional)Number5The maximum amount of objects listed

Execution

import { S3 } from 'slm-aws-tooling'

const data = await S3.listBucketObjects('myBucket')

Response

[{
	"name": "data.zip",
	"lastModified": "21-02-202",
	"size": "2kb",
	"storage": "standard",
	"owner": "SLM"
}]

readFromBucket

Function responsible for reading a single object from the specified S3 Bucket. Returns the content as a Buffer.

Parameters

NameTypeExampleDescription
bucketNameStringmyBucketNameThe name of the target S3 Bucket
objectKeyStringdata.jsonThe name of the target object, including file extension

Execution

import { S3 } from 'slm-aws-tooling'

const data = await S3.readFromBucket('myBucketName', 'data.json')
console.log(data)	// Contents of file "data.json" in Bucket "myBucketName"

writeToBucket

Async function responsible for writing the provided data to a file inside the specified S3 Bucket. IMPORTANT - If the file already exists, it is overwritten.

Parameters

NameTypeExampleDescription
bucketNameStringmyBucketNameThe name of the target S3 Bucket
objectKeyStringdata.jsonThe name of the target object, including file extension
dataany"I love Javascript"The content that is written into the file

Execution

import { S3 } from 'slm-aws-tooling'

const data = await S3.writeToBucket(
	'myBucketName',
	'data.json',
	JSON.stringify({ message: 'My name is Slim Shady' }
)

deleteFromBucket

Async function responsible for deleting an object with the specified key from the specified S3 Bucket.

Parameters

NameTypeExampleDescription
bucketNameStringmyBucketNameThe name of the target S3 Bucket
objectKeyStringdata.jsonThe name of the target object, including file extension

Execution

import { S3 } from 'slm-aws-tooling'

const data = await S3.deleteFromBucket('myBucketName', 'data.json')
console.log(data)	// [{ bucketName: '<bucketName>', created: '<creationDate>' }]

SecretsManager

listSecrets

Async function that returns a list of secrets available in the provided region

Parameters

NameTypeExampleDescription
region (optional)Stringeu-north-1The targeted AWS region. Defaults to eu-north-1

Execution

import { SecretsManager } from 'slm-aws-tooling'

const data = await SecretsManager.listSecrets()

Response

[{
	"name": "API_SECRETS",
	"description": "Secret that holds our API secrets",
	"arn": "<secret ARN>",
	"lastAccessed": "<lastaccessedDates>"
}]

readSecret

Async function that returns the requested secret as a JSON string

NameTypeExampleDescription
secretIdStringAPI_SECRETSThe ID / Name of the secret requested
region (optional)Stringeu-north-1The targeted AWS region. Defaults to eu-north-1

Execution

import { SecretsManager } from 'slm-aws-tooling'

const data = await SecretsManager.readSecret('API_SECRETS')
console.log(JSON.parse(data)) // { API_KEY: 'aqwcs23rqh2', API_URL: 'https://swapi.co/' }

Response

[{
	"API_KEY": "aqwcs23rqh2",
	"API_URL": "https://swapi.co/"
}]

DynamoDB

getTableData

Returns data about the specified table, if it exists

Parameters

NameTypeExampleDescription
tableNameStringmyTableThe name of thet targeted DynamoDB Table

Execution

import { DynamoDB } from 'slm-aws-tooling'

const data = await DynamoDB.getTableData('tableName')

getItemsFromTable

Queries the specified table for items matching the provided query and returns them in an array

Parameters

NameTypeExampleDescription
tableNameStringusersThe name of thet targeted DynamoDB Table
queryobjectjavascript { userIsActive: true }The query to filter items by

Execution

import { DynamoDB } from 'slm-aws-tooling'

const data = await DynamoDB.getItemsFromTable('users', { userIsActive: true })
console.log(data)	// [{ userId: '123', userIsActive: true }, { userId: '4332', userIsActive: true }]

getSingleItemFromTable

Queries the specified table for an item matching the provided query and returns it

Parameters

NameTypeExampleDescription
tableNameStringusersThe name of thet targeted DynamoDB Table
queryobjectjavascript { userIsActive: true }The query to filter items by

Execution

import { DynamoDB } from 'slm-aws-tooling'

const data = await DynamoDB.getSingleItemFromTable('users', { userIsActive: true })
console.log(data)	// [{ userId: '123', userIsActive: true }]

putItemstoTable

Queries the specified table for an item matching the provided query and returns it

Parameters

NameTypeExampleDescription
tableNameStringtagsThe name of thet targeted DynamoDB Table
dataobjectjavascript { tagId: 'x-im-tag-12', tagName: 'polis' }The item to be written into the table

Execution

import { DynamoDB } from 'slm-aws-tooling'

try {
	await DynamoDB.putItemstoTable('tags', { tagId: 'x-im-tag-12', tagName: 'polis' })
} catch (err) {
	console.log('Something went wrong')
}

deleteItemsFromTable

Queries the specified table for an item matching the provided query and returns it

Parameters

NameTypeExampleDescription
tableNameStringtagsThe name of thet targeted DynamoDB Table
queryobject Arrayjavascript { tagId: 'x-im-tag-12', tagName: 'polis' }A list of filter objects to match and delete

Execution

import { DynamoDB } from 'slm-aws-tooling'

try {
	await DynamoDB.deleteItemsFromTable('tags', [{ tagId: 'x-im-tag-12', tagName: 'polis' }])
} catch (err) {
	console.log('Something went wrong')
}
1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago