1

I need to create AWS lambda function to execute the python program. I need to incorporate the following shell command in it.

curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select(.region=="ap-southeast-1") | .ip_prefix'

Could someone guide me on this.

2

1 Answer 1

1

To simply shell out to curl and jq to get that data,

import subprocess

data = subprocess.check_output("""curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select(.region=="ap-southeast-1") | .ip_prefix'""", shell=True)

but you really probably shouldn't do that since e.g. there's no guarantee you have curl and jq in the Lambda execution environment (not to mention the overhead).

Instead, if you have the requests library,

import requests

resp = requests.get("https://ip-ranges.amazonaws.com/ip-ranges.json")
resp.raise_for_status()
prefixes = {
    r["ip_prefix"]
    for r in resp.json()["prefixes"]
    if r["region"] == "ap-southeast-1"
}
Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks. Is their a way for me to store all the IPs in a single file
In general, yes, of course you can write them into a file. However, you can't write files in lambda functions.
Understood. I need to fetch these output IPs and store them in a file, later dump the file to S3 for further processing.
So you need to store them in S3? Fine, that you can do; the boto3 library is the go-to S3 client for Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.