This project demonstrates the integration with AWS Auto-Scaling service and .NET Core, using SAM for building and test.
This sample contains source code and supporting files for a serverless application that you can deploy with the SAM CLI.
./src- Multilayer .NET Core project for the application's Lambda function../.events- Invocation events that you can use to invoke the function../test- Unit tests for the application code with XUnit.template.yaml- A template that defines the application's AWS resources.
Using a JSON as input is possible to suspend or resume processes from a specific auto-scaling by tag name.
Use case: SUSPEND the Terminate and Launch processes as the initial stage from Blue-Green Deploy on Code Pipeline and then, RESUME in the final stage.
JSON sample for use at AWS or in this project.
{
"Scalings": [
{
"Tag": "tag-name-here",
"Suspend": false
}
]
}Many scalings:
{
"Scalings": [
{
"Tag": "tag-name-here",
"Suspend": false
},
{
"Tag": "tag-name-here",
"Suspend": true
}
]
}About Processes, is possible to specify, but if not it will use the default list.
{
"Scalings": [
{
"Tag": "tag-name-here",
"Suspend": false,
"Processes": [
"Terminate",
"Launch"
]
}
]
}Default values are defined on file ProcessService.cs:
public class ProcessService : IProcessService
{
private static ProcessType LaunchProcessType => new ProcessType {ProcessName = "Launch"};
private static ProcessType ScheduledActionsProcessType => new ProcessType {ProcessName = "ScheduledActions"};
private static ProcessType TerminateProcessType => new ProcessType {ProcessName = "Terminate"};
// comment for brevity
}This project consists of:
Dotnet.AWSLambda.AutoScaling.Application.Function.cs- class file containing a class with a single function handler method;aws-lambda-tools-defaults.json- default argument settings for use with Rider or Visual Studio and command line deployment tools for AWS.
Install Amazon.Lambda.Tools Global Tools if not already installed.
dotnet tool install -g Amazon.Lambda.ToolsIf already installed check if a new version is available.
dotnet tool update -g Amazon.Lambda.ToolsThe Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- .NET Core - Install .NET Core
- Docker - Install Docker community edition
Build application with the sam build command.
sam buildThe SAM CLI installs dependencies defined in ./src/Dotnet.AWSLambda.AutoScaling.Application/Dotnet.AWSLambda.AutoScaling.Application.csproj, creates a deployment package, and saves it in the .aws-sam/build folder.
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the ./.events folder in this project.
json-event.jsonrepresents a simple json input.
request-event.jsonrepresents a request json input.
Run functions locally and invoke them with the sam local invoke command.
sam local invoke -e json-event.jsonOR
sam local invoke -e request-event.jsonThe SAM CLI can also emulate the application's as API. Use the sam local start-api to run the API locally on port 3000.
sam local start-apiThen, is possible to request using cURL or REST Client:
cURL
curl --header "Content-Type: application/json" -X POST -d "{ 'Scalings': [ { 'Tag': 'your-tag-name-here', 'Suspend': false }, { 'Tag': 'your-tag-name-h', 'Suspend': true } ] }" http://127.0.0.1:3000/apiREST Client
POST http://127.0.0.1:3000/api/
content-type: application/json
{
"Scalings": [
{
"Tag": "tag-name-here",
"Suspend": false
},
{
"Tag": "tag-name-here",
"Suspend": true
}
]
}The SAM CLI reads the application template to determine the API's routes and the functions that they invoke.
Events:
AutoScalingManager:
Type: Api
Properties:
Path: '/api'
Method: postYou can set credentials in the AWS credentials file on your local system. This file must be located in one of the following locations:
-
~/.aws/credentialson Linux or macOS -
C:\Users\USERNAME\.aws\credentialson Windows
This file should contain lines in the following format:
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key- Environment variables – You can set the
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYenvironment variables.
To set these variables on Linux or macOS, use the export command:
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_keyTo set these variables on Windows, use the set command:
set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_keyIf you are testing this lambda project with SAM, is necessary to inform the credentials on template.yaml:
Environment:
Variables:
AWS_ACCESS_KEY_ID: VALUE
AWS_SECRET_ACCESS_KEY: VALUE
AWS_DEFAULT_REGION: VALUETests are defined in the test folder in this project.
dotnet test