-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare_ip_management.tf
191 lines (178 loc) · 5.86 KB
/
cloudflare_ip_management.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
### I manage several project that utilize Cloudflare. I needed a way to quickly deploy
### and maintain the Cloudflare IP ranges in my security groups.
### You may need to make a variety of changes to get this working in your environment.
terraform {
required_providers {
http = {
source = "hashicorp/http"
version = "~> 3.0"
}
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
# use your profile name or convert to access keys or env vars
region = "us-east-1"
profile = "rootacct"
shared_credentials_files = ["~/.aws/config"]
}
# Get current Cloudflare IP address ranges so they can be added to the inbound security groups - IPv4 ranges
data "http" "cloudflare_ipv4" {
url = "https://www.cloudflare.com/ips-v4"
request_headers = {
Accept = "application/text"
}
}
# Get current Cloudflare IP address ranges so they can be added to the inbound security groups - IPv6 ranges
data "http" "cloudflare_ipv6" {
url = "https://www.cloudflare.com/ips-v6"
request_headers = {
Accept = "application/text"
}
}
# Convert strings to sets
# I have no idea *why* this regex works, but it does. Please open an issue if there is a better way.
# Yes, I realize there is a REST API where I can get this cleaner, but I wanted to do it without an API key.
locals {
CloudflareAddressesIPV4 = toset(regexall("^*[0-9].*", data.http.cloudflare_ipv4.response_body))
CloudflareAddressesIPV6 = toset(regexall("^*:*.*", data.http.cloudflare_ipv6.response_body))
}
# Get exiting VPC - replace MyVPC with your VPC name
data "aws_vpc" "vpc" {
filter {
name = "tag:Name"
values = ["MyVPC"]
}
}
# Create Security Group - or import one using a data source
resource "aws_security_group" "public_sg" {
name = "public-sg"
description = "Public internet access"
vpc_id = data.aws_vpc.vpc.id
tags = {
Name = "public-sg"
ManagedBy = "terraform"
}
}
# HTTP CloudFlare OUT IPv4
resource "aws_vpc_security_group_egress_rule" "private_out_http_v4" {
for_each = local.CloudflareAddressesIPV4
security_group_id = aws_security_group.public_sg.id
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr_ipv4 = each.key
tags = {
Name = "EgressTCP80IPv4"
Description = "Allows Cloudflare Owned IPs TCP:80 Outbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTP CloudFlare OUT IPv6
resource "aws_vpc_security_group_egress_rule" "private_out_http_v6" {
for_each = local.CloudflareAddressesIPV6
security_group_id = aws_security_group.public_sg.id
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr_ipv6 = each.key
tags = {
Name = "EgressTCP80IPv6"
Description = "Allows Cloudflare Owned IPs TCP:80 Outbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTPS CloudFlare OUT IPv4
resource "aws_vpc_security_group_egress_rule" "private_out_https_v4" {
for_each = local.CloudflareAddressesIPV4
security_group_id = aws_security_group.public_sg.id
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv4 = each.key
tags = {
Name = "EgressTCP443IPv4"
Description = "Allows Cloudflare Owned IPs TCP:443 Outbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTPS CloudFlare OUT IPv6
resource "aws_vpc_security_group_egress_rule" "private_out_https_v6" {
for_each = local.CloudflareAddressesIPV6
security_group_id = aws_security_group.public_sg.id
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv6 = each.key
tags = {
Name = "EgressTCP443IPv4"
Description = "Allows Cloudflare Owned IPs TCP:443 Outbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTP CloudFlare IN IPv4
resource "aws_vpc_security_group_ingress_rule" "public_in_http_v4" {
for_each = local.CloudflareAddressesIPV4
security_group_id = aws_security_group.public_sg.id
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr_ipv4 = each.key
tags = {
Name = "IngressTCP80IPv4"
Description = "Allows Cloudflare Owned IPs TCP:80 Inbound"
ManagedBy = "terraform"
}
}
# HTTP CloudFlare IN IPv6
resource "aws_vpc_security_group_ingress_rule" "public_in_http_v6" {
for_each = local.CloudflareAddressesIPV6
security_group_id = aws_security_group.public_sg.id
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr_ipv6 = each.key
tags = {
Name = "IngressTCP80IPv6"
Description = "Allows Cloudflare Owned IPs TCP:80 Inbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTPS CloudFlare IN IPv4
resource "aws_vpc_security_group_ingress_rule" "public_in_https_v4" {
for_each = local.CloudflareAddressesIPV4
security_group_id = aws_security_group.public_sg.id
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv4 = each.key
tags = {
Name = "IngressTCP443IPv4"
Description = "Allows Cloudflare Owned IPs TCP:443 Inbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}
# HTTPS CloudFlare IN IPv6
resource "aws_vpc_security_group_ingress_rule" "public_in_https_v6" {
for_each = local.CloudflareAddressesIPV6
security_group_id = aws_security_group.public_sg.id
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv6 = each.key
tags = {
Name = "IngressTCP443IPv4"
Description = "Allows Cloudflare Owned IPs TCP:443 Inbound"
ManagedBy = "terraform"
CloudflareRange = each.key
}
}