Skip to content

Instantly share code, notes, and snippets.

@kixorz
Last active February 14, 2024 07:39
Show Gist options
  • Save kixorz/3b172e2fc3ce35421ee9 to your computer and use it in GitHub Desktop.
Save kixorz/3b172e2fc3ce35421ee9 to your computer and use it in GitHub Desktop.
Function retrieving AWS Lambda public IP address. Copy and paste this to your Lambda console, use standard permissions, execute and observe the log to see the public IP address of your Lambda function.
var http = require('http');
exports.handler = function(event, context) {
http.get('http://httpbin.org/get', function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.info(body);
context.done(null);
});
}).on('error', function(e) {
console.error(e.message);
context.fail(null);
});
};
@agentleo
Copy link

agentleo commented Jul 2, 2019

thanks @kixorz
btw for python dudes out there, it's:
requests.get('http://checkip.amazonaws.com').text.rstrip()

also, is there a way to provide the lambda with an ip (the user's) to act from ?
or make sure lambda really randomize / change ip's all the time ?

Thanks again man

@kixorz
Copy link
Author

kixorz commented Jul 2, 2019 via email

@endurance
Copy link

you don't have to move those bytes yourself.

Axios

@lydiahelkinz
Copy link

lydiahelkinz commented May 2, 2023

does this code assure us that lambda function uses different IP addresses for scrapping to avoid being blocked by Captcha? thanks

@kixorz
Copy link
Author

kixorz commented May 2, 2023

Axios is a pretty cool library, but this code is on purpose free of third party libraries.

@kixorz
Copy link
Author

kixorz commented May 2, 2023

@lydiahelkinz This code only tells you what the current public IP of the Lambda is.

If you're looking at scraping, you should consider using a good proxy service, implement a proxy layer in your code and block certain tracking requests.

@lydiahelkinz
Copy link

can i use lambda function without going through proxy services

@kixorz
Copy link
Author

kixorz commented May 2, 2023

@lydiahelkinz Yes, you can. Depends on what you're scraping. On some sites you will be very successful, on others it will fail. If you hit sites running on say CloudFlare or a similar CDNs with adaptive firewalls and bot protections, you will need to advance your game as the default Lambdas won't be enough. You may still use Lambdas as your compute platform, but the HTTP handling and network access will need to be more advanced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment