0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nginx起動 https化 Terraform AWS https化

Posted at

ALBとドメインの紐付けとhttpsの対応

ドメイン作成

  • AWS Certificate Manager
    • TLS証明書の発行
  • Route 53 CNAMEレコード
    • TLS証明書発行時にドメインの所有を証明するために作成
  • AWS Certificate Manager Validation
    • TLS証明書発行時にドメインの所有を証明するために作成
    • ACMでドメインを使用して所有証明をする場合は基本的にCNAMEレコードとワンセットで定義する。
  • Route 53 Aレコード
    • ALBとドメインの紐付け用レコード
  • セキュリティグループルール
    • 作成済みのALB用セキュリティグループへhttpsも受け付けるようルールを追加する
  • ALB httpリスナー
    • httpリクエスト受けつけ、そのリクエストをhttpsへリダイレクトさせるルール
  • ALB httpsリスナー
    • httpsリクエストを受けつけ、そのリクエストを作成済みのECS(nginx)へ流すルール

コードの記述

variable は変数の定義に使用
data は既存のリソースを参照
variable はRoute53で管理しているドメイン名を入力
data でそのドメインを参照してACMの証明書発行やALBとドメインの紐付け

無料のFreenomでドメイン取得
https://my.freenom.com/clientarea.php
Route53でホストゾーン作成
NSレコードが4件生成されるのでFreenomで取得したDNSのNameserverへNSレコードを追記

variable "domain" {
  description = "Route 53 で管理しているドメイン名"
  type        = "string"

  #FIXME:
  default = "<YOUR DOMAIN NAME>"
}

# Route53 Hosted Zone
# https://www.terraform.io/docs/providers/aws/d/route53_zone.html
data "aws_route53_zone" "main" {
  name         = "${var.domain}"
  private_zone = false
}

# ACM
# https://www.terraform.io/docs/providers/aws/r/acm_certificate.html
resource "aws_acm_certificate" "main" {
  domain_name = "${var.domain}"

  validation_method = "DNS"

  lifecycle {
    create_before_destroy = true
  }
}

# Route53 record
# https://www.terraform.io/docs/providers/aws/r/route53_record.html
resource "aws_route53_record" "validation" {
  depends_on = ["aws_acm_certificate.main"]

  zone_id = "${data.aws_route53_zone.main.id}"

  ttl = 60

  name    = "${aws_acm_certificate.main.domain_validation_options.0.resource_record_name}"
  type    = "${aws_acm_certificate.main.domain_validation_options.0.resource_record_type}"
  records = ["${aws_acm_certificate.main.domain_validation_options.0.resource_record_value}"]
}

# ACM Validate
# https://www.terraform.io/docs/providers/aws/r/acm_certificate_validation.html
resource "aws_acm_certificate_validation" "main" {
  certificate_arn = "${aws_acm_certificate.main.arn}"

  validation_record_fqdns = ["${aws_route53_record.validation.0.fqdn}"]
}

# Route53 record
# https://www.terraform.io/docs/providers/aws/r/route53_record.html
resource "aws_route53_record" "main" {
  type = "A"

  name    = "${var.domain}"
  zone_id = "${data.aws_route53_zone.main.id}"

  alias = {
    name                   = "${aws_lb.main.dns_name}"
    zone_id                = "${aws_lb.main.zone_id}"
    evaluate_target_health = true
  }
}

# ALB Listener
# https://www.terraform.io/docs/providers/aws/r/lb_listener.html
resource "aws_lb_listener" "https" {
  load_balancer_arn = "${aws_lb.main.arn}"

  certificate_arn = "${aws_acm_certificate.main.arn}"

  port     = "443"
  protocol = "HTTPS"

  default_action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.main.id}"
  }
}

# ALB Listener Rule
# https://www.terraform.io/docs/providers/aws/r/lb_listener_rule.html
resource "aws_lb_listener_rule" "http_to_https" {
  listener_arn = "${aws_lb_listener.main.arn}"

  priority = 99

  action {
    type = "redirect"

    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }

  condition {
    field  = "host-header"
    values = ["${var.domain}"]
  }
}

# Security Group Rule
# https://www.terraform.io/docs/providers/aws/r/security_group_rule.html
resource "aws_security_group_rule" "alb_https" {
  security_group_id = "${aws_security_group.alb.id}"

  type = "ingress"

  from_port = 443
  to_port   = 443
  protocol  = "tcp"

  cidr_blocks = ["0.0.0.0/0"]
}

planで確認後apply

freenomで作成したドメインへhttpでアクセスし、httpsへリダイレクトされ、nginxが表示されることを確認

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?