Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
- [Package and Deploy to Lambda](#package-and-deploy-to-lambda)
- [Getting started](#getting-started)
- [Advanced](#advanced)
- [Compiled Languages (Java)](#compiled-languages-java)
- [Compiled Languages](#compiled-languages)
- [Java](#compiled-languages-java)
- [.NET Core](#compiled-languages-dotnetcore)
- [IAM Credentials](#iam-credentials)
- [Lambda Environment Variables](#lambda-environment-variables)
- [Environment Variable file](#environment-variable-file)
Expand Down Expand Up @@ -323,7 +325,9 @@ $ sam deploy --template-file ./packaged.yaml --stack-name mystack --capabilities

## Advanced

### Compiled Languages (Java)
### Compiled Languages

#### Java

To use SAM Local with compiled languages, such as Java that require a packaged artifact (e.g. a JAR, or ZIP), you can specify the location of the artifact with the `AWS::Serverless::Function` `CodeUri` property in your SAM template.

Expand Down Expand Up @@ -355,7 +359,30 @@ $ echo '{ "some": "input" }' | sam local invoke
$ sam local start-api
```

You can find a full Java example in the [samples/java](samples/java) folder
You can find a full Java example in the [samples/java](samples/java) folder.

#### .NET Core

To use SAM Local with compiled languages, such as .NET Core that require a packaged artifact (e.g. a ZIP), you can specify the location of the artifact with the `AWS::Serverless::Function` `CodeUri` property in your SAM template.

For example:

```
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
ExampleJavaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: HelloWorld::HelloWorld.Function::Handler
CodeUri: ./artifacts/HelloWorld.zip
Runtime: dotnetcore2.0
```

You should then build your ZIP file using your normal build process.

You can find a .NET Core example in the [samples/hello-world/dotnetcore2.0](samples/hello-world/dotnetcore2.0) folder.

### IAM Credentials

Expand Down
3 changes: 3 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To work with the example applications in this repository, first ensure that you'
* SAM Local
* An Amazon S3 bucket
* (Optional) Maven
* (Optional) .NET Core 2.0

To install the AWS CLI, follow the instructions at [Installing the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/installing.html).

Expand All @@ -19,6 +20,8 @@ To create an Amazon S3 bucket, follow the instructions at [Create a Bucket](http

To run the Java examples, you'll need to install Maven. For more information, see [Creating a .jar Deployment Package Using Maven without any IDE (Java) ](http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html).

To run the .NET Core 2.0 examples you'll need to install .NET Core SDK. For more information, see [.NET Downloads](https://www.microsoft.com/net/download/windows).

## Recommended ##

* [Postman](https://www.getpostman.com/)
Expand Down
35 changes: 35 additions & 0 deletions samples/hello-world/dotnetcore2.0/HelloWorld.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{15BADD3C-EB9E-4027-83A8-4164B0FD0B94}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "src\HelloWorld\HelloWorld.csproj", "{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{7C692A39-E2E3-4170-AB91-86BD226D61D7}"
ProjectSection(SolutionItems) = preProject
build.cake = build.cake
build.ps1 = build.ps1
build.sh = build.sh
event.json = event.json
template.yml = template.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{F090076F-EE78-4CC6-B614-9069D413A885}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582} = {15BADD3C-EB9E-4027-83A8-4164B0FD0B94}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C72B9A4-F5C5-4019-BF0F-CC10BDECF582}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
39 changes: 39 additions & 0 deletions samples/hello-world/dotnetcore2.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# AWS SAM Hello World Example #

A simple AWS SAM template that specifies a single Lambda function.

## Usage ##

To create and deploy the SAM Hello World example, first ensure that you've met the requirements described in the [root README](../../README.md). Then follow the steps below.

### Build your package ###

#### Linux & macOS

sh build.sh --target=Package

#### Windows (Powershell)

build.ps1 --target=Package

### Test your application locally ###

Use [SAM Local](https://github.com/awslabs/aws-sam-local) to run your Lambda function locally:

sam local invoke "HelloWorld" -e event.json

### Package artifacts ###

Run the following command, replacing `BUCKET-NAME` with the name of your bucket:

sam package --template-file template.yaml --s3-bucket BUCKET-NAME --output-template-file packaged-template.yaml

This creates a new template file, packaged-template.yaml, that you will use to deploy your serverless application.

### Deploy to AWS CloudFormation ###

Run the following command, replacing `MY-NEW-STACK` with a name for your CloudFormation stack.

sam deploy --template-file packaged-template.yaml --stack-name MY-NEW-STACK --capabilities CAPABILITY_IAM

This uploads your template to an S3 bucket and deploys the specified resources using AWS CloudFormation.
231 changes: 231 additions & 0 deletions samples/hello-world/dotnetcore2.0/build.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////

var target = Argument<string>("target", "Default");
var configuration = Argument<string>("configuration", "Release");
var verbosity = Argument<string>("verbosity", "Minimal");

///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////

var sourceDir = Directory("./src");

var solutions = GetFiles("./**/*.sln");
var projects = new []
{
sourceDir.Path + "/HelloWorld/HelloWorld.csproj",
};

// BUILD OUTPUT DIRECTORIES
var artifactsDir = Directory("./artifacts");
var publishDir = Directory("./publish/");

// VERBOSITY
var dotNetCoreVerbosity = Cake.Common.Tools.DotNetCore.DotNetCoreVerbosity.Normal;
if (!Enum.TryParse(verbosity, true, out dotNetCoreVerbosity))
{
dotNetCoreVerbosity = Cake.Common.Tools.DotNetCore.DotNetCoreVerbosity.Normal;
Warning(
"Verbosity could not be parsed into type 'Cake.Common.Tools.DotNetCore.DotNetCoreVerbosity'. Defaulting to {0}",
dotNetCoreVerbosity);
}

///////////////////////////////////////////////////////////////////////////////
// COMMON FUNCTION DEFINITIONS
///////////////////////////////////////////////////////////////////////////////

string GetProjectName(string project)
{
return project
.Split(new [] {'/'}, StringSplitOptions.RemoveEmptyEntries)
.Last()
.Replace(".csproj", string.Empty);
}

///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////

Setup(ctx =>
{
// Executed BEFORE the first task.
EnsureDirectoryExists(artifactsDir);
EnsureDirectoryExists(publishDir);
Information("Running tasks...");
});

Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});

///////////////////////////////////////////////////////////////////////////////
// TASK DEFINITIONS
///////////////////////////////////////////////////////////////////////////////

Task("Clean")
.Description("Cleans all directories that are used during the build process.")
.Does(() =>
{
foreach(var solution in solutions)
{
Information("Cleaning {0}", solution.FullPath);
CleanDirectories(solution.FullPath + "/**/bin/" + configuration);
CleanDirectories(solution.FullPath + "/**/obj/" + configuration);
Information("{0} was clean.", solution.FullPath);
}

CleanDirectory(artifactsDir);
CleanDirectory(publishDir);
});

Task("Restore")
.Description("Restores all the NuGet packages that are used by the specified solution.")
.Does(() =>
{
var settings = new DotNetCoreRestoreSettings
{
DisableParallel = false,
NoCache = true,
Verbosity = dotNetCoreVerbosity
};

foreach(var solution in solutions)
{
Information("Restoring NuGet packages for '{0}'...", solution);
DotNetCoreRestore(solution.FullPath, settings);
Information("NuGet packages restored for '{0}'.", solution);
}
});

Task("Build")
.Description("Builds all the different parts of the project.")
.Does(() =>
{
var msBuildSettings = new DotNetCoreMSBuildSettings
{
TreatAllWarningsAs = MSBuildTreatAllWarningsAs.Error,
Verbosity = dotNetCoreVerbosity
};

var settings = new DotNetCoreBuildSettings
{
Configuration = configuration,
MSBuildSettings = msBuildSettings,
NoRestore = true
};

foreach(var solution in solutions)
{
Information("Building '{0}'...", solution);
DotNetCoreBuild(solution.FullPath, settings);
Information("'{0}' has been built.", solution);
}
});

Task("Publish")
.Description("Publish the Lambda Functions.")
.Does(() =>
{
foreach(var project in projects)
{
var projectName = project
.Split(new [] {'/'}, StringSplitOptions.RemoveEmptyEntries)
.Last()
.Replace(".csproj", string.Empty);

var outputDirectory = System.IO.Path.Combine(publishDir, projectName);

var msBuildSettings = new DotNetCoreMSBuildSettings
{
TreatAllWarningsAs = MSBuildTreatAllWarningsAs.Error,
Verbosity = dotNetCoreVerbosity
};

var settings = new DotNetCorePublishSettings
{
Configuration = configuration,
MSBuildSettings = msBuildSettings,
NoRestore = true,
OutputDirectory = outputDirectory,
Verbosity = dotNetCoreVerbosity
};

Information("Publishing '{0}'...", projectName);
DotNetCorePublish(project, settings);
Information("'{0}' has been published.", projectName);
}
});

Task("Pack")
.Description("Packs all the different parts of the project.")
.Does(() =>
{
foreach(var project in projects)
{
var projectName = GetProjectName(project);

Information("Packing '{0}'...", projectName);
var path = System.IO.Path.Combine(publishDir, projectName);
var files = GetFiles(path + "/*.*");
Zip(
path,
System.IO.Path.Combine(artifactsDir, $"{projectName}.zip"),
files);
Information("'{0}' has been packed.", projectName);
}
});

Task("Run-Local")
.Description("Runs all the acceptance tests locally.")
.Does(() =>
{
var settings = new ProcessSettings
{
Arguments = "local invoke \"HelloWorld\" -e event.json",
};

Information("Starting the SAM local...");
using(var process = StartAndReturnProcess("sam", settings))
{
process.WaitForExit();
Information("Exit code: {0}", process.GetExitCode());
}
Information("SAM local has finished.");
});

///////////////////////////////////////////////////////////////////////////////
// TARGETS
///////////////////////////////////////////////////////////////////////////////

Task("Package")
.Description("This is the task which will run if target Package is passed in.")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.IsDependentOn("Publish")
.IsDependentOn("Pack")
.Does(() => { Information("Package target ran."); });

Task("Run")
.Description("This is the task which will run if target Run is passed in.")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.IsDependentOn("Publish")
.IsDependentOn("Pack")
.IsDependentOn("Run-Local")
.Does(() => { Information("Run target ran."); });

Task("Default")
.Description("This is the default task which will run if no specific target is passed in.")
.IsDependentOn("Default");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a Reflexive edges in graph are not allowed exception (the task being dependant on itself).


///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////

RunTarget(target);
Loading