Skip to content

Commit

Permalink
Add API key and read table name from env var
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoychev committed Jun 15, 2019
1 parent ded6a10 commit d701997
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ service: gym101
provider:
name: aws
runtime: python3.7
#stage: dev
#region: us-east-1
apiKeys:
- ${opt:stage, self:provider.stage}-myFirstKey
- ${opt:stage, self:provider.stage}-mySecondKey
environment:
DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
iamRoleStatements:
Expand All @@ -11,11 +16,6 @@ provider:
- dynamodb:Query
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

# you can overwrite defaults here
# stage: dev
# region: us-east-1


functions:
getExercises:
handler: src.exercises.get_exercises
Expand All @@ -24,6 +24,7 @@ functions:
- http:
path: excercises
method: get
private: true

resources:
Resources:
Expand Down
4 changes: 3 additions & 1 deletion src/exercises.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import boto3
from boto3.dynamodb.conditions import Key

Expand All @@ -9,8 +10,9 @@ def get_exercises(event, context):
equipment = queryStringParameters['equipment']
print(equipment)

table_name = os.environ['DYNAMODB_TABLE']
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('gym101-dev')
table = dynamodb.Table(table_name)

response = table.query(
KeyConditionExpression=Key('equipment').eq(equipment)
Expand Down

0 comments on commit d701997

Please sign in to comment.