Skip to content

Commit

Permalink
Pass table and index names from serverless config to Lambda env
Browse files Browse the repository at this point in the history
  • Loading branch information
epiphone committed Jan 2, 2020
1 parent e8c18aa commit 63bcdde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Original version (see [branch](https://github.com/epiphone/serverless-url-shorte
- [ ] Integration tests: try running integration tests locally against `serverless-offline` and `serverless-dynamodb-local`
- [x] ~~Smarter hourly time-series aggregation as opposed to the naive full-table scan~~
- [ ] CI/CD: build, run tests and lint on GitHub actions + run `sls deploy` if on `master` branch
- [ ] Set DynamoDB table names as Serverless Framework config variables and pass to Lambda handlers as env vars to avoid hardcoding table names to code
- [x] ~~Set DynamoDB table names as Serverless Framework config variables and pass to Lambda handlers as env vars to avoid hardcoding table names to code~~
- [ ] Tune the Lambda IAM policy to avoid overly wide permissions

## Dependencies
Expand Down
5 changes: 2 additions & 3 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { APIGatewayProxyHandler } from 'aws-lambda' // tslint:disable-line:no-im
import * as AWS from 'aws-sdk'
import * as shortId from 'shortid'

// TODO use env vars:
const URLS_TABLE = 'urls'
const URL_INDEX = 'UrlIndex'
const URLS_TABLE = process.env.URLS_TABLE_NAME!
const URL_INDEX = process.env.URL_INDEX_NAME!

const dynamoDb = process.env.IS_OFFLINE
? new AWS.DynamoDB.DocumentClient({
Expand Down
8 changes: 6 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ service:
name: url-shortener

custom:
urls_table_name: urls
url_index_name: UrlIndex
dynamodb:
stages:
- dev
Expand All @@ -20,6 +22,8 @@ provider:
minimumCompressionSize: 1024 # Enable gzip compression for responses > 1 KB
environment:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1
URLS_TABLE_NAME: ${self:custom.urls_table_name}
URL_INDEX_NAME: ${self:custom.url_index_name}
iamRoleStatements:
- Effect: Allow
Action:
Expand Down Expand Up @@ -65,7 +69,7 @@ resources:
urlsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: urls
TableName: ${self:custom.urls_table_name}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
Expand All @@ -78,7 +82,7 @@ resources:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
GlobalSecondaryIndexes:
- IndexName: UrlIndex
- IndexName: ${self:custom.url_index_name}
KeySchema:
- AttributeName: url
KeyType: HASH
Expand Down

0 comments on commit 63bcdde

Please sign in to comment.