How to Deploy and test AWS serverless application (API Gateway) created in .Net Core locally?

974 views Asked by At

I'm new to AWS server less programming. I've created a sample app. Blog(sample available with Visual Studio) using [.Net Core 1.0], now I want to deploy it locally and test it. I've tried AWS SAM Local and LocalStack but I'm confused as there is no clear explanation or steps for .Net Core application.

Can anyone provide me clear steps to deploy and execute this application locally?

1

There are 1 answers

2
Neil Bostrom On BEST ANSWER

The serverless sample out of the box from Amazon doesn't come with simple "press F5" way to run the code locally.

The easiest way to test your code locally is to create the sample with Unit Tests. These unit tests include everything you need to initialise Functions class so that you can run it. You could move this code into a simple console app or create unit tests that cover all the scenarios you want to test locally.

Here is the sample Unit Test from the project:

public class FunctionTest : IDisposable
{ 
    string TableName { get; }
    IAmazonDynamoDB DDBClient { get; }

    public FunctionTest()
    {
        this.TableName = "AWSServerless2-Blogs-" + DateTime.Now.Ticks;
        this.DDBClient = new AmazonDynamoDBClient(RegionEndpoint.USWest2);

        SetupTableAsync().Wait();
    }

    [Fact]
    public async Task BlogTestAsync()
    {
        TestLambdaContext context;
        APIGatewayProxyRequest request;
        APIGatewayProxyResponse response;

        Functions functions = new Functions(this.DDBClient, this.TableName);

        // Add a new blog post
        Blog myBlog = new Blog();
        myBlog.Name = "The awesome post";
        myBlog.Content = "Content for the awesome blog";

        request = new APIGatewayProxyRequest
        {
            Body = JsonConvert.SerializeObject(myBlog)
        };
        context = new TestLambdaContext();
        response = await functions.AddBlogAsync(request, context);
        Assert.Equal(200, response.StatusCode);