글로벌 변수 세팅

글로벌 변수 세팅

글로벌 변수란?

글로벌 변수는 다른 여러 리소스 생성 시에 자주 참조되는 값들을 모아 놓은 곳입니다. 아래의 예시는 주관적인 관점에서 모아놓은 예시이므로, 필요에 따라 추가하시면 편리하게 사용하실 수 있습니다.

추후에 사용할 때 변수들을 하나씩 넣으셔도 됩니다.

글로벌 변수 정의

먼저, var_global.tf에는 아래의 내용을 포함합니다.

  • Account ID

  • VPC(Amazon Virtual Private Cloud)

  • IAM(AWS Identity and Access Management)

  • WAF(AWS Web Application Firewall)

  • KMS(Amazon Key Management Service)

terraform/variables/var_global.tf
# Atlantis user
variable "atlantis_user" {
  description = "The username that will be triggering atlantis commands. This will be used to name the session when assuming a role. More information - https://github.com/runatlantis/atlantis#assume-role-session-names"
  default     = "atlantis_user"
}

# Account IDs
# Add all account ID to here 
variable "account_id" {
  default = {
    prod     = "xxxxxxxxxx"
  }
}

# Remote State that will be used when creating other resources
# You can add any resource here, if you want to refer from others
variable "remote_state" {
  default = {
    # VPC
    vpc = {
      dayonedapne2 = {
        region = "ap-northeast-2"
        bucket = "dayone-prod-apnortheast2-tfstate"
        key    = "dayone/terraform/vpc/dayoned_apnortheast2/terraform.tfstate"
      }

      dayonepapne2 = {
        region = "ap-northeast-2"
        bucket = "dayone-prod-apnortheast2-tfstate"
        key    = "dayone/terraform/vpc/dayonep_apnortheast2/terraform.tfstate"
      }
    }


    # WAF ACL
    waf_web_acl_global = {
      prod = {
        region = ""
        bucket = ""
        key    = ""
      }
    }


    # AWS IAM
    iam = {
      prod = {
        region = "ap-northeast-2"
        bucket = "dayone-prod-apnortheast2-tfstate"
        key    = "dayone/terraform/iam/dayone-prod/terraform.tfstate"
      }
    }


    # AWS KMS
    kms = {
      prod = {
        apne2 = {
          region = ""
          bucket = ""
          key    = ""
        }
      }
    }
  }
}

도메인 관련 변수 모음

먼저, var_route53.tf에는 아래의 내용을 포함합니다.

  • Route53 Zone ID

  • ACM(Amazon Certificate Manager)

variable "r53_variables" {
  default = {
    prod = {
      dayone_io_zone_id = ""

      star_dayone_io_acm_arn_apnortheast2 = ""
      star_dayone_io_acm_arn_useast1      = ""
      www_dayone_io_acm_arn_useast1       = ""
    }
  }
}

글로벌 변수 사용하기

  • 상위 폴더에 정의된 글로벌 변수를 사용하기 위해서는 해당 파일을 디렉토리로 옮겨야 합니다.

  • 이때 효율적인 코드 운영을 위해서 soft link를 생성해서 사용합니다.

코드를 복사하실 때 cp 명령어에 -R 옵션을 주면 soft link를 그대로 복사합니다.

# Use Soft Link
$ ln -s ../path/to/variables/var_route53.tf
$ ln -s ../path/to/variables/var_global.tf

Last updated