Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for defining timeToLiveSpecification as part of table … #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ inputs:
KeyType: HASH
Projection:
ProjectionType: 'ALL'
timeToLiveSpecification: # (optional)
AttributeName: attribute2
Enabled: false
region: us-east-1
```

Expand Down
14 changes: 13 additions & 1 deletion src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const { mergeDeepRight, pick } = require('ramda');
const AWS = require('aws-sdk');
// eslint-disable-next-line import/no-unresolved
const { Component } = require('@serverless/core');
const { log, createTable, deleteTable, describeTable, updateTable } = require('./utils');
const {
log,
createTable,
deleteTable,
describeTable,
updateTable,
updateTimeToLive,
} = require('./utils');

const outputsList = ['name', 'arn', 'region'];

Expand All @@ -27,6 +34,7 @@ const defaults = {
name: null,
region: 'us-east-1',
deletionPolicy: 'delete',
timeToLiveSpecification: undefined,
};

class AwsDynamoDb extends Component {
Expand Down Expand Up @@ -88,6 +96,10 @@ class AwsDynamoDb extends Component {
await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config });
}

if (config.timeToLiveSpecification) {
await updateTimeToLive({ dynamodb, ...config });
}

log(`Table ${config.name} was successfully deployed to the ${config.region} region.`);

this.state.arn = config.arn;
Expand Down
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,28 @@ async function deleteTable({ dynamodb, name }) {
return !!res;
}

async function updateTimeToLive({ dynamodb, name, timeToLiveSpecification = {} }) {
return await dynamodb
.waitFor('tableExists', { TableName: name }, async (err) => {
if (err) throw err;
return await dynamodb
.updateTimeToLive({
TableName: name,
TimeToLiveSpecification: {
AttributeName: timeToLiveSpecification.AttributeName,
Enabled: timeToLiveSpecification.Enabled,
},
})
.promise();
})
.promise();
}

module.exports = {
log,
createTable,
describeTable,
updateTable,
deleteTable,
updateTimeToLive,
};
2 changes: 1 addition & 1 deletion templates/aws-dynamodb-starter/serverless.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ org: serverlessinc
description: Deploys a serverless NoSQL database powered by AWS DynamoDB
keywords: aws, serverless, nosql, database
repo: https://github.com/serverless-components/aws-dynamodb
license: MIT
license: MIT
6 changes: 6 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const instanceYaml = {
stage: 'dev',
inputs: {
deletionPolicy: 'delete',
timeToLiveSpecification: {
AttributeName: 'expires',
Enabled: true,
},
attributeDefinitions: [
{
AttributeName: 'attribute1',
Expand Down Expand Up @@ -75,6 +79,8 @@ it('should successfully deploy dynamodb table and local index', async () => {

const res = await getTable(credentials, name);

// const resTTL = await getTableTimeToLive(credentials, name);

expect(instance.outputs.name).toBeDefined();
expect(instance.outputs.arn).toBeDefined();
expect(res.Table.AttributeDefinitions.length).toEqual(2);
Expand Down
30 changes: 28 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getCredentials = () => {
};

if (!credentials.aws.accessKeyId || !credentials.aws.accessKeyId) {
throw new Error('Unable to run tests. AWS credentials not found in the envionrment');
throw new Error('Unable to run tests. AWS credentials not found in the environment');
}

return credentials;
Expand Down Expand Up @@ -67,4 +67,30 @@ const getTable = async (credentials, tableName) => {
.promise();
};

module.exports = { sleep, generateId, getCredentials, getServerlessSdk, getTable };
/*
* Fetches a DynamoDB timeToLive for a table from aws for validation
* @param ${object} credentials - the cross provider credentials object
* @param ${string} tableName - the name of the dynamodb table
*/
const getTableTimeToLive = async (credentials, tableName) => {
const config = {
credentials: credentials.aws,
region: 'us-east-1',
};
const dynamodb = new AWS.DynamoDB(config);

return dynamodb
.describeTimeToLive({
TableName: tableName,
})
.promise();
};

module.exports = {
sleep,
generateId,
getCredentials,
getServerlessSdk,
getTable,
getTableTimeToLive,
};