Code Monkey home page Code Monkey logo

aws-sam-cli-experiments's Introduction

aws-sam-lambda

This is an experimental repository, a number of scripts will help you to build and deploy a serverless application to AWS or to localstack.

This serverless application contains:

  • An API endpoint
  • A lambda that responds to the API endpoint
  • An SQS queue
  • An SNS topic
  • A lambda that reads from the SQS queue and posts to an SNS topic

A number of scripts provided in this repository illustrate how to perform common serverless build and deploy actions.

HelloWorldFunction directory

This actually contains 2 lambda functions:

  • ApiGatewayrespondingApp - responds to GET method at an http endpoint.
  • QueueIOApp - copies SQS queue messages into an SNS topic.

Prerequisites

Assumed, you already have Docker installed.

  • install-prerequisites-aws-sam-cli.sh
  • install-prerequisites-localstack-cli.sh

Setup accounts

The AWS Toolkit provides an easy way to select a current AWS profile in the bottom right corner of IntelliJ. It has a handy menu option called Edit Credential Files.

If you haven't already, create an IAM User with an access key. This will be used in a named profile that the AWS CLI and IntelliJ AWS Toolkit plugin can use to deploy your application for you.

Put it in a group with the following policies:

  • AdministratorAccess - allows it to create and deploy resources.
  • AmazonS3FullAccess - allows it to create, read and write to S3 buckets.

Now put this user's credentials into a named profile on your system:

  • Documentation: Connect to an AWS account
  • Edit the credentials and config files, and add a named profile.
  • This profile should provide the aws_access_key_id and aws_secret_access_key for an IAM user in your account (not the root user).

NB. these files can also be found in: ~/.aws

Validate and build

Validate the SAM template.yaml against a named AWS profile with:

  • validate-remote.sh

Either build the project within IntelliJ, or:

cd HelloWorldFunction
gradle build

or

sam build

Testing

cd HelloWorldFunction
gradle test

Invoke a lambda locally (without localstack)

Build, and test by providing a custom event, with sam local invoke:

sam build
sam local invoke HelloWorldFunction --event events/event.json

To emulate the API, use sam local start-api. It'll run on port 3000:

sam local start-api
curl http://localhost:3000/

Build and deploy to AWS

Either, use the AWS Toolkit which provides a Deploy Serverless Application option:

The Deploy Serverless Application opion

Alternatively:

  • deploy-remote.sh

This will offer a guided deployment, and save configuration to: samconfig.toml

Explore AWS resources

After deployment, you'll be given a list of resources in the new stack.

You can also explore the AWS account using the web interface, or AWS Explorer in IntelliJ (provided by the AWS Toolkit).

AWS Explorer in IntelliJ

Build and deploy to localstack

  • start-localstack.sh
  • deploy-localstack.sh

View remote logs from AWS

Tail remote logs with sam logs:

sam logs -n QueueIOFunction --stack-name aws-sam-lambda --tail

This uses the logical name of the function (as found in template.yaml) to look up and tail the appropriate logs from CloudWatch.

You could also do it through the AWS web interface.

Cleanup

To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

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

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.