02-12 14:16
반응형
250x250
Recent Posts
Recent Comments
Link
관리 메뉴

DevOps Tasks

Terraform을 통한 AWS API Gateway 도메인 자동화 본문

DevOps

Terraform을 통한 AWS API Gateway 도메인 자동화

데밥스 2024. 8. 4. 20:43
728x90
반응형

 

Overview

아래의 Terraform 코드는 AWS API Gateway에서 사용자 지정 도메인을 설정하고, 이를 위해 ACM 인증서를 발급하며, 도메인 검증을 위한 Route 53 레코드를 생성하는 과정을 자동화한다.

이 과정은 수동으로 CNAME 레코드를 등록해야 하는 번거로움을 없애준다.

각 리소스는 특정 역할을 가지며, 순차적으로 종속성을 설정하여 올바른 순서대로 실행된다.

 


aws_acm_certificate 리소스

resource "aws_acm_certificate" "example" {
  domain_name       = var.custom_domain_name
  validation_method = "DNS"

  tags = {
    Name = "API Gateway Certificate"
  }
}

역할

ACM (AWS Certificate Manager)에서 사용자 지정 도메인 이름에 대한 SSL/TLS 인증서를 생성

기능

domain_name : 인증서를 발급받을 도메인 이름입니다. 예: "example.com"

validation_method : DNS 검증을 사용하여 도메인 소유권을 확인한다.

tags : 인증서에 태그를 추가하여 나중에 쉽게 식별할 수 있다.

 


aws_route53_zone 데이터 소스

data "aws_route53_zone" "selected" {
  name = var.route53_zone_name
}

역할

지정된 도메인 이름에 해당하는 Route 53 호스팅 영역의 ID를 가져온다.

기능

name : 호스팅 영역의 도메인 이름이다. 예: "example.com"

 

 


aws_route53_record 리소스

resource "aws_route53_record" "example_validation" {
  for_each = {
    for dvo in aws_acm_certificate.example.domain_validation_options : dvo.domain_name => {
      name   = dvo.resource_record_name
      record = dvo.resource_record_value
      type   = dvo.resource_record_type
    }
  }

  allow_overwrite = true
  name            = each.value.name
  records         = [each.value.record]
  ttl             = 300
  type            = each.value.type
  zone_id         = data.aws_route53_zone.selected.zone_id
  depends_on      = [aws_acm_certificate.example]
}

역할

도메인 검증을 위해 ACM에서 제공한 DNS 레코드를 Route 53에 생성한다.

기능

for_each : ACM 인증서의 도메인 검증 옵션마다 DNS 레코드를 생성한다.

allow_overwrite : 기존 레코드가 있으면 덮어쓴다.

name, records, ttl, type : DNS 레코드의 속성이다.

zone_id : 호스팅 영역의 ID이다.

depends_on : 이 리소스는 ACM 인증서 리소스가 먼저 생성된 후에 실행된다.

 

 


aws_acm_certificate_validation 리소스

resource "aws_acm_certificate_validation" "example" {
  certificate_arn         = aws_acm_certificate.example.arn
  validation_record_fqdns = [for opt in aws_acm_certificate.example.domain_validation_options : opt.resource_record_name]
  depends_on              = [aws_route53_record.example_validation]
}

역할

ACM 인증서를 검증한다.

기능

certificate_arn : ACM 인증서의 ARN (Amazon Resource Name)이다.

validation_record_fqdns : 도메인 검증을 위해 DNS 레코드의 이름을 리스트로 만든다.

depends_on : 이 리소스는 Route 53 검증 레코드 리소스가 생성된 후에 실행된다.

 

 


aws_api_gateway_domain_name 리소스

resource "aws_api_gateway_domain_name" "example" {
  domain_name              = var.custom_domain_name
  regional_certificate_arn = aws_acm_certificate.example.arn
  endpoint_configuration {
    types = [var.api_endpoint_type]
  }
  depends_on               = [aws_acm_certificate_validation.example]
}

역할

API Gateway에서 사용자 지정 도메인 이름을 설정한다.

기능

domain_name : API Gateway에서 사용할 사용자 지정 도메인 이름이다.

regional_certificate_arn : ACM 인증서의 ARN이다.

endpoint_configuration : API Gateway의 엔드포인트 유형이다.

예: REGIONAL 또는 EDGE.

depends_on : 이 리소스는 ACM 인증서 검증 리소스가 완료된 후에 실행된다.

 

 


aws_api_gateway_base_path_mapping 리소스

resource "aws_api_gateway_base_path_mapping" "example" {
  api_id      = aws_api_gateway_rest_api.this.id
  domain_name = aws_api_gateway_domain_name.example.id
  stage_name  = module.environment.environment.fullName
  depends_on  = [aws_api_gateway_domain_name.example]
}

역할

API Gateway 사용자 지정 도메인을 특정 API와 매핑한다.

기능

api_id : API Gateway의 ID이다.

domain_name : 사용자 지정 도메인 이름의 ID이다.

stage_name : API Gateway 스테이지 이름이다.

depends_on : 이 리소스는 API Gateway 사용자 지정 도메인 리소스가 완료된 후에 실행된다.

 


Summary

 

인증서 생성

  • aws_acm_certificate 리소스를 통해 지정된 도메인 이름에 대한 인증서를 생성한다.

호스팅 영역 ID 가져오기

  • aws_route53_zone 데이터 소스를 통해 도메인 이름에 해당하는 호스팅 영역의 ID를 가져온다.

DNS 레코드 생성

  • aws_route53_record 리소스를 통해 ACM에서 제공한 도메인 검증 옵션에 따라 Route 53에 DNS 레코드를 생성한다.

인증서 검증

  • aws_acm_certificate_validation 리소스를 통해 도메인 검증이 완료될 때까지 기다린다.

사용자 지정 도메인 생성

  • aws_api_gateway_domain_name 리소스를 통해 API Gateway에서 사용자 지정 도메인을 설정한다.

도메인 매핑

  • aws_api_gateway_base_path_mapping 리소스를 통해 사용자 지정 도메인과 특정 API를 매핑한다.
728x90
반응형