Skip to content

Commit

Permalink
Merge pull request #24749 from wellsiau-aws/d-elbv2-host-zone-id
Browse files Browse the repository at this point in the history
add: new data source aws_lb_hosted_zone_id
  • Loading branch information
ewbankkit authored May 18, 2022
2 parents 5f1dea2 + 8a7cff8 commit ab0cd6b
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/24749.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_lb_hosted_zone_id
```
1 change: 1 addition & 0 deletions .github/labeler-pr-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ service/elbv2:
- 'website/**/lb\.*'
- 'website/**/lb_listener*'
- 'website/**/lb_target_group*'
- 'website/**/lb_hosted*'
service/emr:
- 'internal/service/emr/**/*'
- 'website/**/emr_*'
Expand Down
2 changes: 1 addition & 1 deletion docs/contributing/contribution-checklists.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ manually sourced values from documentation. Amazon employees can code search
previous region values to find new region values in internal packages like
RIPStaticConfig if they are not documented yet.

- [ ] Check [Elastic Load Balancing endpoints and quotas](https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region) and add Route53 Hosted Zone ID if available to [`internal/service/elb/hosted_zone_id_data_source.go`](../../internal/service/elb/hosted_zone_id_data_source.go)
- [ ] Check [Elastic Load Balancing endpoints and quotas](https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region) and add Route53 Hosted Zone ID if available to [`internal/service/elb/hosted_zone_id_data_source.go`](../../internal/service/elb/hosted_zone_id_data_source.go) and [`internal/service/elbv2/hosted_zone_id_data_source.go`](../../internal/service/elbv2/hosted_zone_id_data_source.go)
- [ ] Check [Amazon Simple Storage Service endpoints and quotas](https://docs.aws.amazon.com/general/latest/gr/s3.html#s3_region) and add Route53 Hosted Zone ID if available to [`internal/service/s3/hosted_zones.go`](../../internal/service/s3/hosted_zones.go)
- [ ] Check [CloudTrail Supported Regions docs](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-supported-regions.html#cloudtrail-supported-regions) and add AWS Account ID if available to [`internal/service/cloudtrail/service_account_data_source.go`](../../internal/service/cloudtrail/service_account_data_source.go)
- [ ] Check [Elastic Load Balancing Access Logs docs](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html#attach-bucket-policy) and add Elastic Load Balancing Account ID if available to [`internal/service/elb/service_account_data_source.go`](../../internal/service/elb/service_account_data_source.go)
Expand Down
13 changes: 7 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,13 @@ func Provider() *schema.Provider {
"aws_elb_service_account": elb.DataSourceServiceAccount(),

// Adding the Aliases for the ALB -> LB Rename
"aws_alb_listener": elbv2.DataSourceListener(),
"aws_alb_target_group": elbv2.DataSourceTargetGroup(),
"aws_alb": elbv2.DataSourceLoadBalancer(),
"aws_lb_listener": elbv2.DataSourceListener(),
"aws_lb_target_group": elbv2.DataSourceTargetGroup(),
"aws_lb": elbv2.DataSourceLoadBalancer(),
"aws_alb": elbv2.DataSourceLoadBalancer(),
"aws_alb_listener": elbv2.DataSourceListener(),
"aws_alb_target_group": elbv2.DataSourceTargetGroup(),
"aws_lb": elbv2.DataSourceLoadBalancer(),
"aws_lb_hosted_zone_id": elbv2.DataSourceHostedZoneID(),
"aws_lb_listener": elbv2.DataSourceListener(),
"aws_lb_target_group": elbv2.DataSourceTargetGroup(),

"aws_emr_release_labels": emr.DataSourceReleaseLabels(),

Expand Down
120 changes: 120 additions & 0 deletions internal/service/elbv2/hosted_zone_id_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package elbv2

import (
"fmt"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

// See https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region
var HostedZoneIdPerRegionALBMap = map[string]string{
endpoints.AfSouth1RegionID: "Z268VQBMOI5EKX",
endpoints.ApEast1RegionID: "Z3DQVH9N71FHZ0",
endpoints.ApNortheast1RegionID: "Z14GRHDCWA56QT",
endpoints.ApNortheast2RegionID: "ZWKZPGTI48KDX",
endpoints.ApNortheast3RegionID: "Z5LXEXXYW11ES",
endpoints.ApSouth1RegionID: "ZP97RAFLXTNZK",
endpoints.ApSoutheast1RegionID: "Z1LMS91P8CMLE5",
endpoints.ApSoutheast2RegionID: "Z1GM3OXH4ZPM65",
endpoints.ApSoutheast3RegionID: "Z08888821HLRG5A9ZRTER",
endpoints.CaCentral1RegionID: "ZQSVJUPU6J1EY",
endpoints.CnNorth1RegionID: "Z1GDH35T77C1KE",
endpoints.CnNorthwest1RegionID: "ZM7IZAIOVVDZF",
endpoints.EuCentral1RegionID: "Z215JYRZR1TBD5",
endpoints.EuNorth1RegionID: "Z23TAZ6LKFMNIO",
endpoints.EuSouth1RegionID: "Z3ULH7SSC9OV64",
endpoints.EuWest1RegionID: "Z32O12XQLNTSW2",
endpoints.EuWest2RegionID: "ZHURV8PSTC4K8",
endpoints.EuWest3RegionID: "Z3Q77PNBQS71R4",
endpoints.MeSouth1RegionID: "ZS929ML54UICD",
endpoints.SaEast1RegionID: "Z2P70J7HTTTPLU",
endpoints.UsEast1RegionID: "Z35SXDOTRQ7X7K",
endpoints.UsEast2RegionID: "Z3AADJGX6KTTL2",
endpoints.UsGovEast1RegionID: "Z166TLBEWOO7G0",
endpoints.UsGovWest1RegionID: "Z33AYJ8TM3BH4J",
endpoints.UsWest1RegionID: "Z368ELLRRE2KJ0",
endpoints.UsWest2RegionID: "Z1H1FL5HABSF5",
}

// See https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region
var HostedZoneIdPerRegionNLBMap = map[string]string{
endpoints.AfSouth1RegionID: "Z203XCE67M25HM",
endpoints.ApEast1RegionID: "Z12Y7K3UBGUAD1",
endpoints.ApNortheast1RegionID: "Z31USIVHYNEOWT",
endpoints.ApNortheast2RegionID: "ZIBE1TIR4HY56",
endpoints.ApNortheast3RegionID: "Z1GWIQ4HH19I5X",
endpoints.ApSouth1RegionID: "ZVDDRBQ08TROA",
endpoints.ApSoutheast1RegionID: "ZKVM4W9LS7TM",
endpoints.ApSoutheast2RegionID: "ZCT6FZBF4DROD",
endpoints.ApSoutheast3RegionID: "Z01971771FYVNCOVWJU1G",
endpoints.CaCentral1RegionID: "Z2EPGBW3API2WT",
endpoints.CnNorth1RegionID: "Z3QFB96KMJ7ED6",
endpoints.CnNorthwest1RegionID: "ZQEIKTCZ8352D",
endpoints.EuCentral1RegionID: "Z3F0SRJ5LGBH90",
endpoints.EuNorth1RegionID: "Z1UDT6IFJ4EJM",
endpoints.EuSouth1RegionID: "Z23146JA1KNAFP",
endpoints.EuWest1RegionID: "Z2IFOLAFXWLO4F",
endpoints.EuWest2RegionID: "ZD4D7Y8KGAS4G",
endpoints.EuWest3RegionID: "Z1CMS0P5QUZ6D5",
endpoints.MeSouth1RegionID: "Z3QSRYVP46NYYV",
endpoints.SaEast1RegionID: "ZTK26PT1VY4CU",
endpoints.UsEast1RegionID: "Z26RNL4JYFTOTI",
endpoints.UsEast2RegionID: "ZLMOA37VPKANP",
endpoints.UsGovEast1RegionID: "Z1ZSMQQ6Q24QQ8",
endpoints.UsGovWest1RegionID: "ZMG1MZ2THAWF1",
endpoints.UsWest1RegionID: "Z24FKFUX50B4VW",
endpoints.UsWest2RegionID: "Z18D5FSROUN65G",
}

func DataSourceHostedZoneID() *schema.Resource {
return &schema.Resource{
Read: dataSourceHostedZoneIDRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidRegionName,
},
"load_balancer_type": {
Type: schema.TypeString,
Optional: true,
Default: elbv2.LoadBalancerTypeEnumApplication,
ValidateFunc: validation.StringInSlice([]string{elbv2.LoadBalancerTypeEnumApplication, elbv2.LoadBalancerTypeEnumNetwork}, false),
},
},
}
}

func dataSourceHostedZoneIDRead(d *schema.ResourceData, meta interface{}) error {
region := meta.(*conns.AWSClient).Region
if v, ok := d.GetOk("region"); ok {
region = v.(string)
}

lbType := elbv2.LoadBalancerTypeEnumApplication
if v, ok := d.GetOk("load_balancer_type"); ok {
lbType = v.(string)
}

if lbType == elbv2.LoadBalancerTypeEnumApplication {
if zoneId, ok := HostedZoneIdPerRegionALBMap[region]; ok {
d.SetId(zoneId)
} else {
return fmt.Errorf("unsupported AWS Region: %s", region)
}
} else if lbType == elbv2.LoadBalancerTypeEnumNetwork {
if zoneId, ok := HostedZoneIdPerRegionNLBMap[region]; ok {
d.SetId(zoneId)
} else {
return fmt.Errorf("unsupported AWS Region: %s", region)
}
}

return nil
}
69 changes: 69 additions & 0 deletions internal/service/elbv2/hosted_zone_id_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package elbv2_test

import (
"testing"

"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
tfelbv2 "github.com/hashicorp/terraform-provider-aws/internal/service/elbv2"
)

func TestAccLBHostedZoneIDDataSource_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, elbv2.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSLbHostedZoneIdConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_lb_hosted_zone_id.main", "id", tfelbv2.HostedZoneIdPerRegionALBMap[acctest.Region()]),
),
},
{
Config: testAccCheckAWSLbHostedZoneIdExplicitRegionConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_lb_hosted_zone_id.regional", "id", "Z32O12XQLNTSW2"),
),
},
{
Config: testAccCheckAWSLbHostedZoneIdExplicitNetworkConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_lb_hosted_zone_id.network", "id", tfelbv2.HostedZoneIdPerRegionNLBMap[acctest.Region()]),
),
},
{
Config: testAccCheckAWSLbHostedZoneIdExplicitNetworkRegionConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_lb_hosted_zone_id.network-regional", "id", "Z2IFOLAFXWLO4F"),
),
},
},
})
}

const testAccCheckAWSLbHostedZoneIdConfig = `
data "aws_lb_hosted_zone_id" "main" {}
`

//lintignore:AWSAT003
const testAccCheckAWSLbHostedZoneIdExplicitRegionConfig = `
data "aws_lb_hosted_zone_id" "regional" {
region = "eu-west-1"
}
`

const testAccCheckAWSLbHostedZoneIdExplicitNetworkConfig = `
data "aws_lb_hosted_zone_id" "network" {
load_balancer_type = "network"
}
`

//lintignore:AWSAT003
const testAccCheckAWSLbHostedZoneIdExplicitNetworkRegionConfig = `
data "aws_lb_hosted_zone_id" "network-regional" {
region = "eu-west-1"
load_balancer_type = "network"
}
`
2 changes: 1 addition & 1 deletion names/names_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ elastic-inference,elasticinference,elasticinference,elasticinference,,elasticinf
elastictranscoder,elastictranscoder,elastictranscoder,elastictranscoder,,elastictranscoder,,,ElasticTranscoder,ElasticTranscoder,,1,,aws_elastictranscoder_,,elastictranscoder_,Elastic Transcoder,Amazon,,,,,
elasticache,elasticache,elasticache,elasticache,,elasticache,,,ElastiCache,ElastiCache,,1,,aws_elasticache_,,elasticache_,ElastiCache,Amazon,,,,,
es,es,elasticsearchservice,elasticsearchservice,elasticsearch,es,,es;elasticsearchservice,Elasticsearch,ElasticsearchService,,1,aws_elasticsearch_,aws_es_,,elasticsearch_,Elasticsearch,Amazon,,,,,
elbv2,elbv2,elbv2,elasticloadbalancingv2,,elbv2,,elasticloadbalancingv2,ELBV2,ELBV2,,1,aws_a?lb(\b|_listener|_target_group),aws_elbv2_,,lb\.;lb_listener;lb_target_group,ELB (Elastic Load Balancing),,,,,,
elbv2,elbv2,elbv2,elasticloadbalancingv2,,elbv2,,elasticloadbalancingv2,ELBV2,ELBV2,,1,aws_a?lb(\b|_listener|_target_group),aws_elbv2_,,lb\.;lb_listener;lb_target_group;lb_hosted,ELB (Elastic Load Balancing),,,,,,
elb,elb,elb,elasticloadbalancing,,elb,,elasticloadbalancing,ELB,ELB,,1,aws_(app_cookie_stickiness_policy|elb|lb_cookie_stickiness_policy|lb_ssl_negotiation_policy|load_balancer_|proxy_protocol_policy),aws_elb_,,app_cookie_stickiness_policy;elb;lb_cookie_stickiness_policy;lb_ssl_negotiation_policy;load_balancer;proxy_protocol_policy,ELB Classic,,,,,,
mediaconnect,mediaconnect,mediaconnect,mediaconnect,,mediaconnect,,,MediaConnect,MediaConnect,,1,,aws_mediaconnect_,,media_connect_,Elemental MediaConnect,AWS,,,,,
mediaconvert,mediaconvert,mediaconvert,mediaconvert,,mediaconvert,,,MediaConvert,MediaConvert,,1,aws_media_convert_,aws_mediaconvert_,,media_convert_,Elemental MediaConvert,AWS,,,,,
Expand Down
40 changes: 40 additions & 0 deletions website/docs/d/lb_hosted_zone_id.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "ELB (Elastic Load Balancing)"
layout: "aws"
page_title: "AWS: aws_lb_hosted_zone_id"
description: |-
Provides AWS Elastic Load Balancing Hosted Zone Id
---

# Data Source: aws_lb_hosted_zone_id

Use this data source to get the HostedZoneId of the AWS Elastic Load Balancing (ELB) in a given region for the purpose of using in an AWS Route53 Alias. Specify the ELB type (`network` or `application`) to return the relevant the associated HostedZoneId. Ref: [ELB service endpoints](https://docs.aws.amazon.com/general/latest/gr/elb.html#elb_region)

## Example Usage

```terraform
data "aws_lb_hosted_zone_id" "main" {}
resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.primary.zone_id
name = "example.com"
type = "A"
alias {
name = aws_lb.main.dns_name
zone_id = data.aws_lb_hosted_zone_id.main.id
evaluate_target_health = true
}
}
```

## Argument Reference

* `region` - (Optional) Name of the region whose AWS ELB HostedZoneId is desired.
Defaults to the region from the AWS provider configuration.

* `load_balancer_type` - (Optional) The type of load balancer to create. Possible values are `application` or `network`. The default value is `application`.

## Attributes Reference

* `id` - The ID of the AWS ELB HostedZoneId in the selected region.

0 comments on commit ab0cd6b

Please sign in to comment.