Score:0

How can an AWS lambda function call an EC2 instance via private ip address?

cz flag

How can I get a lambda to call an ec2 instances via its private ip address?

Long story short, making a discord bot for my minecraft server mates to start and stop the AWS instance, and to handle auto shutdown if no one is on the server.

Lambda function would handle starting, stopping, getting the current auto-assigned public ip address, and confirming the server is running via Minecraft's query protocol (UDP port 25565)

I am trying to do this as cost efficient as possible, and want to see if I can do this without an elastic ip address assigned. As I will have more then one minecraft server that will need to launched. And there is a fee if that server is offline with a elastic ip address, or you are using more then 1 elastic ip.

I have tested the same function when the same server was using an elastic ip address, and it worked as expected. Now, I want to know if I can do the same with the private ip address.

Tim avatar
gp flag
Tim
Ah... what part are you having problems with? Finding the private non-elastic IP or calling it? If you can call an elastic IP I'm not sure what the difference is to call a private IP. You probably know this but stopping the instance is done by calling the EC2 API for which you don't need the IP address.
user3346931 avatar
cz flag
Calling the private ip address once the server instance is running, is what I need help with. The starting and stopping of the instance is handled by the ec2 api on the lambda. When the api does its "statusCheck" action, it will call describeInstance and call UDP port 25565 at the same time, then combine them and filter it for the discord bot to read.
Tim avatar
gp flag
Tim
If you can call an elastic IP address, what is the difference to call a public IP address? I don't understand the problem you're having. You'd just call the EC2 API to find the IP either way wouldn't you?
Score:0
af flag

If you have not done so already you need to connect Lambda to the VPC that the EC2 instance is running within. https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html

By default Lambda functions can only communicate with resources on the public internet, so the EC2 instance's public IP, the AWS APIs, etc.

From there you need to make sure the security group (SG) assigned to the EC2 instance can receive (Inbound) traffic from the SG assigned to the Lambda function. This would be UDP port 25565 or whatever you need.

You also need to make sure the SG assigned to the Lambda function can make outbound connections to the SG assigned to the EC2 instace, again on UDP port 2265 or whatever you need. This is a little easier as the Outbound policy is usually wide open.

For testing you can assign the SG for the Lambda function to a different EC2 instances, put the instance playing the part of the Lambda function in the same subnet(s) will help validate the setup further.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.