• EnglishEspañol日本語한국어Português
  • ログイン今すぐ開始

この機械翻訳は参考用に提供されます。

英語版と翻訳版に矛盾がある場合は、英語版が優先されます。詳細については、このページ を参照してください。

問題を作成する

Terragrunt を使用して複数の環境を管理する

Terraform は、 HashiCorp によって構築された、人気のあるコードとしてのインフラストラクチャ ソフトウェア ツールです。これを使用して、New Relic を含むあらゆる種類のインフラストラクチャとサービスをプロビジョニングします。 とアラート。

Terragruntは、Terraform の薄いラッパーであり、次の追加ツールを提供します。

  • 繰り返しを減らす
  • 複数の Terraform モジュールの操作
  • リモート状態の管理

このガイドでは、Terragrunt を使用して次のことを行います。

  • Terraform 構成を生成する
  • ファイルの作成
  • 複数の環境を管理する

あなたが始める前に

このガイドを使用するには、New Relic と Terraform の両方に関する基本的な知識が必要です。

まだ行っていない場合:

このガイドの例に従うために、 GitHubでサンプル コードを見つけることができます。

コンフィギュレーションの作成

Terragrunt は、Terraform 構成用の追加ツールを提供します。ここでは、Terragrunt、Terraform、および New Relic を使用して構成を作成します。

ワークスペースを初期化します。

bash
$
mkdir terragrunt-config && cd terragrunt-config

新しいフォルダーで、 terragrunt.hclファイルを作成します。

bash
$
touch terragrunt.hcl

次に、 devというサブフォルダーを含む環境フォルダーを作成します。

bash
$
mkdir -p environments/dev

次に、 main.tfファイルとprovider.tfファイルを含むsrcフォルダーを作成します。

bash
$
mkdir src
$
touch src/main.tf
$
touch src/provider.tf

main.tfで Terraform リソースを構成し、 provider.tf でプロバイダーを構成します。

src/provider.tfで、New Relic プロバイダーを構成します。

terraform {
required_version = "~> 0.14.3"
required_providers {
newrelic = {
source = "newrelic/newrelic"
version = "~> 2.14.0"
}
}
}

src/main.tfで、DemoPolicyという名前の New Relic アラート ポリシーを追加します。

resource "newrelic_alert_policy" "DemoPolicy" {
name = "My Demo Policy"
}

environment/devで、 terragrunt.hclという名前のファイルを作成します。

bash
$
touch environments/dev/terragrunt.hcl

その中に、次のincludeステートメントを追加します。

include {
path = find_in_parent_folders()
}

これは、Terragrunt に任意の .hcl を使用するように指示します。親フォルダーで見つかった構成ファイル。

terraformブロックを追加して、Terragrunt にソース参照を提供します。

include {
path = find_in_parent_folders()
}
terraform {
source = "../../src"
}

src/provider.tfで、API キー、アカウント ID、リージョンを使用して New Relic プロバイダーを構成します。

terraform {
required_version = "~> 0.14.3"
required_providers {
newrelic = {
source = "newrelic/newrelic"
version = "~> 2.14.0"
}
}
}
variable "newrelic_personal_apikey" {}
variable "newrelic_account_id" {}
variable "newrelic_region" {}
provider "newrelic" {
account_id = var.newrelic_account_id
api_key = var.newrelic_personal_apikey
region = var.newrelic_region
}

変数を使用して、構成を動的に保ちます。

environment/devで、terragrunt を初期化します。

bash
$
terragrunt init

これにより、環境変数を含む少しのコンテキストが設定され、 terraform initが実行されます。

bash
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
[terragrunt] [/workspace/terragrunt-config/environments/dev] 2021/02/02 13:30:31 Copying lock file [output] from /workspace/terragrunt-config/environments/dev/.terragrunt-cache/e-PoBgWhdv3v8QGOtDQxS_WeYu4/
69zjIFUfApJiUt8gFmi-6-dcPe8/.terraform.lock.hcl to /workspace/terragrunt-config/environments/dev

environment/dev/terragrunt.hclで、 inputsブロックを追加して、New Relic アカウント変数の値を提供します。

inputs = {
newrelic_personal_apikey = "NRAK-***" # Your New Relic account ID
newrelic_account_id = "12345" # Your New Relic account ID
newrelic_region = "US" # US or EU (defaults to US)
}

次に、 terragrunt planを実行します。

bash
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# newrelic_alert_policy.DemoPolicy will be created
+ resource "newrelic_alert_policy" "DemoPolicy" {
+ account_id = (known after apply)
+ id = (known after apply)
+ incident_preference = "PER_POLICY"
+ name = "My Demo Policy"
}
Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Terragrunt はinputsブロックの値を提供します。

terragrunt applyを実行:

bash
$
terragrunt apply

これで、デモ ポリシーが New Relic アカウントに追加されました。

構成に追加

基本的な New Relic 構成を作成したので、 「New Relic と Terraform の開始」およびTerraform モジュールガイドの構成を追加します。

ヒント

これらのガイドをまだ行っていない場合は、 Terragrunt intro Github リポジトリから構成をコピーできます。

src/main.tfで、アラート チャネルの電子メール アドレスを希望の電子メール アドレスに更新します。

resource "newrelic_alert_policy" "DemoPolicy" {
name = "My Demo Policy"
}
resource "newrelic_alert_channel" "DemoChannel" {
name = "My Demo Channel"
type = "email"
config {
recipients = "your@email_address.com"
include_json_attachment = "1"
}
}
resource "newrelic_alert_policy_channel" "ChannelSubs" {
policy_id = newrelic_alert_policy.DemoPolicy.id
channel_ids = [
newrelic_alert_channel.DemoChannel.id
]
}
module "HostConditions" {
source = "git::https://github.com/jsbnr/demo-terraform.git"
policyId = newrelic_alert_policy.DemoPolicy.id
cpu_critical = 88
cpu_warning = 78
diskPercent = 68
}

ここでは、New Relic アラート チャネルを追加し、デモ ポリシーをアラート チャネルにサブスクライブし、Github でホストされているモジュールを追加しました。

terragrunt initを実行してからterragrunt applyを実行します。

bash
$
terragrunt init
$
terragrunt apply

Terraform が処理を終了すると、アラート ポリシーには 2 つの条件と 1 つのアラート チャネルが含まれます。

環境を Terragrunt 変数として使用する

Terragrunt を使用すると、実行中の環境の名前を作成中のデータの名前に追加して、New Relic でリソースをより識別しやすくすることができます。

ルートterragrunt.hclファイルで、 env_nameの入力を作成します。

inputs = {
env_name = "develop"
}

src/main.tfで、ファイルにenv_nameという名前の新しい変数を追加します:

variable "env_name" {}

新しいenv_name変数をアラート ポリシーとアラート チャネル リソース ブロックに追加します。

resource "newrelic_alert_policy" "DemoPolicy" {
name = "${var.env_name}: My Demo Policy"
}
resource "newrelic_alert_channel" "DemoChannel" {
name = "${env_name}: My Demo Channel"
type = "email"
config {
recipients = "your@email_address.com"
include_json_attachment = "1"
}
}

terragrunt planを実行して、ポリシー名に追加された環境変数を確認します。

bash
# newrelic_alert_policy.DemoPolicy will be updated in-place
~ resource "newrelic_alert_policy" "DemoPolicy" {
id = "1216533"
~ name = "My Demo Policy" -> "develop: My Demo Policy"
# (2 unchanged attributes hidden)
}
# newrelic_alert_policy_channel.ChannelSubs must be replaced
-/+ resource "newrelic_alert_policy_channel" "ChannelSubs" {
~ channel_ids = [
- 4737437,
] -> (known after apply) # forces replacement
~ id = "1216533:4737437" -> (known after apply)
# (1 unchanged attribute hidden)
}

ここでは、環境をterragrunt.hclにハードコードしました。これをより動的にするには、terragrunt 組み込み関数を使用して環境を取得します。

ルートterragrunt.hclファイルで、入力を更新してpath_relative_to_include()を使用し、値をenv_name変数として渡します。

inputs = {
env_name = path_relative_to_include()
}

terragrunt planを実行:

bash
# newrelic_alert_policy.DemoPolicy will be updated in-place
~ resource "newrelic_alert_policy" "DemoPolicy" {
id = "1216533"
~ name = "My Demo Policy" -> "environments/dev: My Demo Policy"
# (2 unchanged attributes hidden)
}
# newrelic_alert_policy_channel.ChannelSubs must be replaced
-/+ resource "newrelic_alert_policy_channel" "ChannelSubs" {
~ channel_ids = [
- 4737437,
] -> (known after apply) # forces replacement
~ id = "1216533:4737437" -> (known after apply)
# (1 unchanged attribute hidden)
}
Plan: 2 to add, 1 to change, 2 to destroy.

env_name変数には./environments/dev/パス全体が含まれていることに注意してください。代わりに、「dev」部分のみを含めます。

terragrunt.hclを更新して、 env_nameから「environements/」を削除します。

locals {
env_name = replace(path_relative_to_include(), "environments/", "")
}
inputs = {
env_name = local.env_name
}

ここでは、 localsブロックを追加してローカル変数を作成し、組み込みのreplace関数を使用して相対パスの不要な部分を削除しました。次に、ローカル変数を使用するようにinputsブロックを更新しました。

terragrunt planを実行:

bash
# newrelic_alert_policy.DemoPolicy will be updated in-place
~ resource "newrelic_alert_policy" "DemoPolicy" {
id = "1216533"
~ name = "My Demo Policy" -> "dev: My Demo Policy"
# (2 unchanged attributes hidden)
}
# newrelic_alert_policy_channel.ChannelSubs must be replaced
-/+ resource "newrelic_alert_policy_channel" "ChannelSubs" {
~ channel_ids = [
- 4737437,
] -> (known after apply) # forces replacement
~ id = "1216533:4737437" -> (known after apply)
# (1 unchanged attribute hidden)
}
Plan: 2 to add, 1 to change, 2 to destroy.

新しいポリシー名は「dev: My Demo Policy」です。

terragrunt applyを実行して構成を更新します。

bash
$
terragrunt apply

状態をリモート ストレージに移動する

現時点では、状態ファイルはローカルです。ここで、Terraform 構成を更新して Amazon S3 に保存します。

ヒント

Terragrunt では複数の環境を構成できるため、状態ファイルを独自の S3 バケットに保存して、互いに上書きしないようにする必要があります。

開発状態ファイル用の S3 バケットを作成します。

ルートterragrunt.hclで、ファイルを S3 のどこに配置するかを Terragrunt に指示するremote_stateブロックを追加します。

remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
bucket = "YOUR_S3_BUCKET_NAME" # Amazon S3 bucket required
key = "envs/${local.env_name}/terraform.tfstate"
region = "us-east-1"
encrypt = true
profile = "YOUR_PROFILE_NAME" # Profile name required
}
}

ここでは、バケット名、リージョン、暗号化、およびプロファイルを指定するリモート状態構成を定義しました。プレースホルダーの値を実際の値に置き換えてください。keyでは、前に作成したローカルのenv_nameを使用して、状態ファイルの環境を動的に設定しました。最後に、バケットにbackend.tfという名前の新しいファイルを生成するよう Terragrunt に指示しました。

terragrunt planを実行:

bash
$
terragrunt plan

バケットに、 envs という名前のフォルダーが表示されます。その中には、 terraform.tfstateファイルを含むdevsというフォルダーがあります。

ヒント

envs/dev内には、 terragrunt-cacheという名前の隠しフォルダーがあります。その中には、Terragrunt が生成したbackend.tfファイルがあります。

新しい環境を作成する

開発環境を構成したので、ほとんどの作業を再利用する別の環境を作成します。

環境の下に、 nonprodという名前のフォルダーを作成します。その中に、 terragrunt.hclというファイルを作成します。

bash
$
mkdir nonprod && cd nonprod
$
touch terragrunt.hcl

environment/nonprod/terragrunt.hclで、 environments/dev/terragrunt.hclから構成をコピーします。

include {
path= find_in_parent_folders()
}
terraform {
source = "../../src"
}
inputs = {
newrelic_personal_apikey = "NRAK-***" # Your New Relic account ID
newrelic_account_id = "12345" # Your New Relic account ID
newrelic_region = "US" # US or EU (defaults to US)
}

ヒント

非本番環境用に別のアカウントを使用している場合は、新しいアカウント ID、API キー、リージョンでinputsを更新してください。

nonprod内で、 terragrunt initterragrunt applyを実行します。

bash
$
terragrunt init
$
terragrunt apply

Terraform は、「nonprod:」で始まる新しいリソース セットを作成します。

これで、dev と nonprod という 2 つの環境を作成しましたが、名前以外は同じです。

src/main.tfで、Host Conditions モジュールに新しい変数を追加します。

variable "cpu_critical" {default = 89}
variable "cpu_warningl" {default = 79}
variable "diskPercentage" {default = 69}

このような変数を使用すると、構成がより動的になります。

cpu_criticalcpu_warning 、およびdiskPercentage変数を使用するようにHostConditionsを更新します。

module "HostConditions" {
source = "git::https://github.com/jsbnr/demo-terraform.git"
policyId = newrelic_alert_policy.DemoPolicy.id
cpu_critical = var.cpu_critical
cpu_warning = var.cpu_warninig
diskPercent = var.dskPercentage
}

terragrunt planを実行:

bash
$
terragrunt plan

HostConditions値に変数のデフォルトが含まれるようになりました。

nonprod/terragrunt.hclで、変数に値を追加します。

inputs = {
newrelic_personal_apikey = "NRAK-***" # Your New Relic account ID
newrelic_account_id = "12345" # Your New Relic account ID
newrelic_region = "US" # US or EU (defaults to US)
cpu_critical = 50
cpu_warninig = 40
diskPercentage = 98
}

これにより、値が環境構成に渡されます。

terragrunt applyを実行:

bash
$
terragrunt apply

New Relic アカウントには、製品固有ではない構成を持つ新しいポリシーがあります。

結論

おめでとう! Terragrnt を使用して、New Relic 構成を生成し、複数の環境を管理しました。Terragrunt の紹介例New Relic Terraform プロバイダーのドキュメントTerragrunt のクイック スタートを確認して、構成を次のレベルに引き上げる方法を学習してください。

Copyright © 2024 New Relic株式会社。

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.