Skip to content

Instantly share code, notes, and snippets.

@pengwk
Forked from dduvnjak/add_cloudflare_ips.sh
Created April 20, 2025 05:41
Show Gist options
  • Select an option

  • Save pengwk/2306bd4da72cdbe7375cc330a0c7ab47 to your computer and use it in GitHub Desktop.

Select an option

Save pengwk/2306bd4da72cdbe7375cc330a0c7ab47 to your computer and use it in GitHub Desktop.
Add CloudFlare IP addresses to an EC2 Security Group using awscli
# first we download the list of IP ranges from CloudFlare
wget https://www.cloudflare.com/ips-v4
# set the security group ID
SG_ID="sg-00000000000000"
# iterate over the IP ranges in the downloaded file
# and allow access to ports 80 and 443
while read p
do
aws ec2 authorize-security-group-ingress --group-id $SG_ID --ip-permissions IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges="[{CidrIp=$p,Description='Cloudflare'}]"
aws ec2 authorize-security-group-ingress --group-id $SG_ID --ip-permissions IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges="[{CidrIp=$p,Description='Cloudflare'}]"
done< ips-v4
rm ips-v4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment