Skip to content

Commit 5012a6a

Browse files
address PR feedback
1 parent b33cd7b commit 5012a6a

File tree

14 files changed

+30
-20
lines changed

14 files changed

+30
-20
lines changed

starters/serverless-framework-sqs-dynamodb/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
IS_OFFLINE=true
33
# connection info for Redis used for caching
44
REDIS_CACHE_URL=redis://:sOmE_sEcUrE_pAsS@localhost:6379
5+
# how long should items last in the cache by default
6+
DEFAULT_CACHE_TIME=300000

starters/serverless-framework-sqs-dynamodb/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ git clone https://github.com/thisdot/starter.dev.git
6060

6161
This README uses `npm` for commands. If you're using `yarn` or `pnpm`, utilize the equivalent version of the commands.
6262

63-
1. Create a `.env` file:
63+
1. Create a `.env` file. This is to support any variable in the Serverless Configuration being read from `env:` and test running:
6464

6565
```bash
6666
cp .env.example .env
@@ -92,11 +92,11 @@ npm start
9292

9393
### General Commands
9494

95-
- `build` bundles the project using the serverless packaging serverless. The produced artifacts will ship bundles shipped to AWS on deployment. You can optionally pass `--analyze <function name>` to run the bundle analyzer and visualize the results to understand your handler bundles.
95+
- `build` bundles the project using the Serverless Framework's out of the box `package` command. The produced artifacts will be shipped to AWS on deployment. You can optionally pass `--analyze <function name>` to run the bundle analyzer and visualize the results to understand your handler bundles.
9696
- `deploy` ships the project to the configured AWS account using the Serverless Framework CLI command.
9797
- `start` runs the `serverless-offline` provided server for local development and testing. Be sure to have the local docker infrastructure running to emulate the related services.
9898
- `test` runs `jest` under the hood.
99-
- `lint` runs `eslint` under the hood. You can use all the eslint available command line arguments. To lint the entire project, run `npm run lint .`, or equivalent. You can affix `--fix` to auto-correct linting issues that eslint can handle.
99+
- `lint` runs `eslint` under the hood. You can use all the available eslint command line arguments. To lint the entire project, run `npm run lint .`, or equivalent. You can affix `--fix` to auto-correct linting issues that eslint can handle.
100100
- `format:check` runs prettier format checking on all project files.
101101
- `format:write` runs prettier format writing on all project files.
102102

@@ -141,7 +141,7 @@ This starter kit ships with a set of RESTful APIs. All routes are served via `ht
141141

142142
## Serverless Configuration
143143

144-
This kit uses the TypeScript option for configuration. It is type checked using the `@serverless/typescript` definitions over the DefinitelyTyped definitions because DefinitelyTyped is currently behind on its definition. However, the `@serverless/typescript` types have known issues with certain fields are noted directly in the configuration.
144+
This kit uses the TypeScript option for configuration. It is type checked using the `@serverless/typescript` definitions over the DefinitelyTyped definitions because DefinitelyTyped is currently behind on its definition. However, the `@serverless/typescript` types have known issues with certain fields, which are noted directly in the configuration.
145145

146146
It is not compatible with the automated CI/CD of the Serverless Dashboard as they only support the default YAML format. You can read more about [setting up CI/CD using GitHub for this project on the This Dot blog](https://www.thisdot.co/blog/github-actions-for-serverless-framework-deployments).
147147

@@ -227,6 +227,8 @@ There is an existing [DynamoDB Local preset for Jest](https://github.com/shelfio
227227

228228
## Deployment
229229

230+
<!-- TODO: add note about AWS setup -->
231+
230232
As a serverless implementation, most of the infrastructure will be deployed and configured correctly simply utilizing the `deploy` script provided by this kit which is just an alias for [`serverless deploy`](https://www.serverless.com/framework/docs/providers/aws/cli-reference/deploy). However, the Redis instance is not configurable via the Serverless Configuration and will need to be set up ahead of your first deploy and configured via environment variables. We recommend using [Serverless Framework's interface for AWS Secret Manager](https://www.serverless.com/blog/aws-secrets-management/) for security purposes.
231233

232234
This entire stack can be deployed via CI tools such as GitHub Actions, CircleCI, etc. and is our recommended approach as this kit is incompatible with the Serverless Dashboard. The Serverless Dashboard CI only works with the configuration in the YAML format which we do not use to give developers type-safety in the config file.

starters/serverless-framework-sqs-dynamodb/serverless.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ const serverlessConfiguration: AWS = {
9898
environment: {
9999
REGION: '${aws:region}',
100100
SLS_STAGE: '${sls:stage}',
101-
// DynamoDB Tables
101+
DEFAULT_CACHE_TIME: '${env:DEFAULT_CACHE_TIME}',
102102
REDIS_CACHE_URL: '${env:REDIS_CACHE_URL}',
103+
// DynamoDB Tables
103104
TECHNOLOGIES_TABLE: '${param:technologiesTable}',
104105
},
105106
iam: {

starters/serverless-framework-sqs-dynamodb/src/handlers/example_job_processor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('demo', () => {
66
let logMock: jest.SpyInstance;
77

88
beforeAll(async () => {
9-
logMock = jest.spyOn(console, 'log').mockImplementation(() => ({}));
9+
logMock = jest.spyOn(console, 'log').mockImplementation(() => {});
1010
subject = await handler(
1111
{
1212
Records: [

starters/serverless-framework-sqs-dynamodb/src/handlers/example_stream_processor.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ describe('demo', () => {
66
let logMock: jest.SpyInstance;
77

88
beforeAll(async () => {
9-
logMock = jest.spyOn(console, 'log').mockImplementation(() => ({}));
9+
logMock = jest.spyOn(console, 'log').mockImplementation(() => {});
1010
subject = await handler(
1111
{
1212
Records: [
13+
{
14+
eventName: 'UNKNOWN',
15+
},
1316
{
1417
eventName: 'INSERT',
1518
dynamodb: {

starters/serverless-framework-sqs-dynamodb/src/handlers/example_stream_processor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import type { DynamoDBStreamHandler, DynamoDBRecord } from 'aws-lambda';
22

33
const recordHandler = async (record: DynamoDBRecord) => {
4-
if (record.eventName === 'INSERT' && record.dynamodb) {
4+
if (!record.dynamodb) {
5+
return;
6+
}
7+
8+
if (record.eventName === 'INSERT') {
59
console.log('Inserted Record', record.dynamodb.NewImage);
6-
} else if (record.eventName === 'MODIFY' && record.dynamodb) {
10+
} else if (record.eventName === 'MODIFY') {
711
console.log('Updated Record');
812
console.log('New Values', record.dynamodb.NewImage);
913
console.log('Old Values', record.dynamodb.OldImage);
10-
} else if (record.eventName === 'REMOVE' && record.dynamodb) {
14+
} else if (record.eventName === 'REMOVE') {
1115
console.log('Removed Record', record.dynamodb.OldImage);
1216
}
1317
};

starters/serverless-framework-sqs-dynamodb/src/handlers/generate_job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { APIGatewayProxyHandler } from 'aws-lambda';
2+
import { StatusCodes } from 'http-status-codes';
23
import { sendMessage } from '@/utils/sqs/sendMessage';
34

45
export const handler: APIGatewayProxyHandler = async () => {
@@ -8,7 +9,7 @@ export const handler: APIGatewayProxyHandler = async () => {
89
});
910

1011
return {
11-
statusCode: resp.success ? 201 : 400,
12+
statusCode: resp.success ? StatusCodes.CREATED : StatusCodes.BAD_REQUEST,
1213
body: JSON.stringify(resp.data),
1314
};
1415
};

starters/serverless-framework-sqs-dynamodb/src/handlers/technology_create.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import type { APIGatewayProxyResult, APIGatewayProxyEvent, Context, Callback } f
22
import { PutItemCommand, ServiceInputTypes, ServiceOutputTypes } from '@aws-sdk/client-dynamodb';
33
import { AwsStub, mockClient } from 'aws-sdk-client-mock';
44
import { getClient } from '@/utils/dynamodb/getClient';
5-
import { removeFromCache } from '@/utils/cache/removeFromCache';
65
import * as technologyCreate from '@/models/technology/create';
7-
import { getCacheKey } from '@/models/technology/getCacheKey';
86
import { handler } from './technology_create';
97

108
describe('POST /technology', () => {
@@ -33,7 +31,6 @@ describe('POST /technology', () => {
3331

3432
afterAll(async () => {
3533
ddbMock.restore();
36-
await removeFromCache(getCacheKey('87af19b1-aa0d-4178-a30c-2fa8cd1f2cff'));
3734
});
3835

3936
it('returns 201 status', () => {
@@ -63,7 +60,6 @@ describe('POST /technology', () => {
6360

6461
afterAll(async () => {
6562
jest.restoreAllMocks();
66-
await removeFromCache(getCacheKey('87af19b1-aa0d-4178-a30c-2fa8cd1f2cff'));
6763
});
6864

6965
it('returns 400 status', () => {
@@ -97,7 +93,6 @@ describe('POST /technology', () => {
9793

9894
afterAll(async () => {
9995
jest.restoreAllMocks();
100-
await removeFromCache(getCacheKey('87af19b1-aa0d-4178-a30c-2fa8cd1f2cff'));
10196
});
10297

10398
it('returns 500 status', () => {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { scan } from '@/utils/dynamodb/scan';
22

33
export const getAll = async () => {
4-
const items = await scan(process.env.TECHNOLOGIES_TABLE);
5-
return items;
4+
return await scan(process.env.TECHNOLOGIES_TABLE);
65
};

starters/serverless-framework-sqs-dynamodb/src/types/environment.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare global {
44
REGION: string;
55
SLS_STAGE: string;
66

7+
DEFAULT_CACHE_TIME: string;
78
REDIS_CACHE_URL: string;
89
TECHNOLOGIES_TABLE: string;
910
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const DEFAULT_CACHE_TIME = 300_000; // 5 minutes = 1000 ms/s * 60 s/min * 5 min
1+
export const DEFAULT_CACHE_TIME = parseInt(process.env.DEFAULT_CACHE_TIME, 10);

starters/serverless-framework-sqs-dynamodb/src/utils/dynamodb/deleteItem.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('dynamodb.deleteItem()', () => {
4242

4343
describe('when item does not exist', () => {
4444
beforeAll(async () => {
45+
jest.spyOn(console, 'warn').mockImplementation(() => {});
4546
jest.spyOn(console, 'error').mockImplementation(() => {});
4647
ddbMock.on(DeleteItemCommand).resolves({
4748
Attributes: undefined,

starters/serverless-framework-sqs-dynamodb/src/utils/dynamodb/deleteItem.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const deleteItem = async (tableName: string, key: Record<string, unknown>
1515
const response = await client.send(command);
1616

1717
if (!response || !response.Attributes) {
18+
console.warn('dynamodb.deleteItem Warning - ${response}');
1819
return null;
1920
}
2021
return unmarshall(response.Attributes);

starters/serverless-framework-sqs-dynamodb/src/utils/isOffline/isOffline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Utility function for checking where functions are being run locally via serverless offline
2+
* Utility function for checking if functions are being run locally via serverless offline
33
* or if they're running on infrastructure. Helpful for detecting which connection string to use.
44
*
55
* @returns boolean are we running locally or on infra?

0 commit comments

Comments
 (0)