├── .DS_Store ├── LICENSE.txt ├── README.md ├── doc └── SAM.docx ├── images └── process_flow.png ├── json ├── create-person-table.json └── env.json ├── package ├── Person.zip └── setup.cfg ├── packaged.yml ├── process_flow.png ├── sam ├── src └── Person.py └── template.yml /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/.DS_Store -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sam-dynamodb-local 2 | A hands-on tutorial on using SAM with DynamoDB Local. 3 | 4 | ## Develop and test your serverless applications locally with DynamoDB local. 5 | 6 | This repository contains an AWS Lambda function that puts and gets a Person record (First name, Last name, Age) to/from a local DynamoDB instance. The process flow is as follows: 7 | 8 | ![Architecture](https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/master/process_flow.png) 9 | 10 | 11 | 12 | ### Development Environment Prerequisites 13 | - [Install AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) the credentials. 14 | - [Docker](https://runnable.com/docker/getting-started/) installed 15 | - [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) 16 | - Python [3.6/3.7](https://www.python.org/downloads/) with [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html) package 17 | - An AWS Account with permissions to IAM, CloudFormation, S3, Lambda, and DynamoDB (to deploy and test in a AWS region). 18 | 19 | ### Steps to deploy this application 20 | Please note: 21 | a. I will be using the us-east-1 region (Virginia) for deploying this application, but you may deploy to any region where Lambda and DynamoDB are available. 22 | b. User-specified input parameters will be marked with curly braces in the following instructions. For e.g., {s3-bucket-name} will need to be replaced with a bucket name of your choice, such as mybucketname etc. 23 | 24 | 25 | #### Steps 26 | 1. Clone this repo and change to the directory 'sam-dynamodb-local'. 27 | 28 | 2. Start DynamoDB Local by executing the following at the command prompt: 29 | *docker run -p 8000:8000 amazon/dynamodb-local* 30 | This will run the DynamoDB local in a docker container at port 8000. 31 | 32 | 3. At the command prompt, list the tables on DynamoDB Local by executing: 33 | *aws dynamodb list-tables --endpoint-url http://localhost:8000* 34 | 35 | 4. An output such as the one shown below confirms that the DynamoDB local instance has been installed and running: 36 | *{* 37 | *"TableNames": []* 38 | *}* 39 | 40 | 5. At the command prompt, create the PersonTable by executing: 41 | *aws dynamodb create-table --cli-input-json file://json/create-person-table.json --endpoint-url http://localhost:8000* 42 | 43 | **Note:** If you misconfigured your table and need to delete it, you may do so by executing the following command: 44 | *aws dynamodb delete-table --table-name PersonTable --endpoint-url http://localhost:8000* 45 | 46 | 6. At the command prompt, start the local API Gateway instance by executing: 47 | *sam local start-api --env-vars json/env.json* 48 | 49 | ### Testing the application 50 | 1. Insert a Person item in the table by executing the following CURL command at the prompt: 51 | *curl -d '{"FName": "Henry", "LName": "McKenna", "Age": 10}' http://127.0.0.1:3000/* 52 | 53 | This should output the partition key(GUID) of the item inserted into the local DynamoDB instance, such as: 54 | *12a72031-8b5c-4c76-8c90-83f3d5132088* 55 | 56 | 2. Let's retrieve the item from the local DynamoDB instance by executing the following CURL command at the prompt: 57 | *curl -X GET -d ‘{“Id”:”12a72031-8b5c-4c76-8c90-83f3d5132088”}’ http://127.0.0.1:3000* 58 | 59 | This will display the output as follows: 60 | *Henry McKenna 10* 61 | 62 | **Note:** You may alternatively use a GUI to view the items on the local DynamoDB instance, such as the [DynamoDB Local Shell](http://localhost:8000/shell/) or [DynamoDB Manager](https://github.com/YoyaTeam/dynamodb-manager). 63 | 64 | ### Deploying the application 65 | 1. Create a S3 bucket for storing SAM deployment artifacts in the us-east-1 region (or a region of your choosing). Please note that you may not use '-' or '.' in your bucket name. 66 | *aws s3 mb s3://{s3-bucket-name} --region us-east-1* 67 | 68 | 2. Create the Serverless Application Model package using CLI. 69 | *sam package \ 70 | --region us-east-1 \ 71 | --template-file template.yml \ 72 | --s3-bucket {s3-bucket-name} \ 73 | --output-template-file packaged.yml* 74 | 75 | 2. Deploy the packaged template. 76 | *aws cloudformation deploy \ 77 | --region us-east-1 \ 78 | --template-file packaged.yml \ 79 | --stack-name {stack_name} \ 80 | --capabilities CAPABILITY_IAM* 81 | 82 | 3. After the stack has been successfully created, you may test the application using the CURL commands as shown above. 83 | 84 | -------------------------------------------------------------------------------- /doc/SAM.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/doc/SAM.docx -------------------------------------------------------------------------------- /images/process_flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/images/process_flow.png -------------------------------------------------------------------------------- /json/create-person-table.json: -------------------------------------------------------------------------------- 1 | { 2 | "TableName": "PersonTable", 3 | "KeySchema": [ 4 | { "AttributeName": "Id", "KeyType": "HASH" } 5 | ], 6 | "AttributeDefinitions": [ 7 | { "AttributeName": "Id", "AttributeType": "S" } 8 | ], 9 | "ProvisionedThroughput": { 10 | "ReadCapacityUnits": 1, 11 | "WriteCapacityUnits": 1 12 | } 13 | } -------------------------------------------------------------------------------- /json/env.json: -------------------------------------------------------------------------------- 1 | { 2 | "Person": { 3 | "TABLE": "PersonTable", 4 | "REGION": "us-west-2", 5 | "AWSENV": "AWS_SAM_LOCAL", 6 | "DEVENV" : "OSX" 7 | } 8 | } -------------------------------------------------------------------------------- /package/Person.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/package/Person.zip -------------------------------------------------------------------------------- /package/setup.cfg: -------------------------------------------------------------------------------- 1 | [install] 2 | prefix= 3 | -------------------------------------------------------------------------------- /packaged.yml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Metadata: 3 | AWS::ServerlessRepo::Application: 4 | Author: Ganesh Shankaran 5 | Description: A simple PUT-GET implementation demonstrating the use of DynamoDB Local 6 | using SAM. 7 | HomePageUrl: https://github.com/ganshan/sam-dynamodb-local 8 | Labels: 9 | - sam-dynamodb-local 10 | LicenseUrl: s3://gshsar-ddblocal/86d3f3a95c324c9479bd8986968f4327 11 | Name: dynamodb-local-python 12 | ReadmeUrl: s3://gshsar-ddblocal/1ecfc496b744cb576fe71339567c0f8e 13 | SemanticVersion: 0.0.1 14 | SourceCodeUrl: https://github.com/ganshan/sam-dynamodb-local 15 | SpdxLicenseId: Apache-2.0 16 | Parameters: 17 | AWSENVNAME: 18 | Default: AWS_SAM_LOCAL 19 | Description: AWS Environment where code is being executed (AWS_SAM_LOCAL or AWS). 20 | Type: String 21 | DEVENVNAME: 22 | Default: OSX 23 | Description: Developer environment where code is being executed (OSX, Windows, 24 | or Linux). 25 | Type: String 26 | REGIONNAME: 27 | Default: us-east-1 28 | Description: Default region for deployment. 29 | Type: String 30 | TABLENAME: 31 | Default: PersonTable 32 | Description: The DynamoDB table for storing Person information. 33 | Type: String 34 | Resources: 35 | Person: 36 | Properties: 37 | CodeUri: s3://gshsar-ddblocal/9172a03776c8d3612d6e8c176a4680f4 38 | Description: A simple serverless application to demonstrate GET/PUT using DynamoDB 39 | Local. 40 | Environment: 41 | Variables: 42 | AWSENV: 43 | Ref: AWSENVNAME 44 | DEVENV: 45 | Ref: DEVENVNAME 46 | REGION: 47 | Ref: REGIONNAME 48 | TABLE: 49 | Ref: TABLENAME 50 | Events: 51 | GetPerson: 52 | Properties: 53 | Method: GET 54 | Path: / 55 | Type: Api 56 | PutPerson: 57 | Properties: 58 | Method: POST 59 | Path: / 60 | Type: Api 61 | Handler: Person.lambda_handler 62 | MemorySize: 128 63 | Policies: AmazonDynamoDBFullAccess 64 | Runtime: python3.6 65 | Timeout: 300 66 | Type: AWS::Serverless::Function 67 | PersonTable: 68 | Properties: 69 | AttributeDefinitions: 70 | - AttributeName: Id 71 | AttributeType: S 72 | KeySchema: 73 | - AttributeName: Id 74 | KeyType: HASH 75 | ProvisionedThroughput: 76 | ReadCapacityUnits: 1 77 | WriteCapacityUnits: 1 78 | TableName: 79 | Ref: TABLENAME 80 | Type: AWS::DynamoDB::Table 81 | Transform: AWS::Serverless-2016-10-31 82 | -------------------------------------------------------------------------------- /process_flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/process_flow.png -------------------------------------------------------------------------------- /sam: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganshan/sam-dynamodb-local/0460b480575deff77a4af4570281de4fb4da5331/sam -------------------------------------------------------------------------------- /src/Person.py: -------------------------------------------------------------------------------- 1 | import os 2 | import uuid 3 | import json 4 | import boto3 5 | import traceback 6 | from botocore.exceptions import ClientError 7 | 8 | def lambda_handler(event, context): 9 | 10 | # Get environment variables 11 | table_name = os.environ['TABLE'] 12 | region = os.environ['REGION'] 13 | aws_environment = os.environ['AWSENV'] 14 | dev_environment = os.environ['DEVENV'] 15 | 16 | # Check if executing locally or on AWS, and configure DynamoDB connection accordingly. 17 | if aws_environment == "AWS_SAM_LOCAL": 18 | # SAM LOCAL 19 | if dev_environment == "OSX": 20 | # Environment ins Mac OSX 21 | person_table = boto3.resource('dynamodb', endpoint_url="http://docker.for.mac.localhost:8000/").Table(table_name) 22 | 23 | elif dev_environment == "Windows": 24 | # Environment is Windows 25 | person_table = boto3.resource('dynamodb', endpoint_url="http://docker.for.windows.localhost:8000/").Table(table_name) 26 | 27 | else: 28 | # Environment is Linux 29 | person_table = boto3.resource('dynamodb', endpoint_url="http://127.0.0.1:8000").Table(table_name) 30 | else: 31 | # AWS 32 | person_table = boto3.resource('dynamodb', region_name=region).Table(table_name) 33 | 34 | 35 | # Print statement for debugging. 36 | #print(event) 37 | 38 | # Load body JSON for processing 39 | try: 40 | bodydict = json.loads(event['body']) 41 | except: 42 | return {'statusCode': 400, 'body': 'malformed JSON'} 43 | 44 | # GET Method 45 | if event['httpMethod'] == 'GET': 46 | print("In GET method") 47 | 48 | try: 49 | response = person_table.get_item( 50 | Key={'Id': bodydict['Id']} 51 | ) 52 | except ClientError as e: 53 | print(e.response['Error']['Message']) 54 | return {'statusCode': 400, 'body': e.response['Error']['Message']} 55 | else: 56 | item = response['Item'] 57 | print("GetItem succeeded:") 58 | resp = item['FirstName'] + ' ' + item['LastName'] + ' ' + str(item['Age']) 59 | return {'statusCode': 200, 'body': resp} 60 | 61 | # POST Method 62 | elif event['httpMethod'] == 'POST': 63 | # Check for missing information in the request. 64 | if ('FName' not in event['body']) or not(bodydict["FName"]): 65 | return {'statusCode': 400, 'body': 'missing person first name.'} 66 | if ('LName' not in bodydict) or not(bodydict["LName"]): 67 | return {'statusCode': 400, 'body': 'missing person last name.'} 68 | if ('Age' not in event['body']) or not(bodydict["Age"]): 69 | return {'statusCode': 400, 'body': 'missing person age.'} 70 | 71 | # Write to DynamoDB table 72 | # Generate ID for the item to be put 73 | PersonId = str(uuid.uuid4()) 74 | 75 | # Put item in the DynamoDB table 76 | try: 77 | person_table.put_item( 78 | Item={ 79 | 'Id': PersonId, 80 | 'FirstName': bodydict["FName"], 81 | 'LastName': bodydict["LName"], 82 | 'Age': bodydict["Age"] 83 | } 84 | ) 85 | except: 86 | traceback.print_exc() 87 | return {'statusCode': 400, 'body': 'Error in putting item.'} 88 | else: 89 | print ('Item inserted') 90 | # Echo back the Person ID as success message. 91 | return {'statusCode': 200, 'body': PersonId} 92 | 93 | -------------------------------------------------------------------------------- /template.yml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion : '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | 4 | Metadata: 5 | AWS::ServerlessRepo::Application: 6 | Name: "dynamodb-local-python" 7 | Description: "A simple PUT-GET implementation demonstration use of DynamoDB Local using SAM." 8 | Author: "Ganesh Shankaran" 9 | SpdxLicenseId: "Apache-2.0" 10 | LicenseUrl: "LICENSE.txt" 11 | ReadmeUrl: "README.md" 12 | Labels: ['sam-dynamodb-local'] 13 | HomePageUrl: https://github.com/ganshan/sam-dynamodb-local 14 | SemanticVersion: 0.0.1 15 | SourceCodeUrl: https://github.com/ganshan/sam-dynamodb-local 16 | 17 | Parameters: 18 | TABLENAME: 19 | Type: String 20 | Description: The DynamoDB table for storing Person information. 21 | Default: 'PersonTable' 22 | 23 | REGIONNAME: 24 | Type: String 25 | Description: Default region for deployment. 26 | Default: 'us-east-1' 27 | 28 | AWSENVNAME: 29 | Type: String 30 | Description: AWS Environment where code is being executed (AWS_SAM_LOCAL or AWS). 31 | Default: 'AWS_SAM_LOCAL' 32 | 33 | DEVENVNAME: 34 | Type: String 35 | Description: Developer environment where code is being executed (OSX, Windows, or Linux). 36 | Default: 'OSX' 37 | 38 | 39 | Resources: 40 | PersonTable: 41 | Type: AWS::DynamoDB::Table 42 | Properties: 43 | TableName: !Ref TABLENAME 44 | AttributeDefinitions: 45 | - AttributeName: Id 46 | AttributeType: S 47 | KeySchema: 48 | - AttributeName: Id 49 | KeyType: HASH 50 | ProvisionedThroughput: 51 | ReadCapacityUnits: 1 52 | WriteCapacityUnits: 1 53 | 54 | Person: 55 | Type: "AWS::Serverless::Function" 56 | Properties: 57 | Runtime: python3.6 58 | Handler: Person.lambda_handler 59 | CodeUri: ./src 60 | MemorySize: 128 61 | Timeout: 300 62 | Description: 'A simple serverless application to demonstrate GET/PUT using DynamoDB Local.' 63 | Policies: AmazonDynamoDBFullAccess 64 | Environment: 65 | Variables: 66 | TABLE: !Ref TABLENAME 67 | REGION: !Ref REGIONNAME 68 | AWSENV: !Ref AWSENVNAME 69 | DEVENV: !Ref DEVENVNAME 70 | 71 | Events: 72 | PutPerson: 73 | Type: Api 74 | Properties: 75 | Path: / 76 | Method: POST 77 | 78 | GetPerson: 79 | Type: Api 80 | Properties: 81 | Path: / 82 | Method: GET --------------------------------------------------------------------------------