Skip to content

Instantly share code, notes, and snippets.

@kixorz
Last active February 14, 2024 07:39
Show Gist options
  • Select an option

  • Save kixorz/3b172e2fc3ce35421ee9 to your computer and use it in GitHub Desktop.

Select an option

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);
});
};
@jakesylvestre

Copy link
Copy Markdown

@kixorz I'm just curious, does this change on every execution?

@kixorz

kixorz commented Nov 18, 2016

Copy link
Copy Markdown
Author

It may change. It depends if the Lambda function remains on the same machine "warmed up".

@agentleo

agentleo commented Jul 2, 2019

Copy link
Copy Markdown

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

kixorz commented Jul 2, 2019 via email

Copy link
Copy Markdown
Author

@endurance

Copy link
Copy Markdown

you don't have to move those bytes yourself.

Axios

@lydiahelkinz

lydiahelkinz commented May 2, 2023

Copy link
Copy Markdown

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

@kixorz

kixorz commented May 2, 2023

Copy link
Copy Markdown
Author

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

@kixorz

kixorz commented May 2, 2023

Copy link
Copy Markdown
Author

@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
Copy Markdown

can i use lambda function without going through proxy services

@kixorz

kixorz commented May 2, 2023

Copy link
Copy Markdown
Author

@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