LoginSignup
0
0

既存のAkamai CDNの配信設定をTerraformで管理するには

Last updated at Posted at 2022-06-23

Akamai CDNの配信設定をTerraformで管理する

Akamai CDNの配信設定はAkamai Control Center (ACC) からProperty Managerを利用して行います。
通常、ACCからPropertyを管理するためにはGUIで設定を行いますが、既存のProperty設定をコード化していくために、今回はTerraformを利用した手順をご紹介します。

Terraform化するまでの流れ

AkamaiのTerraformで管理できるリソースの一覧はこちらで参照できます。
https://registry.terraform.io/providers/akamai/akamai/latest/docs

しかしながら、上記を参考にTerraformのコードを1から作成する場合にはかなりの労力が必要となります。
今回はAkamai CLI Terraformを活用して設定をエクスポートし、そこからTerraform管理化する手順について解説します。

なお、API Keyを発行し、~/.edgercに記載が終わっている状態を前提としています。

image.png

ACCから配信設定を作成

ACCよりCDNの配信設定を作成もしくは既存の設定を利用します。
今回はAkamaiの配信製品であるDownload Deliveryを利用しています。

image.png

Akamai CLI Terraformインストール

まずはAkamai CLIのインストールを行います。

Macの場合

$ brew install akamai
$ akamai --version
akamai version 1.4.2

次にAkamai CLI Terraformをインストールします

$ akamai install terraform
$ akamai terraform --section default --version
akamai terraform version 0.7.1

sectionには、~/.edgercに記載している名前を利用してください。
今回は[default]のsectionに記載してあるアカウントを対象としています。

Export Propertyの実施

Akamai CLI Terraformのcreate-propertyを利用することで既存の設定からTerraformのコードを作成することができます。
ACCより対象のProperty名を控えて、値に入力し、コマンドを実行します。

$ akamai terraform --section default export-property [property_name]

実施後に以下のファイルが作成され、構築コードが記載されます。

$ tree ./
./
├── import.sh
├── property-snippets
│   └── main.json
├── property.tf
└── variables.tf

Importを実施する

既存環境をTerraform管理化にするにはImportコマンドを実行する必要がありますが、先程Akamai CLI Terraformにて作成した中にimport.shが用意されています。
こちらのファイルにはすでに対象の引数が記載されていますので、コードをコピペするか.shを実行してImportを行います。

$ vim import.sh
terraform init
terraform import akamai_edge_hostname.xxxxxxxxxxxxxxx ehn_xxxxx,ctr_xxxxxx,grp_xxxxx
terraform import akamai_property.xxxxxxxxxxxxxxxx prp_xxxxx,ctr_xxxxx,grp_xxxxx

差分を修正する

terraform planを実行して差分があるかどうか確認をします。

$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create
  ~ update in-place
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # akamai_edge_hostname.xxxxxxxxxxxxxx must be replaced
-/+ resource "akamai_edge_hostname" "xxxxxxxxxxxxxxxxx" {
      ~ contract      = "ctr_xxxxxxx" -> (known after apply)
      ~ group         = "grp_xxxxxxxx" -> (known after apply)
      ~ id            = "xxxxxxxxxx" -> (known after apply)
      + ip_behavior   = "IPV6_COMPLIANCE" # forces replacement
      + product       = (known after apply)
      + product_id    = "prd_Download_Delivery"
      ~ use_cases     = jsonencode( # whitespace changes

本環境の場合には3箇所修正する必要がありました。環境に応じて修正をしてください。

修正1

property.tfの記載とtfstateに差分があるため、cert_provisioning_typeのCPS_MANAGEDの記載をDEFAULTに合わせる。
cert_provisioning_typeの確認
https://registry.terraform.io/providers/akamai/akamai/latest/docs/resources/property

$ sed -i -e "s/CPS_MANAGED/DEFAULT/g" property.tf

修正2

Importされた際にip_behaviorの項目がnullになっているが、null指定はできないためIPV6_COMPLIANCE(IPv4/IPv6を受け付ける)を指定する。
ip_behaviorの項目確認
https://registry.terraform.io/providers/akamai/akamai/latest/docs/resources/edge_hostname

$ sed -i -e "s/\"ip_behavior\": null/\"ip_behavior\": \"IPV6_COMPLIANCE\"/g" terraform.tfstate

修正3

Importされた際にproduct_idが空となっているが配信プロダクト名に合わせるため、この環境ではprd_Download_Deliveryを追加する。
Product IDの確認
https://registry.terraform.io/providers/akamai/akamai/latest/docs/guides/appendix#common-product-ids

$ sed -i -e "s/\"product_id\": \"\"/\"product_id\": \"prd_Download_Delivery\"/g" terraform.tfstate

再度Planの実施

差分がakamai_property_activationのみになったことを確認します。
以下のコマンド出力の場合、ステージングの環境に適用されているVersionが最新ではないため、apply時に最新のVersionがステージングに適用されます。

$ terraform plan
 
 # akamai_property_activation.xxxxxxxxxxxxxxxxx will be created
  + resource "akamai_property_activation" "xxxxxxxxxxxxxxxxx" {
      + activation_id                  = (known after apply)
      + auto_acknowledge_rule_warnings = true
      + 
      + contact                        = [
      +       + contact                        = [
          + "xxxxxxxx@xxxxx.com",
        ]
      + errors                         = (known after apply)
      + id                             = (known after apply)
      + network                        = "STAGING"

おわりに

既存の配信設定をTerraformでコード化する際に1からコードを作成するのではなく、Akamai CLI Terraformを利用することで、配信設定をエクスポートしてTerraformのコード化を行うことができます。
AkamaiのTerraformのコードの記載方法やパラメーターの指定の仕方などをAkamai CLI Terraformが自動で出力したコードの内容を参考にすることもできます。

実際の運用では、さらにコードを追記して機能を増やしたり、コードを再利用して新規の配信設定を作成することこともできますが、そのたたき台づくりを、まずは今回のような手順で簡単に始めることができます。

関連記事

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