Kilian, thanks for your reply. I'm sorry for delayed answer - the notifications on my Slack were turned off. The snippet below creates an ACL.
I got help from Pulumi AI for some parts.
---------------
import pulumi
import pulumi_aws as aws
from pulumi import ResourceOptions
# Fetch the existing API Gateway
api_gateway = aws.apigatewayv2.get_api(api_gateway_name)
# Create IP Set 1
ip_set_1 = aws.wafv2.IpSet(
"ipset1",
name="ipset_1",
description="IP set containing 10.9.8.77/32",
ip_address_version="IPV4",
scope="REGIONAL", # Use "CLOUDFRONT" for global
addresses=["10.9.8.77/32"],
)
# Create IP Set 2
ip_set_2 = aws.wafv2.IpSet(
"ipset2",
name="ipset_2",
description="IP set containing 5.6.7.8/24",
ip_address_version="IPV4",
scope="REGIONAL",
addresses=["5.6.7.8/24"],
)
# Create the Web ACL
web_acl = aws.wafv2.WebAcl("waf-dev",
name="waf-dev",
description="Web ACL for API Gateway",
scope="REGIONAL",
default_action=aws.wafv2.WebAclDefaultActionArgs(
allow={},
),
visibility_config=aws.wafv2.WebAclVisibilityConfigArgs(
cloudwatch_metrics_enabled=True,
metric_name="WAFv2Metric",
sampled_requests_enabled=True,
),
rules=[
# User-Defined Rule 1: AllowIP
aws.wafv2.WebAclRuleArgs(
name="AllowIP",
priority=0,
action=aws.wafv2.WebAclRuleActionArgs(
allow={}
),
statement=aws.wafv2.WebAclRuleStatementArgs(
ip_set_reference_statement=aws.wafv2.WebAclRuleStatementIpSetReferenceStatementArgs(
arn=ip_set_1.arn
)
),
visibility_config=aws.wafv2.WebAclRuleVisibilityConfigArgs(
cloudwatch_metrics_enabled=True,
metric_name="AllowIPMetric",
sampled_requests_enabled=True,
),
),
# User-Defined Rule 2: BlockIP
aws.wafv2.WebAclRuleArgs(
name="BlockIP",
priority=1,
action=aws.wafv2.WebAclRuleActionArgs(
block={}
),
statement=aws.wafv2.WebAclRuleStatementArgs(
ip_set_reference_statement=aws.wafv2.WebAclRuleStatementIpSetReferenceStatementArgs(
arn=ip_set_2.arn
)
),
visibility_config=aws.wafv2.WebAclRuleVisibilityConfigArgs(
cloudwatch_metrics_enabled=True,
metric_name="BlockIPSet2Metric",
sampled_requests_enabled=True,
),
),
# AWS Managed Rules - Amazon IP Reputation List
aws.wafv2.WebAclRuleArgs(
name="AWSManagedRulesAmazonIpReputationList",
priority=2,
override_action=aws.wafv2.WebAclRuleOverrideActionArgs(
none={}
),
statement=aws.wafv2.WebAclRuleStatementArgs(
managed_rule_group_statement=aws.wafv2.WebAclRuleStatementManagedRuleGroupStatementArgs(
name="AWSManagedRulesAmazonIpReputationList",
vendor_name="AWS",
)
),
visibility_config=aws.wafv2.WebAclRuleVisibilityConfigArgs(
cloudwatch_metrics_enabled=True,
metric_name="AmazonIpReputationListMetric",
sampled_requests_enabled=True,
),
),
]
# Enable CloudWatch Logs
visibility_config=aws.wafv2.WebAclVisibilityConfigArgs(
cloudwatch_metrics_enabled=True,
metric_name="WebACLMetric",
sampled_requests_enabled=True,
),
Logging Configuration
logging_configuration=aws.wafv2.WebAclLoggingConfigurationArgs(
log_destination_configs=[
"arnawslogsus east 1123456789012log group/aws/wafv2/webacl"
],
resource_arn="", # wait for Web ACL creation
),
)
# Update Logging Configuration with Web ACL ARN
web_acl_logging = aws.wafv2.WebAclLoggingConfiguration(
"webAclLogging",
resource_arn=web_acl.arn,
log_destination_configs=[
"arnawslogsus east 1123456789012log group/aws/wafv2/webacl"
],
web_acl_id=web_acl.id,
opts=ResourceOptions(depends_on=[web_acl]),
)
# Associate Web ACL with API Gateway
web_acl_association = aws.wafv2.WebAclAssociation(
"webAclAssociation",
resource_arn=api_gateway.arn,
web_acl_arn=web_acl.arn,
)
# Export the Web ACL ARN
pulumi.export("web_acl_arn", web_acl.arn)