Sunil Ojha
Sunil Ojha

Reputation: 273

Can we test AWS lambdas written in C# using node.js?

Our developers are working on a project which they will be developing using AWS lambdas written in C#. They'll be using SNS, DynamoDB and some logging tool.

We are supposed to test these lambdas(black box testing only) so can we create an automation framework using node.js? We need to test each and every lambda by sending the request in JSON format and then process the response. Also, we need to verify the logs (right now in CloudWatch) for which they will be using any tool.

Please suggest.

Upvotes: 0

Views: 222

Answers (2)

Coco
Coco

Reputation: 906

Yeah! All of this is super easy with the Serverless Framework. If you deploy the code below with the Serverless Framework, using the settings in serverless.yml you will be able to do so easily.

  1. Set up the Serverless Framework
  2. Create a new service by writing this in your command line:
serverless create --template aws-csharp --path pathOfService --name whateverName
  1. Go into that directory and replace contents of Handler.cs with this:

    using System; 
    using Amazon.Lambda.Core;
    using Amazon.Lambda.Serialization.Json;
    using Amazon.Lambda.APIGatewayEvents;

    [assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

    namespace AwsDotnetCsharp { 
        public class Handler { 
            public APIGatewayProxyResponse Hello(APIGatewayProxyRequest request) { 
                return new APIGatewayProxyResponse() {
                    StatusCode = 200, 
                    Body = "Go Serverless v1.0! Your function executed successfully!", 
                }; 
            }    
        } 
    }

  1. Open your serverless.yml and replace contents with:
service: whateverName #the name you used to create the service

provider:
    name: aws
    runtime: dotnetcore1.0

package:
    artifact: bin/release/netcoreapp1.0/deploy-package.zip

functions:
    hello:
        handler: CsharpHandlers::AwsDotnetCsharp.Handler::Hello
        events:
            - http:
                path: hello
                method: GET
                cors: true
  1. Build the project by writing build.cmd
  2. Deploy your project: serverless deploy


After deploying, there will be a link in your command line that you can use to invoke the function.

Upvotes: 1

Noel Llevares
Noel Llevares

Reputation: 16037

I will not argue with your choice of testing strategy but will just offer a solution to your need.

Yes, you can as long as you are targeting the same AWS account.

You can invoke Lambda functions using NodeJS regardless of what runtime the Lambdas were written in. Basically, in this case, you invoke the Lambda with a certain input and you assert the output.

(Since you didn't mention any API Gateway, I assume that you are not using it so direct Lambda invocations are the most appropriate.)

You can also connect directly to DynamoDB and assert the contents.

For SNS and CloudWatch, these will be more tricky to test. Basically, before you run your tests, you need to setup listeners to the SNS topic or to the CloudWatch Log Group. These listeners can write the input they received into DynamoDB and then you can verify from there.

P.S.

Since you asked for my suggestions in the comment, I'll add them here since the comment box is just too tiny.

Regarding the test strategy, is there anything else that you would like to suggest?

I think it would be a better investment of your developer's time and resources if you focus on testing the logic of your application. Unit tests first and foremost.

You can do integration tests locally (you can test integration between Lambda's locally, you can also create a simple web server that can simulate SNS, Cloudwatch Logs can be intercepted using mocks, etc.)

The way you are doing your testing right now relies too much on AWS and it is almost like you're testing those AWS services yourself. I think you can just assume that those services will work as advertised so you only have to worry whether your application calls those AWS API calls properly.

Your approach to testing is a remnant of the non-cloud era where we host and manage the entire infrastructure ourselves and it made sense to test that all pieces are working. With cloud-based services, you don't need that anymore and you just focus on your application.

Upvotes: 1

Related Questions