hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.87k stars 9.21k forks source link

Add data source aws_route53_resolver_firewall_domain_list #21020

Open dthvt opened 3 years ago

dthvt commented 3 years ago

Community Note

Description

AWS provides two managed domain lists that can be incorporated in Route 53 DNS Firewall rules. They have well known names, but the aws_route53_resolver_firewall_rule requires a domain list ID, not a well known name. This issue requests adding a data source for resolving these well known names to domain list IDs.

New or Affected Resource(s)

Potential Terraform Configuration

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.
data "aws_route53_resolver_firewall_domain_list" "malware" {
    name = "AWSManagedDomainsMalwareDomainList"
}

resource "aws_route53_resolver_firewall_rule" "managed_malware" {
  name = "aws-malware-list"
  action = "BLOCK"
  block_response = "NODATA"
  firewall_domain_list_id = data.aws_route53_resolver_firewall_domain_list.malware.id
  firewall_rule_group_id = aws_route53_resolver_firewall_rule_group.main.id
  priority = 100
}

References

posquit0 commented 2 years ago

Is there any discussion for this feature?

autarchprinceps commented 2 years ago

This is important, because the IDs aren't globally the same either. They can change depending on your region

bodgit commented 2 years ago

Looks like 4.39.0 has #25509 merged however that's not really much use; you can get the ID of a domain list if you know the ID already. It would be more useful to use the name.

I'll have a look to see if the name can be used instead of/as well as the ID when querying.

bodgit commented 2 years ago

Looking through the AWS SDK, it looks like you can currently only retrieve domain lists by ID: https://docs.aws.amazon.com/sdk-for-go/api/service/route53resolver/#GetFirewallDomainListInput

So it will need the AWS SDK to be updated before this can be fixed.

dthvt commented 2 years ago

The https://docs.aws.amazon.com/sdk-for-go/api/service/route53resolver/#Route53Resolver.ListFirewallDomainLists endpoint returns the AWS managed lists as well as any custom lists. So it would be possible to search the returned list for the name.

$ aws route53resolver list-firewall-domain-lists | jq '.FirewallDomainLists[] | select(.Name == "AWSManagedDomainsMalwareDomainList")'
{
  "Id": "rslvr-fdl-xxxx",
  "Arn": "arn:aws:route53resolver:us-east-1:xxxx:firewall-domain-list/rslvr-fdl-xxxx",
  "Name": "AWSManagedDomainsMalwareDomainList",
  "CreatorRequestId": "AWSManagedDomainsMalwareDomainList",
  "ManagedOwnerName": "Route 53 Resolver DNS Firewall"
}
elduds commented 1 year ago

Looks like IDs are common across AWS accounts but unique within a region. So can be statically defined per region as locals, or in a module.

I've given it a dirty hack shot here: https://registry.terraform.io/modules/elduds/route53resolver-firewall-domainlists/aws/latest

Allowing you to define your domain lists by name, eg:

resource "aws_route53_resolver_firewall_rule" "block_malware_domains" {
  action                  = "BLOCK"
  block_response          = "NODATA"
  firewall_domain_list_id = module.route53resolver-firewall-domainlists.all_domain_lists[data.aws_region.current.name].AWSManagedDomainsMalwareDomainList
  firewall_rule_group_id  = aws_route53_resolver_firewall_rule_group.managed_domain_lists.id
  name                    = "block-AWSManagedDomainsMalwareDomainList"
  priority                = 300
}

Will just need new versions as new AWS regions come online.