Code Monkey home page Code Monkey logo

aws-sam-test's Introduction

aws-sam-test

A small repo to test creating a Lambda function with the SAM CLI.

The lambda_handler.py file contains the lambda function.
The template.yml fle is the SAM template file used to create the lambda function. You will need to update this file with the arn of your Lambda function role.
The requirements.txt file is an empty Python requirements file that allows for local building and testing.

Prerequisites

Before you can run this package you must have the associated IAM permissions and a S3 bucket to read and write to.
In the following commands replace <bucket> with your S3 bucket name and a unique <stack-name>.
You also need a Lambda role as created in this repo and update the template.yml file with this arn.

Automated Build, Deploy & Remote Test

Executing the following command will build, deploy and remotely test the lambda function.

bash deploy.bsh <bucket> <stack-name>

If successful you should see Test successful at end of script output Please read the deploy.bsh file to see all of the individual commands.

Manual Build, Deploy & Test

Note that all of these operations occur in the package folder so only these contents are deployed to AWS.

Build

This command builds the CloudFormation package .aws-sam to be tested locally and deployed.

cd package
sam build

Local Test

First start a local Lambda endpoint with this command.

sam local start-lambda

Then in a new terminal invoke the local lambda with this command.

rm response.json
aws lambda invoke \
  --function-name demo-sam-lambda \
  --endpoint-url "http://127.0.0.1:3001" \
  --payload '{"first_name": "Dan", "last_name": "the man"}' \
  --cli-binary-format raw-in-base64-out \
  --no-verify-ssl response.json

You should see a 200 status code and the response.json file should contain {"message": "Hello Dan the man!"}.

Deploy

This command deploys the CloudFormation template in .aws-sam, which creates the lambda function.

sam deploy --stack-name <stack-name> --s3-bucket <bucket> --capabilities CAPABILITY_IAM

Test deployed function

rm response.json
aws lambda invoke \
  --function-name demo-sam-lambda \
  --payload '{"first_name": "Dangerous", "last_name": "Dan"}' \
  --cli-binary-format raw-in-base64-out \
  response.json

You should see a 200 status code and the response.json file should contain {"message": "Hello Dangerous Dan!"}.

References

aws-sam-test's People

Contributors

daniel-fudge avatar

Watchers

 avatar  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.