0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS 無料チュートリアルその12: terraform destroy → terraform apply でも https 表示されるように

0
Posted at

AWSの無料枠で出来る範囲のチュートリアルを ChatGPT に示して頂いたので、試してみた経過を簡単にまとめてみました。
AWS等の実際の画面や操作はどんな感じか知りたい方を読者対象としているつもりです。

著者はAWS クラウドプラクティショナー、ソリューションアーキテクト アソシエイト という資格を取得済みで「AWSってなんぞや?」という概要を知識としてある程度理解しているが、実際にAWSの画面を触ったことが無いという状態でAWSのチュートリアルをこなしていくという状況です。

今回の内容としては、

terraform destroy → apply
↓
ALBが作り直されても
↓
https://net-4.net が自動で新しいALBに繋がる

を目標にしたいと思います。

→ そのためには、

Route53をTerraformで管理する

完成イメージ

ALB(新規作成)
↓
Terraformが自動でDNS更新
↓
net-4.net → 新ALB

🏗 追加するコード(そのまま使えます)

📄 main.tf に追加

main.tf
############################################
# Route53(ドメイン → ALB)
############################################

# ホストゾーン取得
data "aws_route53_zone" "main" {
  # ここは皆さんのドメイン名に変更して下さい
  name = "net-4.net"
}

# Aレコード(ALBへ紐付け)
resource "aws_route53_record" "alb_record" {
  zone_id = data.aws_route53_zone.main.zone_id
  # ここは皆さんのドメイン名に変更して下さい
  name    = "net-4.net"
  type    = "A"

  alias {
    name                   = module.alb.alb_dns
    zone_id                = module.alb.alb_zone_id
    evaluate_target_health = true
  }
}

🥈 modules/alb に追加

modules/alb/outputs.tf
output "alb_dns" {
  value = aws_lb.alb.dns_name
}

output "alb_zone_id" {
  value = aws_lb.alb.zone_id
}

🧠 なぜ zone_id が必要?

Route53のエイリアスは
DNS + ZoneID のセットで必要

実行手順

terraform apply

⚠️ 注意

① Hosted Zoneが必要

Route53に net-4.net が存在していること

② 名前は完全一致

net-4.net

エラー

aws_route53_record.alb_record: Creating...
╷
│ Error: creating Route53 Record: operation error Route 53: ChangeResourceRecordSets, https response error StatusCode: 400, RequestID: 9e8416e3-30ce-4691-a250-d958628fac06, InvalidChangeBatch: [Tried to create resource record set [name='net-4.net.', type='A'] but it already exists]
│
│   with aws_route53_record.alb_record,
│   on main.tf line 57, in resource "aws_route53_record" "alb_record":
│   57: resource "aws_route53_record" "alb_record" {
│

🎯 結論

既にRoute53にAレコードが存在している
→ Terraformが新規作成できない

🧠 状況

手動で作ったAレコード(net-4.net)
+
Terraformで同じものを作ろうとしている

👉 だから衝突

解決方法

手動でAレコードを削除

手順

  1. Route53
  2. net-4.net のホストゾーン
  3. Aレコード削除

その後

terraform apply
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?