SAM Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk'); | |
module.exports.create = (event, context, callback) => { | |
const timestamp = new Date().getTime(); | |
const data = JSON.parse(event.body); | |
var params = { | |
TableName: "customerTable", | |
Item: { | |
"customer_id": data.customer_id, | |
"created_timestamp": timestamp | |
} | |
}; | |
var dynamodbDocClient = new AWS.DynamoDB.DocumentClient(); | |
dynamodbDocClient.put(params, function(err, data) { | |
if (err) callback(Error(err)) | |
else callback(null, {statusCode: 200, body: JSON.stringify(params)}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sam-app", | |
"version": "1.0.0", | |
"description": "Sam App", | |
"main": "create.js", | |
"license": "MIT" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: 'AWS::Serverless-2016-10-31' | |
Description: A microservice using AWS SAM, API Gateway, Lambda, and DynamoDB | |
Resources: | |
newCustomer: | |
Type: 'AWS::Serverless::Function' | |
Properties: | |
Handler: create.create | |
Runtime: nodejs8.10 | |
CodeUri: . | |
Description: Used to create a new customer in the database | |
MemorySize: 512 | |
Timeout: 10 | |
Policies: | |
- Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'dynamodb:PutItem' | |
Resource: | |
'Fn::Join': | |
- '' | |
- - 'arn:aws:dynamodb:' | |
- Ref: 'AWS::Region' | |
- ':' | |
- Ref: 'AWS::AccountId' | |
- ':table/customerTable' | |
Events: | |
Api1: | |
Type: Api | |
Properties: | |
Path: /create | |
Method: POST | |
customerTable: | |
Type: 'AWS::DynamoDB::Table' | |
Properties: | |
TableName: customerTable | |
AttributeDefinitions: | |
- AttributeName: customer_id | |
AttributeType: S | |
KeySchema: | |
- AttributeName: customer_id | |
KeyType: HASH | |
ProvisionedThroughput: | |
ReadCapacityUnits: 1 | |
WriteCapacityUnits: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment