How to setup AWS Server Application Model (AWS SAM)

enter image description here

Here is a quick overwiew on how to setup and run a single page applciation in AWS Lambda using AWS SAM using Pyhton Dash.

Dash is an open-source Python framework used for building analytical web applications. It is a powerful library that simplifies the development of data-driven applications. It's especially useful for Python data scientists who aren't very familiar with web development.

Dash is simple enough that you can bind a user interface to your code in less than 10 minutes.

Dash apps are rendered in the web browser. You can deploy your apps to VMs or Kubernetes clusters and then share them through URLs. Since Dash apps are viewed in the web browser, Dash is inherently cross-platform and mobile ready.

There is a lot behind the framework. To learn more about how it is built and what motivated Dash, read our announcement letter or our post Dash is React for Python.

How to setup in local Linux/Windows environment:

Prerequisite for build and test AWS SAM in local environment

•AWS account •Configure AWS Identity and Access Management (IAM) and AWS credentials •Docker – Only for testing application in local environment •SAM CLI Online documentation URL:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-linux.html

Docker installation in ubuntu:

  • https://docs.docker.com/engine/install/ubuntu/
  • Other Linux flavors: https://docs.docker.com/engine/install/

Install AWS SAM CLI:

  • Download SAM CLI from : https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
  • Verify the integrity: sha256sum aws-sam-cli-linux-x86_64.zip
  • Unzip in sam-installation/ directory : unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
  • Install : sudo ./sam-installation/install
  • Verify installation: sam –version
  • It will show version like : SAM CLI, version 1.18.0

Setup sample application locally:

sam init

Sample output

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.7
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
 Next steps can be found in the README file at ./sam-app/README.md

Build and run locally:

Build docker container with python dependencies

**sam build --use-container --manifest ./sampleapp/requirements.txt**

Test docker container before running it locally – to verify package was build properly without any error

**sam local invoke Sample--event ./events.json**

{"statusCode": "308", "headers": {"Content-Type": "text/html; charset=utf-8", "Content-Length": "288", "Location": "https://127.0.0.1:4000/sample/?foo=bar"}, "isBase64Encoded": false, "body": "n
Redirecting...
You should be redirected automatically to target URL: href="https://127.0.0.1:4000/sample/?foo=bar">https://127.0.0.1:4000/sample/?foo=bar. If not click the link."}END RequestId: b658fa19-74b4-4ffb-a8a4-052152b69a21REPORT RequestId: b658fa19-74b4-4ffb-a8a4-052152b69a21  Init Duration: 0.13 ms  Duration: 1931.79 ms  Billed Duration: 1932 ms  Memory Size: 3072 MB  Max Memory Used: 3072 MB

Run docker container locally – on successfully start application is accessible with below url http://127.0.0.1:3000/BasePathName/Dash

**sam local start-api –host 0.0.0.0**
Mounting Sample at http://127.0.0.1:3000/{proxy+} [DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT]

Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

Deploy in AWS

**Setup AWS account credentials**
export AWS_ACCESS_KEY_ID=**************

export AWS_SECRET_ACCESS_KEY=*****************

sam deploy –guided
Facebook Facebook Twitter Reddit

Leave a Reply