awsdocs/aws-lambda-developer-guide

sample-apps/ec2-spot - does `ec2Client` need to be `static`?

DmitryLukyanov opened this issue · 4 comments

I'm looking at this sample app and have a question why this sample uses a static modifier for ec2Client? As far as I understand, the intention is to use this client as singleton, but the same behavior will be reached if ec2Client will be non static, won't it? Like:

    private AmazonEC2Client ec2Client;

    public Function() {
      AWSSDKHandler.RegisterXRayForAllServices();
      ec2Client = new AmazonEC2Client();
    }

I'd like to know the answer to this as well.

not the author of that sample code and not affiliated with this repo, nor with aws, but here's something from the docs regarding static members (using java but I think it's also applicable to dotnet):

In the following example, the logger, serializer, and AWS SDK client are created when the function serves its first event. Subsequent events served by the same function instance are much faster because those resources already exist.

// Handler value: example.Handler
public class Handler implements RequestHandler<SQSEvent, String>{
  private static final Logger logger = LoggerFactory.getLogger(Handler.class);
  private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
  private static final LambdaAsyncClient lambdaClient = LambdaAsyncClient.create();
  ...
  @Override
  public String handleRequest(SQSEvent event, Context context)
  {
    String response = new String();
    // call Lambda API
    logger.info("Getting account settings");
    CompletableFuture<GetAccountSettingsResponse> accountSettings =
        lambdaClient.getAccountSettings(GetAccountSettingsRequest.builder().build());
    // log execution details
    logger.info("ENVIRONMENT VARIABLES: " + gson.toJson(System.getenv()));
    ...

https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html

So it seems like the real value of using a static member comes from initializing it outside of the constructor (so that it only happens once across multiple lambda invocations). That is not the case in the c# code in the sample app. To fully achieve the goal of performance optimization, the member should be static but also the assignment needs to happen outside the constructor.

Otoh, I don't see the same in the c# docs: https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html Maybe that static member is a leftover from converting sample java code to c#? 🤷

hang on, it's a static constructor: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
Then it makes sense. The ec2Client member will be initialized only once. Performance optimization achieved.

So it seems like the real value of using a static member comes from initializing it outside of the constructor (so that it only happens once across multiple lambda invocations). That is not the case in the c# code in the sample app.

As far as I can see, non static ctor is called only once too (at least in all tests I did)