github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_route53_record_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/route53"
    15  )
    16  
    17  func TestCleanRecordName(t *testing.T) {
    18  	cases := []struct {
    19  		Input, Output string
    20  	}{
    21  		{"www.nonexample.com", "www.nonexample.com"},
    22  		{"\\052.nonexample.com", "*.nonexample.com"},
    23  		{"nonexample.com", "nonexample.com"},
    24  	}
    25  
    26  	for _, tc := range cases {
    27  		actual := cleanRecordName(tc.Input)
    28  		if actual != tc.Output {
    29  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    30  		}
    31  	}
    32  }
    33  
    34  func TestExpandRecordName(t *testing.T) {
    35  	cases := []struct {
    36  		Input, Output string
    37  	}{
    38  		{"www", "www.nonexample.com"},
    39  		{"dev.www", "dev.www.nonexample.com"},
    40  		{"*", "*.nonexample.com"},
    41  		{"nonexample.com", "nonexample.com"},
    42  		{"test.nonexample.com", "test.nonexample.com"},
    43  		{"test.nonexample.com.", "test.nonexample.com"},
    44  	}
    45  
    46  	zone_name := "nonexample.com"
    47  	for _, tc := range cases {
    48  		actual := expandRecordName(tc.Input, zone_name)
    49  		if actual != tc.Output {
    50  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    51  		}
    52  	}
    53  }
    54  
    55  func TestAccAWSRoute53Record_basic(t *testing.T) {
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:      func() { testAccPreCheck(t) },
    58  		IDRefreshName: "aws_route53_record.default",
    59  		Providers:     testAccProviders,
    60  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccRoute53RecordConfig,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func TestAccAWSRoute53Record_basic_fqdn(t *testing.T) {
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:      func() { testAccPreCheck(t) },
    75  		IDRefreshName: "aws_route53_record.default",
    76  		Providers:     testAccProviders,
    77  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
    78  		Steps: []resource.TestStep{
    79  			resource.TestStep{
    80  				Config: testAccRoute53RecordConfig_fqdn,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
    83  				),
    84  			},
    85  
    86  			// Ensure that changing the name to include a trailing "dot" results in
    87  			// nothing happening, because the name is stripped of trailing dots on
    88  			// save. Otherwise, an update would occur and due to the
    89  			// create_before_destroy, the record would actually be destroyed, and a
    90  			// non-empty plan would appear, and the record will fail to exist in
    91  			// testAccCheckRoute53RecordExists
    92  			resource.TestStep{
    93  				Config: testAccRoute53RecordConfig_fqdn_no_op,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
    96  				),
    97  			},
    98  		},
    99  	})
   100  }
   101  
   102  func TestAccAWSRoute53Record_txtSupport(t *testing.T) {
   103  	resource.Test(t, resource.TestCase{
   104  		PreCheck:        func() { testAccPreCheck(t) },
   105  		IDRefreshName:   "aws_route53_record.default",
   106  		IDRefreshIgnore: []string{"zone_id"}, // just for this test
   107  		Providers:       testAccProviders,
   108  		CheckDestroy:    testAccCheckRoute53RecordDestroy,
   109  		Steps: []resource.TestStep{
   110  			resource.TestStep{
   111  				Config: testAccRoute53RecordConfigTXT,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
   114  				),
   115  			},
   116  		},
   117  	})
   118  }
   119  
   120  func TestAccAWSRoute53Record_spfSupport(t *testing.T) {
   121  	resource.Test(t, resource.TestCase{
   122  		PreCheck:      func() { testAccPreCheck(t) },
   123  		IDRefreshName: "aws_route53_record.default",
   124  		Providers:     testAccProviders,
   125  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   126  		Steps: []resource.TestStep{
   127  			resource.TestStep{
   128  				Config: testAccRoute53RecordConfigSPF,
   129  				Check: resource.ComposeTestCheckFunc(
   130  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
   131  					resource.TestCheckResourceAttr(
   132  						"aws_route53_record.default", "records.2930149397", "include:notexample.com"),
   133  				),
   134  			},
   135  		},
   136  	})
   137  }
   138  func TestAccAWSRoute53Record_generatesSuffix(t *testing.T) {
   139  	resource.Test(t, resource.TestCase{
   140  		PreCheck:      func() { testAccPreCheck(t) },
   141  		IDRefreshName: "aws_route53_record.default",
   142  		Providers:     testAccProviders,
   143  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   144  		Steps: []resource.TestStep{
   145  			resource.TestStep{
   146  				Config: testAccRoute53RecordConfigSuffix,
   147  				Check: resource.ComposeTestCheckFunc(
   148  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
   149  				),
   150  			},
   151  		},
   152  	})
   153  }
   154  
   155  func TestAccAWSRoute53Record_wildcard(t *testing.T) {
   156  	resource.Test(t, resource.TestCase{
   157  		PreCheck:      func() { testAccPreCheck(t) },
   158  		IDRefreshName: "aws_route53_record.wildcard",
   159  		Providers:     testAccProviders,
   160  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   161  		Steps: []resource.TestStep{
   162  			resource.TestStep{
   163  				Config: testAccRoute53WildCardRecordConfig,
   164  				Check: resource.ComposeTestCheckFunc(
   165  					testAccCheckRoute53RecordExists("aws_route53_record.wildcard"),
   166  				),
   167  			},
   168  
   169  			// Cause a change, which will trigger a refresh
   170  			resource.TestStep{
   171  				Config: testAccRoute53WildCardRecordConfigUpdate,
   172  				Check: resource.ComposeTestCheckFunc(
   173  					testAccCheckRoute53RecordExists("aws_route53_record.wildcard"),
   174  				),
   175  			},
   176  		},
   177  	})
   178  }
   179  
   180  func TestAccAWSRoute53Record_failover(t *testing.T) {
   181  	resource.Test(t, resource.TestCase{
   182  		PreCheck:      func() { testAccPreCheck(t) },
   183  		IDRefreshName: "aws_route53_record.www-primary",
   184  		Providers:     testAccProviders,
   185  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   186  		Steps: []resource.TestStep{
   187  			resource.TestStep{
   188  				Config: testAccRoute53FailoverCNAMERecord,
   189  				Check: resource.ComposeTestCheckFunc(
   190  					testAccCheckRoute53RecordExists("aws_route53_record.www-primary"),
   191  					testAccCheckRoute53RecordExists("aws_route53_record.www-secondary"),
   192  				),
   193  			},
   194  		},
   195  	})
   196  }
   197  
   198  func TestAccAWSRoute53Record_weighted_basic(t *testing.T) {
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:      func() { testAccPreCheck(t) },
   201  		IDRefreshName: "aws_route53_record.www-live",
   202  		Providers:     testAccProviders,
   203  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   204  		Steps: []resource.TestStep{
   205  			resource.TestStep{
   206  				Config: testAccRoute53WeightedCNAMERecord,
   207  				Check: resource.ComposeTestCheckFunc(
   208  					testAccCheckRoute53RecordExists("aws_route53_record.www-dev"),
   209  					testAccCheckRoute53RecordExists("aws_route53_record.www-live"),
   210  					testAccCheckRoute53RecordExists("aws_route53_record.www-off"),
   211  				),
   212  			},
   213  		},
   214  	})
   215  }
   216  
   217  func TestAccAWSRoute53Record_alias(t *testing.T) {
   218  	rs := acctest.RandString(10)
   219  	config := fmt.Sprintf(testAccRoute53ElbAliasRecord, rs)
   220  	resource.Test(t, resource.TestCase{
   221  		PreCheck:      func() { testAccPreCheck(t) },
   222  		IDRefreshName: "aws_route53_record.alias",
   223  		Providers:     testAccProviders,
   224  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   225  		Steps: []resource.TestStep{
   226  			resource.TestStep{
   227  				Config: config,
   228  				Check: resource.ComposeTestCheckFunc(
   229  					testAccCheckRoute53RecordExists("aws_route53_record.alias"),
   230  				),
   231  			},
   232  		},
   233  	})
   234  }
   235  
   236  func TestAccAWSRoute53Record_s3_alias(t *testing.T) {
   237  	resource.Test(t, resource.TestCase{
   238  		PreCheck:     func() { testAccPreCheck(t) },
   239  		Providers:    testAccProviders,
   240  		CheckDestroy: testAccCheckRoute53RecordDestroy,
   241  		Steps: []resource.TestStep{
   242  			resource.TestStep{
   243  				Config: testAccRoute53S3AliasRecord,
   244  				Check: resource.ComposeTestCheckFunc(
   245  					testAccCheckRoute53RecordExists("aws_route53_record.alias"),
   246  				),
   247  			},
   248  		},
   249  	})
   250  }
   251  
   252  func TestAccAWSRoute53Record_weighted_alias(t *testing.T) {
   253  	resource.Test(t, resource.TestCase{
   254  		PreCheck:      func() { testAccPreCheck(t) },
   255  		IDRefreshName: "aws_route53_record.elb_weighted_alias_live",
   256  		Providers:     testAccProviders,
   257  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   258  		Steps: []resource.TestStep{
   259  			resource.TestStep{
   260  				Config: testAccRoute53WeightedElbAliasRecord,
   261  				Check: resource.ComposeTestCheckFunc(
   262  					testAccCheckRoute53RecordExists("aws_route53_record.elb_weighted_alias_live"),
   263  					testAccCheckRoute53RecordExists("aws_route53_record.elb_weighted_alias_dev"),
   264  				),
   265  			},
   266  
   267  			resource.TestStep{
   268  				Config: testAccRoute53WeightedR53AliasRecord,
   269  				Check: resource.ComposeTestCheckFunc(
   270  					testAccCheckRoute53RecordExists("aws_route53_record.green_origin"),
   271  					testAccCheckRoute53RecordExists("aws_route53_record.r53_weighted_alias_live"),
   272  					testAccCheckRoute53RecordExists("aws_route53_record.blue_origin"),
   273  					testAccCheckRoute53RecordExists("aws_route53_record.r53_weighted_alias_dev"),
   274  				),
   275  			},
   276  		},
   277  	})
   278  }
   279  
   280  func TestAccAWSRoute53Record_geolocation_basic(t *testing.T) {
   281  	resource.Test(t, resource.TestCase{
   282  		PreCheck:     func() { testAccPreCheck(t) },
   283  		Providers:    testAccProviders,
   284  		CheckDestroy: testAccCheckRoute53RecordDestroy,
   285  		Steps: []resource.TestStep{
   286  			resource.TestStep{
   287  				Config: testAccRoute53GeolocationCNAMERecord,
   288  				Check: resource.ComposeTestCheckFunc(
   289  					testAccCheckRoute53RecordExists("aws_route53_record.default"),
   290  					testAccCheckRoute53RecordExists("aws_route53_record.california"),
   291  					testAccCheckRoute53RecordExists("aws_route53_record.oceania"),
   292  					testAccCheckRoute53RecordExists("aws_route53_record.denmark"),
   293  				),
   294  			},
   295  		},
   296  	})
   297  }
   298  
   299  func TestAccAWSRoute53Record_latency_basic(t *testing.T) {
   300  	resource.Test(t, resource.TestCase{
   301  		PreCheck:     func() { testAccPreCheck(t) },
   302  		Providers:    testAccProviders,
   303  		CheckDestroy: testAccCheckRoute53RecordDestroy,
   304  		Steps: []resource.TestStep{
   305  			resource.TestStep{
   306  				Config: testAccRoute53LatencyCNAMERecord,
   307  				Check: resource.ComposeTestCheckFunc(
   308  					testAccCheckRoute53RecordExists("aws_route53_record.us-east-1"),
   309  					testAccCheckRoute53RecordExists("aws_route53_record.eu-west-1"),
   310  					testAccCheckRoute53RecordExists("aws_route53_record.ap-northeast-1"),
   311  				),
   312  			},
   313  		},
   314  	})
   315  }
   316  
   317  func TestAccAWSRoute53Record_TypeChange(t *testing.T) {
   318  	resource.Test(t, resource.TestCase{
   319  		PreCheck:      func() { testAccPreCheck(t) },
   320  		IDRefreshName: "aws_route53_record.sample",
   321  		Providers:     testAccProviders,
   322  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   323  		Steps: []resource.TestStep{
   324  			resource.TestStep{
   325  				Config: testAccRoute53RecordTypeChangePre,
   326  				Check: resource.ComposeTestCheckFunc(
   327  					testAccCheckRoute53RecordExists("aws_route53_record.sample"),
   328  				),
   329  			},
   330  
   331  			// Cause a change, which will trigger a refresh
   332  			resource.TestStep{
   333  				Config: testAccRoute53RecordTypeChangePost,
   334  				Check: resource.ComposeTestCheckFunc(
   335  					testAccCheckRoute53RecordExists("aws_route53_record.sample"),
   336  				),
   337  			},
   338  		},
   339  	})
   340  }
   341  
   342  func TestAccAWSRoute53Record_empty(t *testing.T) {
   343  	resource.Test(t, resource.TestCase{
   344  		PreCheck:      func() { testAccPreCheck(t) },
   345  		IDRefreshName: "aws_route53_record.empty",
   346  		Providers:     testAccProviders,
   347  		CheckDestroy:  testAccCheckRoute53RecordDestroy,
   348  		Steps: []resource.TestStep{
   349  			resource.TestStep{
   350  				Config: testAccRoute53RecordConfigEmptyName,
   351  				Check: resource.ComposeTestCheckFunc(
   352  					testAccCheckRoute53RecordExists("aws_route53_record.empty"),
   353  				),
   354  			},
   355  		},
   356  	})
   357  }
   358  
   359  func testAccCheckRoute53RecordDestroy(s *terraform.State) error {
   360  	conn := testAccProvider.Meta().(*AWSClient).r53conn
   361  	for _, rs := range s.RootModule().Resources {
   362  		if rs.Type != "aws_route53_record" {
   363  			continue
   364  		}
   365  
   366  		parts := strings.Split(rs.Primary.ID, "_")
   367  		zone := parts[0]
   368  		name := parts[1]
   369  		rType := parts[2]
   370  
   371  		en := expandRecordName(name, "notexample.com")
   372  
   373  		lopts := &route53.ListResourceRecordSetsInput{
   374  			HostedZoneId:    aws.String(cleanZoneID(zone)),
   375  			StartRecordName: aws.String(en),
   376  			StartRecordType: aws.String(rType),
   377  		}
   378  
   379  		resp, err := conn.ListResourceRecordSets(lopts)
   380  		if err != nil {
   381  			if awsErr, ok := err.(awserr.Error); ok {
   382  				// if NoSuchHostedZone, then all the things are destroyed
   383  				if awsErr.Code() == "NoSuchHostedZone" {
   384  					return nil
   385  				}
   386  			}
   387  			return err
   388  		}
   389  		if len(resp.ResourceRecordSets) == 0 {
   390  			return nil
   391  		}
   392  		rec := resp.ResourceRecordSets[0]
   393  		if FQDN(*rec.Name) == FQDN(name) && *rec.Type == rType {
   394  			return fmt.Errorf("Record still exists: %#v", rec)
   395  		}
   396  	}
   397  	return nil
   398  }
   399  
   400  func testAccCheckRoute53RecordExists(n string) resource.TestCheckFunc {
   401  	return func(s *terraform.State) error {
   402  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   403  		rs, ok := s.RootModule().Resources[n]
   404  		if !ok {
   405  			return fmt.Errorf("Not found: %s", n)
   406  		}
   407  
   408  		if rs.Primary.ID == "" {
   409  			return fmt.Errorf("No hosted zone ID is set")
   410  		}
   411  
   412  		parts := strings.Split(rs.Primary.ID, "_")
   413  		zone := parts[0]
   414  		name := parts[1]
   415  		rType := parts[2]
   416  
   417  		en := expandRecordName(name, "notexample.com")
   418  
   419  		lopts := &route53.ListResourceRecordSetsInput{
   420  			HostedZoneId:    aws.String(cleanZoneID(zone)),
   421  			StartRecordName: aws.String(en),
   422  			StartRecordType: aws.String(rType),
   423  		}
   424  
   425  		resp, err := conn.ListResourceRecordSets(lopts)
   426  		if err != nil {
   427  			return err
   428  		}
   429  		if len(resp.ResourceRecordSets) == 0 {
   430  			return fmt.Errorf("Record does not exist")
   431  		}
   432  
   433  		// rec := resp.ResourceRecordSets[0]
   434  		for _, rec := range resp.ResourceRecordSets {
   435  			recName := cleanRecordName(*rec.Name)
   436  			if FQDN(strings.ToLower(recName)) == FQDN(strings.ToLower(en)) && *rec.Type == rType {
   437  				return nil
   438  			}
   439  		}
   440  		return fmt.Errorf("Record does not exist: %#v", rs.Primary.ID)
   441  	}
   442  }
   443  
   444  const testAccRoute53RecordConfig = `
   445  resource "aws_route53_zone" "main" {
   446  	name = "notexample.com"
   447  }
   448  
   449  resource "aws_route53_record" "default" {
   450  	zone_id = "${aws_route53_zone.main.zone_id}"
   451  	name = "www.NOTexamplE.com"
   452  	type = "A"
   453  	ttl = "30"
   454  	records = ["127.0.0.1", "127.0.0.27"]
   455  }
   456  `
   457  
   458  const testAccRoute53RecordConfigCNAMERecord = `
   459  resource "aws_route53_zone" "main" {
   460  	name = "notexample.com"
   461  }
   462  
   463  resource "aws_route53_record" "default" {
   464  	zone_id = "${aws_route53_zone.main.zone_id}"
   465  	name = "host123.domain"
   466  	type = "CNAME"
   467  	ttl = "30"
   468  	records = ["1.2.3.4"]
   469  }
   470  `
   471  
   472  const testAccRoute53RecordConfigCNAMERecordUpdateToCNAME = `
   473  resource "aws_route53_zone" "main" {
   474  	name = "notexample.com"
   475  }
   476  
   477  resource "aws_route53_record" "default" {
   478  	zone_id = "${aws_route53_zone.main.zone_id}"
   479  	name = "host123.domain"
   480  	type = "A"
   481  	ttl = "30"
   482  	records = ["1.2.3.4"]
   483  }
   484  `
   485  
   486  const testAccRoute53RecordConfig_fqdn = `
   487  resource "aws_route53_zone" "main" {
   488    name = "notexample.com"
   489  }
   490  
   491  resource "aws_route53_record" "default" {
   492    zone_id = "${aws_route53_zone.main.zone_id}"
   493    name    = "www.NOTexamplE.com"
   494    type    = "A"
   495    ttl     = "30"
   496    records = ["127.0.0.1", "127.0.0.27"]
   497  
   498    lifecycle {
   499      create_before_destroy = true
   500    }
   501  }
   502  `
   503  
   504  const testAccRoute53RecordConfig_fqdn_no_op = `
   505  resource "aws_route53_zone" "main" {
   506    name = "notexample.com"
   507  }
   508  
   509  resource "aws_route53_record" "default" {
   510    zone_id = "${aws_route53_zone.main.zone_id}"
   511    name    = "www.NOTexamplE.com."
   512    type    = "A"
   513    ttl     = "30"
   514    records = ["127.0.0.1", "127.0.0.27"]
   515  
   516    lifecycle {
   517      create_before_destroy = true
   518    }
   519  }
   520  `
   521  
   522  const testAccRoute53RecordNoConfig = `
   523  resource "aws_route53_zone" "main" {
   524  	name = "notexample.com"
   525  }
   526  `
   527  
   528  const testAccRoute53RecordConfigSuffix = `
   529  resource "aws_route53_zone" "main" {
   530  	name = "notexample.com"
   531  }
   532  
   533  resource "aws_route53_record" "default" {
   534  	zone_id = "${aws_route53_zone.main.zone_id}"
   535  	name = "subdomain"
   536  	type = "A"
   537  	ttl = "30"
   538  	records = ["127.0.0.1", "127.0.0.27"]
   539  }
   540  `
   541  
   542  const testAccRoute53WildCardRecordConfig = `
   543  resource "aws_route53_zone" "main" {
   544      name = "notexample.com"
   545  }
   546  
   547  resource "aws_route53_record" "default" {
   548  	zone_id = "${aws_route53_zone.main.zone_id}"
   549  	name = "subdomain"
   550  	type = "A"
   551  	ttl = "30"
   552  	records = ["127.0.0.1", "127.0.0.27"]
   553  }
   554  
   555  resource "aws_route53_record" "wildcard" {
   556      zone_id = "${aws_route53_zone.main.zone_id}"
   557      name = "*.notexample.com"
   558      type = "A"
   559      ttl = "30"
   560      records = ["127.0.0.1"]
   561  }
   562  `
   563  
   564  const testAccRoute53WildCardRecordConfigUpdate = `
   565  resource "aws_route53_zone" "main" {
   566      name = "notexample.com"
   567  }
   568  
   569  resource "aws_route53_record" "default" {
   570  	zone_id = "${aws_route53_zone.main.zone_id}"
   571  	name = "subdomain"
   572  	type = "A"
   573  	ttl = "30"
   574  	records = ["127.0.0.1", "127.0.0.27"]
   575  }
   576  
   577  resource "aws_route53_record" "wildcard" {
   578      zone_id = "${aws_route53_zone.main.zone_id}"
   579      name = "*.notexample.com"
   580      type = "A"
   581      ttl = "60"
   582      records = ["127.0.0.1"]
   583  }
   584  `
   585  const testAccRoute53RecordConfigTXT = `
   586  resource "aws_route53_zone" "main" {
   587  	name = "notexample.com"
   588  }
   589  
   590  resource "aws_route53_record" "default" {
   591  	zone_id = "/hostedzone/${aws_route53_zone.main.zone_id}"
   592  	name = "subdomain"
   593  	type = "TXT"
   594  	ttl = "30"
   595  	records = ["lalalala"]
   596  }
   597  `
   598  const testAccRoute53RecordConfigSPF = `
   599  resource "aws_route53_zone" "main" {
   600  	name = "notexample.com"
   601  }
   602  
   603  resource "aws_route53_record" "default" {
   604  	zone_id = "${aws_route53_zone.main.zone_id}"
   605  	name = "test"
   606  	type = "SPF"
   607  	ttl = "30"
   608  	records = ["include:notexample.com"]
   609  }
   610  `
   611  
   612  const testAccRoute53FailoverCNAMERecord = `
   613  resource "aws_route53_zone" "main" {
   614  	name = "notexample.com"
   615  }
   616  
   617  resource "aws_route53_health_check" "foo" {
   618    fqdn = "dev.notexample.com"
   619    port = 80
   620    type = "HTTP"
   621    resource_path = "/"
   622    failure_threshold = "2"
   623    request_interval = "30"
   624  
   625    tags = {
   626      Name = "tf-test-health-check"
   627     }
   628  }
   629  
   630  resource "aws_route53_record" "www-primary" {
   631    zone_id = "${aws_route53_zone.main.zone_id}"
   632    name = "www"
   633    type = "CNAME"
   634    ttl = "5"
   635    failover_routing_policy {
   636      type = "PRIMARY"
   637    }
   638    health_check_id = "${aws_route53_health_check.foo.id}"
   639    set_identifier = "www-primary"
   640    records = ["primary.notexample.com"]
   641  }
   642  
   643  resource "aws_route53_record" "www-secondary" {
   644    zone_id = "${aws_route53_zone.main.zone_id}"
   645    name = "www"
   646    type = "CNAME"
   647    ttl = "5"
   648    failover_routing_policy {
   649      type = "SECONDARY"
   650    }
   651    set_identifier = "www-secondary"
   652    records = ["secondary.notexample.com"]
   653  }
   654  `
   655  
   656  const testAccRoute53WeightedCNAMERecord = `
   657  resource "aws_route53_zone" "main" {
   658  	name = "notexample.com"
   659  }
   660  
   661  resource "aws_route53_record" "www-dev" {
   662    zone_id = "${aws_route53_zone.main.zone_id}"
   663    name = "www"
   664    type = "CNAME"
   665    ttl = "5"
   666    weighted_routing_policy {
   667  	weight = 10
   668    }
   669    set_identifier = "dev"
   670    records = ["dev.notexample.com"]
   671  }
   672  
   673  resource "aws_route53_record" "www-live" {
   674    zone_id = "${aws_route53_zone.main.zone_id}"
   675    name = "www"
   676    type = "CNAME"
   677    ttl = "5"
   678    weighted_routing_policy {
   679  	weight = 90
   680    }
   681    set_identifier = "live"
   682    records = ["dev.notexample.com"]
   683  }
   684  
   685  resource "aws_route53_record" "www-off" {
   686    zone_id = "${aws_route53_zone.main.zone_id}"
   687    name = "www"
   688    type = "CNAME"
   689    ttl = "5"
   690    weighted_routing_policy = {
   691  	weight = 0
   692    }
   693    set_identifier = "off"
   694    records = ["dev.notexample.com"]
   695  }
   696  `
   697  
   698  const testAccRoute53GeolocationCNAMERecord = `
   699  resource "aws_route53_zone" "main" {
   700    name = "notexample.com"
   701  }
   702  
   703  resource "aws_route53_record" "default" {
   704    zone_id = "${aws_route53_zone.main.zone_id}"
   705    name = "www"
   706    type = "CNAME"
   707    ttl = "5"
   708    geolocation_routing_policy {
   709      country = "*"
   710    }
   711    set_identifier = "Default"
   712    records = ["dev.notexample.com"]
   713  }
   714  
   715  resource "aws_route53_record" "california" {
   716    zone_id = "${aws_route53_zone.main.zone_id}"
   717    name = "www"
   718    type = "CNAME"
   719    ttl = "5"
   720    geolocation_routing_policy {
   721      country = "US"
   722      subdivision = "CA"
   723    }
   724    set_identifier = "California"
   725    records = ["dev.notexample.com"]
   726  }
   727  
   728  resource "aws_route53_record" "oceania" {
   729    zone_id = "${aws_route53_zone.main.zone_id}"
   730    name = "www"
   731    type = "CNAME"
   732    ttl = "5"
   733    geolocation_routing_policy {
   734      continent = "OC"
   735    }
   736    set_identifier = "Oceania"
   737    records = ["dev.notexample.com"]
   738  }
   739  
   740  resource "aws_route53_record" "denmark" {
   741    zone_id = "${aws_route53_zone.main.zone_id}"
   742    name = "www"
   743    type = "CNAME"
   744    ttl = "5"
   745    geolocation_routing_policy {
   746      country = "DK"
   747    }
   748    set_identifier = "Denmark"
   749    records = ["dev.notexample.com"]
   750  }
   751  `
   752  
   753  const testAccRoute53LatencyCNAMERecord = `
   754  resource "aws_route53_zone" "main" {
   755    name = "notexample.com"
   756  }
   757  
   758  resource "aws_route53_record" "us-east-1" {
   759    zone_id = "${aws_route53_zone.main.zone_id}"
   760    name = "www"
   761    type = "CNAME"
   762    ttl = "5"
   763    latency_routing_policy {
   764      region = "us-east-1"
   765    }
   766    set_identifier = "us-east-1"
   767    records = ["dev.notexample.com"]
   768  }
   769  
   770  resource "aws_route53_record" "eu-west-1" {
   771    zone_id = "${aws_route53_zone.main.zone_id}"
   772    name = "www"
   773    type = "CNAME"
   774    ttl = "5"
   775    latency_routing_policy {
   776      region = "eu-west-1"
   777    }
   778    set_identifier = "eu-west-1"
   779    records = ["dev.notexample.com"]
   780  }
   781  
   782  resource "aws_route53_record" "ap-northeast-1" {
   783    zone_id = "${aws_route53_zone.main.zone_id}"
   784    name = "www"
   785    type = "CNAME"
   786    ttl = "5"
   787    latency_routing_policy {
   788      region = "ap-northeast-1"
   789    }
   790    set_identifier = "ap-northeast-1"
   791    records = ["dev.notexample.com"]
   792  }
   793  `
   794  
   795  const testAccRoute53ElbAliasRecord = `
   796  resource "aws_route53_zone" "main" {
   797    name = "notexample.com"
   798  }
   799  
   800  resource "aws_route53_record" "alias" {
   801    zone_id = "${aws_route53_zone.main.zone_id}"
   802    name = "www"
   803    type = "A"
   804  
   805    alias {
   806    	zone_id = "${aws_elb.main.zone_id}"
   807    	name = "${aws_elb.main.dns_name}"
   808    	evaluate_target_health = true
   809    }
   810  }
   811  
   812  resource "aws_elb" "main" {
   813    name = "foobar-terraform-elb-%s"
   814    availability_zones = ["us-west-2a"]
   815  
   816    listener {
   817      instance_port = 80
   818      instance_protocol = "http"
   819      lb_port = 80
   820      lb_protocol = "http"
   821    }
   822  }
   823  `
   824  
   825  const testAccRoute53AliasRecord = `
   826  resource "aws_route53_zone" "main" {
   827    name = "notexample.com"
   828  }
   829  
   830  resource "aws_route53_record" "origin" {
   831    zone_id = "${aws_route53_zone.main.zone_id}"
   832    name = "origin"
   833    type = "A"
   834    ttl = 5
   835    records = ["127.0.0.1"]
   836  }
   837  
   838  resource "aws_route53_record" "alias" {
   839    zone_id = "${aws_route53_zone.main.zone_id}"
   840    name = "www"
   841    type = "A"
   842  
   843    alias {
   844      zone_id = "${aws_route53_zone.main.zone_id}"
   845      name = "${aws_route53_record.origin.name}.${aws_route53_zone.main.name}"
   846      evaluate_target_health = true
   847    }
   848  }
   849  `
   850  
   851  const testAccRoute53S3AliasRecord = `
   852  resource "aws_route53_zone" "main" {
   853    name = "notexample.com"
   854  }
   855  
   856  resource "aws_s3_bucket" "website" {
   857    bucket = "website.notexample.com"
   858  	acl = "public-read"
   859  	website {
   860  		index_document = "index.html"
   861  	}
   862  }
   863  
   864  resource "aws_route53_record" "alias" {
   865    zone_id = "${aws_route53_zone.main.zone_id}"
   866    name = "www"
   867    type = "A"
   868  
   869    alias {
   870      zone_id = "${aws_s3_bucket.website.hosted_zone_id}"
   871      name = "${aws_s3_bucket.website.website_domain}"
   872      evaluate_target_health = true
   873    }
   874  }
   875  `
   876  
   877  const testAccRoute53WeightedElbAliasRecord = `
   878  resource "aws_route53_zone" "main" {
   879    name = "notexample.com"
   880  }
   881  
   882  resource "aws_elb" "live" {
   883    name = "foobar-terraform-elb-live"
   884    availability_zones = ["us-west-2a"]
   885  
   886    listener {
   887      instance_port = 80
   888      instance_protocol = "http"
   889      lb_port = 80
   890      lb_protocol = "http"
   891    }
   892  }
   893  
   894  resource "aws_route53_record" "elb_weighted_alias_live" {
   895    zone_id = "${aws_route53_zone.main.zone_id}"
   896    name = "www"
   897    type = "A"
   898  
   899    weighted_routing_policy {
   900  	weight = 90
   901    }
   902    set_identifier = "live"
   903  
   904    alias {
   905      zone_id = "${aws_elb.live.zone_id}"
   906      name = "${aws_elb.live.dns_name}"
   907      evaluate_target_health = true
   908    }
   909  }
   910  
   911  resource "aws_elb" "dev" {
   912    name = "foobar-terraform-elb-dev"
   913    availability_zones = ["us-west-2a"]
   914  
   915    listener {
   916      instance_port = 80
   917      instance_protocol = "http"
   918      lb_port = 80
   919      lb_protocol = "http"
   920    }
   921  }
   922  
   923  resource "aws_route53_record" "elb_weighted_alias_dev" {
   924    zone_id = "${aws_route53_zone.main.zone_id}"
   925    name = "www"
   926    type = "A"
   927  
   928    weighted_routing_policy {
   929  	weight = 10
   930    }
   931    set_identifier = "dev"
   932  
   933    alias {
   934      zone_id = "${aws_elb.dev.zone_id}"
   935      name = "${aws_elb.dev.dns_name}"
   936      evaluate_target_health = true
   937    }
   938  }
   939  `
   940  
   941  const testAccRoute53WeightedR53AliasRecord = `
   942  resource "aws_route53_zone" "main" {
   943    name = "notexample.com"
   944  }
   945  
   946  resource "aws_route53_record" "blue_origin" {
   947    zone_id = "${aws_route53_zone.main.zone_id}"
   948    name = "blue-origin"
   949    type = "CNAME"
   950    ttl = 5
   951    records = ["v1.terraform.io"]
   952  }
   953  
   954  resource "aws_route53_record" "r53_weighted_alias_live" {
   955    zone_id = "${aws_route53_zone.main.zone_id}"
   956    name = "www"
   957    type = "CNAME"
   958  
   959    weighted_routing_policy {
   960  	weight = 90
   961    }
   962    set_identifier = "blue"
   963  
   964    alias {
   965      zone_id = "${aws_route53_zone.main.zone_id}"
   966      name = "${aws_route53_record.blue_origin.name}.${aws_route53_zone.main.name}"
   967      evaluate_target_health = false
   968    }
   969  }
   970  
   971  resource "aws_route53_record" "green_origin" {
   972    zone_id = "${aws_route53_zone.main.zone_id}"
   973    name = "green-origin"
   974    type = "CNAME"
   975    ttl = 5
   976    records = ["v2.terraform.io"]
   977  }
   978  
   979  resource "aws_route53_record" "r53_weighted_alias_dev" {
   980    zone_id = "${aws_route53_zone.main.zone_id}"
   981    name = "www"
   982    type = "CNAME"
   983  
   984    weighted_routing_policy {
   985  	weight = 10
   986    }
   987    set_identifier = "green"
   988  
   989    alias {
   990      zone_id = "${aws_route53_zone.main.zone_id}"
   991      name = "${aws_route53_record.green_origin.name}.${aws_route53_zone.main.name}"
   992      evaluate_target_health = false
   993    }
   994  }
   995  `
   996  
   997  const testAccRoute53RecordTypeChangePre = `
   998  resource "aws_route53_zone" "main" {
   999  	name = "notexample.com"
  1000  }
  1001  
  1002  resource "aws_route53_record" "sample" {
  1003  	zone_id = "${aws_route53_zone.main.zone_id}"
  1004    name = "sample"
  1005    type = "CNAME"
  1006    ttl = "30"
  1007    records = ["www.terraform.io"]
  1008  }
  1009  `
  1010  
  1011  const testAccRoute53RecordTypeChangePost = `
  1012  resource "aws_route53_zone" "main" {
  1013  	name = "notexample.com"
  1014  }
  1015  
  1016  resource "aws_route53_record" "sample" {
  1017    zone_id = "${aws_route53_zone.main.zone_id}"
  1018    name = "sample"
  1019    type = "A"
  1020    ttl = "30"
  1021    records = ["127.0.0.1", "8.8.8.8"]
  1022  }
  1023  `
  1024  
  1025  const testAccRoute53RecordConfigEmptyName = `
  1026  resource "aws_route53_zone" "main" {
  1027  	name = "notexample.com"
  1028  }
  1029  
  1030  resource "aws_route53_record" "empty" {
  1031  	zone_id = "${aws_route53_zone.main.zone_id}"
  1032  	name = ""
  1033  	type = "A"
  1034  	ttl = "30"
  1035  	records = ["127.0.0.1"]
  1036  }
  1037  `