Giter Club home page Giter Club logo

aws-lambda-sam-demo's Introduction

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

aws-lambda-sam-demo's People

Contributors

lvthillo avatar neilkuan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.