lvthillo / aws-lambda-sam-demo

Demo project which uses SAM and AWS lambda to test lambdas locally

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aws-lambda-sam-demo

Demo project which uses SAM and AWS lambda to test lambdas locally. A company has a lot of documents. The documents have a unique ID. It's also possible to have multiple versions of one document. The employees of the company have issues to find out where a certain version of a certain document is stored. To solve this the company has developed a solution to make it easy to query the storage location of a document.

SAM(2)

local setup

Deploy DynamoDB in Docker

$ docker network create sam-demo
$ docker run --network sam-demo --name dynamodb -d -p 8000:8000 amazon/dynamodb-local
$ aws dynamodb create-table --table-name documentTable --attribute-definitions AttributeName=documentId,AttributeType=N AttributeName=versionId,AttributeType=S --key-schema AttributeName=documentId,KeyType=HASH AttributeName=versionId,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --endpoint-url http://localhost:8000

Build SAM application

$ sam build --use-container

Invoke load data function to load data in DynamoDB and verify

$ sam local invoke LoadDataFunction --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=DDBTableName,ParameterValue=documentTable --docker-network sam-demo
$ aws dynamodb scan --table-name documentTable --endpoint-url http://localhost:8000

Start local API Gateway

$ sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=DDBTableName,ParameterValue=documentTable --docker-network sam-demo

Test with curl

$ curl "http://127.0.0.1:3000/document?documentId=1044&versionId=v_1"
{"message": {"location": "s3://bucket-a/8853806831.doc"}}

Open the static web application

AWS setup

Create your s3-bucket in the your default region. Deploy the application using SAM CLI

$ sam build --use-container
$ sam deploy --template-file .aws-sam/build/template.yaml --s3-bucket xxx-bucket  --parameter-overrides ParameterKey=Environment,ParameterValue=aws ParameterKey=DDBTableName,ParameterValue=documentTable --stack-name aws-lambda-sam-demo --capabilities CAPABILITY_NAMED_IAM

Copy the static resources to the newly created S3 bucket

$ aws s3 sync app s3://${bucket_name_via_sam_deploy_created} --acl public-read

--- 
upload: app/script.js to s3://${bucket_name_via_sam_deploy_created}/script.js
upload: app/index.html to s3://${bucket_name_via_sam_deploy_created}/index.html

Cleanup:

$ aws s3 rm s3://${bucket_name_via_sam_deploy_created} --recursive

--- 
delete: s3://${bucket_name_via_sam_deploy_created}/index.html
delete: s3://${bucket_name_via_sam_deploy_created}/script.js

Delete stack

$ aws cloudformation delete-stack --stack-name aws-lambda-sam-demo

About

Demo project which uses SAM and AWS lambda to test lambdas locally

License:Apache License 2.0


Languages

Language:Python 74.6%Language:JavaScript 13.2%Language:HTML 12.2%