Terraform -Route53-

# 目的
terraformでRoute53へZoneを作成する

# 設定
DIRを作成する

```bash
~/terraform/<environment>/<pj-name>/route53/zone/
├── config.tf
├── terraform.tfvars
├── variables.tf
└── zone.tf
```

# template
* config.tf

```bash
///config

provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "${var.region}"
}
```

* variables.tf

```bash
variable "access_key" {}
variable "secret_key" {}
variable "region" {
    default = "ap-northeast-1"
}

```

* terraform.tfvars

```bash
///iam-user
access_key = "*********"
secret_key = "*********************"
```

* zone.tf
    * destroy時にレコードを削除しないよう明示的にfalse

```bash
/// route53 zone

resource "aws_route53_zone" "primary" {
   name = "*****.com"
   force_destroy = "false"
}
```

# テスト

```bash
terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_route53_zone.primary
    comment:        "Managed by Terraform"
    force_destroy:  "false"
    name:           "*****.com"
    name_servers.#: "<computed>"
    vpc_region:     "<computed>"
    zone_id:        "<computed>"


Plan: 1 to add, 0 to change, 0 to destroy.
```

# 実行

```bash
terraform apply
aws_route53_zone.primary: Creating...
  comment:        "" => "Managed by Terraform"
  force_destroy:  "" => "false"
  name:           "" => "*****.com"
  name_servers.#: "" => "<computed>"
  vpc_region:     "" => "<computed>"
  zone_id:        "" => "<computed>"
aws_route53_zone.primary: Still creating... (10s elapsed)
aws_route53_zone.primary: Still creating... (20s elapsed)
aws_route53_zone.primary: Still creating... (30s elapsed)
aws_route53_zone.primary: Still creating... (40s elapsed)
aws_route53_zone.primary: Creation complete

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate
```

# 確認

```bash
terraform show
aws_route53_zone.primary:
  id = ************
  comment = Managed by Terraform
  force_destroy = false
  name = *****.com
  name_servers.# = 4
  name_servers.0 = ns-1345.awsdns-40.org
  name_servers.1 = ns-1877.awsdns-42.co.uk
  name_servers.2 = ns-281.awsdns-35.com
  name_servers.3 = ns-836.awsdns-40.net
  tags.% = 0
  zone_id = ************
```

```bash
aws --profile terraform-user route53 list-hosted-zones
{
    "IsTruncated": false,
    "HostedZones": [
        {
            "Id": "/hostedzone/***********",
            "ResourceRecordSetCount": 8,
            "Name": "*********.xyz.",
            "CallerReference": "*****-****-****-****-*******",
            "Config": {}
        },
        {
            "Id": "/hostedzone/***********",
            "ResourceRecordSetCount": 2,
            "Name": "***.com.",
            "CallerReference": "***********",
            "Config": {
                "Comment": "Managed by Terraform"
            }
        }
    ],
    "MaxItems": "100"
}
```