Skip to content

Commit

Permalink
WAF: Access Control with IPv4 in CloudFront
Browse files Browse the repository at this point in the history
  • Loading branch information
takano32 committed Feb 27, 2024
1 parent 9b9ded5 commit 5effc34
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module "acm" {

resource "aws_cloudfront_distribution" "this" {
http_version = "http2and3"
web_acl_id = aws_wafv2_web_acl.web_acl.arn
origin {
domain_name = module.alb.lb_dns_name
origin_id = "alb"
Expand All @@ -26,7 +27,7 @@ resource "aws_cloudfront_distribution" "this" {
}

enabled = true
is_ipv6_enabled = true
is_ipv6_enabled = false
comment = var.site_domain

aliases = [var.site_domain]
Expand Down
48 changes: 48 additions & 0 deletions waf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
resource "aws_wafv2_ip_set" "ip_set" {
provider = aws.virginia
name = "allow-ip-set"
description = "allow ip set"
scope = "CLOUDFRONT"
ip_address_version = "IPV4"
addresses = [
"116.0.0.0/6",
"116.82.102.140/32",
"113.43.73.18/32"
]
}

resource "aws_wafv2_web_acl" "web_acl" {
provider = aws.virginia
name = "only-from-allow-ip-set"
description = "Web ACL that blocks all traffic except for a allow IP set"
scope = "CLOUDFRONT"
default_action {
block {}
}

rule {
name = "allow-ips-in-ip_set"
priority = 1
action {
allow {}
}

statement {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.ip_set.arn
}
}

visibility_config {
sampled_requests_enabled = false
cloudwatch_metrics_enabled = false
metric_name = "wordpress-allow-ips-in-ip_set"
}
}

visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "wordpress-wafv2-web-acl"
sampled_requests_enabled = false
}
}

0 comments on commit 5effc34

Please sign in to comment.