How can I make my Lambda function talk to a server running on my local machine?

自作多情 提交于 2020-01-03 02:23:29

问题


I am creating a Lambda function that makes a call to some functionality on my server.

I would like to test the Lambda function against a local instance of my server, so that I don't have to deploy to AWS in order to test run the whole flow.

Is there any way to call my local machine's development server from inside Lambda without having to keep deploying to AWS (or some other remote server)?


回答1:


This may be possible if you set up a VPN or Direct Connect between your VPC and your local network.

But honestly, this will probably be more expensive and more complex than simply deploying to an EC2 instance. Automating deployment should be simple and straightforward.




回答2:


Too little info. It depends on how your lambda is configured. If it's in a VPC then how that VPC is configured. Whether your laptop has static IP, if it's sitting behind firewalls.

In general what you need is:

  • your service exposed to Internet:
    • a public IP for your local machine
    • ports (on which your service is exposed) opened up the if blocked by firewall etc.
  • your lambda has Internet access (route, security-group/acl/gateways):
    • if lambda is in public subnet, see AWS doc on how to setup IGW.
    • if it's in private subnet, see AWS doc on how to setup NAT GW.

Else if your laptop is inside some special secure network then VPN as chris suggested, which make sure your service is NOT exposed to Internet.

Some links that might help:

  • Setup Internet access for your lambdas in VPC.
  • Setup Internet access for your VPC.
  • Setup VPC for VPN access.



回答3:


I will write specific to the Java language:

AWS Lambda functions are plain old Java objects so you can write unit tests to trigger them locally. Even if you are using the RequestHandler interface which requires the entry point of the function to provide the

public O handleRequest(I input, Context context);

method and if you are using the context variable you can easily mock that like for example:

@Test
public void testAddAddressHandlerDevStage() throws Exception {
    Context context = mock(Context.class);       
   when(context.getInvokedFunctionArn()).thenReturn("arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:updateAddress:TEST");

    Address newAddress = new Address();
    new AddAddress().handleRequest(newAddress, context);
    assertNotNull(newAddress.getId());
}

If you write unit tests like this you can also trigger them via a CI tool like Jenkins on a test server.

So the above test works with TEST env variables but you can write a configuration for DEV environment too. Java language provides richer tools for such generalizations and abstractions compared to most interpreted language.




回答4:


If your local machine is publicly reachable and your server is callable on an open port then I don't see why it wouldn't just work. Maybe DynDNS hack would work if it is not publicly accessible.

Are you mainly concerned with having your favorite IDE for debugging? If not then it is cheap to set up EC2 server that you only spin up for testing. No charge when not in use. Or maybe checkout out AWS workspaces if you need a richer environment for testing and debugging this.

Also remember your EC2 instances can have Route 53 private host zone mapping an arbirtrary url to your instance which makes things a bit cleaner and closer to production.



来源:https://stackoverflow.com/questions/49325388/how-can-i-make-my-lambda-function-talk-to-a-server-running-on-my-local-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!