Example of a AWS SAM Local in C#
Clone or download
João Rosa
João Rosa Merge pull request #10 from joaoasrosa/joaoasrosa-patch-1
Update template.yml
Latest commit 31ed62a May 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build Update template.yml May 18, 2018
src/Lambda Enhance logging May 12, 2018
tests Add third acceptance test May 12, 2018
.gitignore Enhance gitignore May 12, 2018
.travis.yml Install aws-sam-cli in user mode May 12, 2018
LICENSE Initial commit May 8, 2018
Lambda.sln Add Acceptance tests project May 12, 2018
README.md Address feedback from the previous PR May 16, 2018
appveyor.yml Upgrade PATH in AppVeyor May 12, 2018
build.cake Update Cake May 12, 2018
build.ps1 Add Cake May 11, 2018
build.sh Add Cake May 11, 2018

README.md

C# example for SAM local

This repo presents one approach to use SAM Local in a real-world scenario. It provides an example of how to local develop an AWS Lambda function, using the same build steps locally and in the CI pipeline.

Build status Build Status

Scenario

Using AWS Lambda, I want to send notifications to a 3rd party API using SMS. The AWS Lambda is behind an AWS API Gateway, and the requests and responses are being proxy.

The 3rd party API can return the following responses:

  • Invalid credentials (Unauthorized (401)) - If the client does not set the proper credentials using the Authorization HTTP header, the API returns an Unauthorized HTTP Status Code
  • Insufficient Credits (Forbidden (403)) - If the user account doesn't have credits to use the resources, the API returns a Forbidden HTTP Status Code
  • Ok (OK (200)) - If the API sends the SMS, it returns an Ok HTTP Status Code

Implementation

The AWS Lambda function is implemented using .NET Core 2.0, and is using the APIGatewayProxyRequest and APIGatewayProxyResponse to be able to communicate with AWS API Gateway.

To be able to local test the AWS Lambda implementation, the project uses SAM Local to bootstrap all the necessary components to test it. The SAM Local is instrumented using the template.yml located at ./build folder.

Given the 3rd party API doesn't provide a sandbox mode for the acceptance tests, a Testing API was created where it is possible to replicate the behavior of the real API. SAM Local uses Docker containers, therefore the Testing API is implemented using the same technology.

The Acceptance Tests are based on the expected behavior of the API (BDD FTW), where it uses a Given/When/Then declarative instructions to specify the steps for the tests.

Pre-requirements

Windows

You can follow the download instructions or use your preferred package manager.

macOS

You can follow the download instructions or use your preferred package manager.

Linux

You can follow the download instructions or use your preferred package manager.

Run the tests locally

It uses Cake to build and run the tests in a transparent way in all supported platforms.

Windows

Using a Powershell terminal, on the root folder of the project:

./build.ps1 --target=Test-Local

macOS & Linux

Using a Terminal, on the root folder of the project:

sh ./build.sh --target=Test-Local

Know issues

At the moment, there are issues with the Docker Network on Windows. We are investigating a solution to it. Please stay tuned!