要使用Terraform管理现有的Akamai CDN交付设置

使用Terraform来管理Akamai CDN的分发设置。

在中文中,可以这样表达:
Akamai CDN的传递设置是通过Akamai控制中心(ACC)使用属性管理器进行的。
通常,为了管理属性,我们会在ACC中通过图形用户界面进行设置,但为了将现有的属性设置编码化,本次我们将介绍使用Terraform的步骤。

直至完成Terraform的过程

您可以在此处参考Akamai Terraform可管理的资源清单。
https://registry.terraform.io/providers/akamai/akamai/latest/docs

然而,如果要根据以上内容从头开始创建Terraform代码,将需要相当大的工作量。
在本次介绍中,我们将利用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

请使用在~/.edgerc文件中记录的名称来选择特定的section。
这次我们将以[default]的section中的账户为目标。

创建财产的实施

通过使用Akamai CLI Terraform的create-property命令,您可以根据现有的配置创建Terraform代码。
请将所需属性的名称从ACC中记录下来,并输入到命令中,然后执行命令。

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

实施后,将创建以下文件,并包含构建代码。

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

进行进口操作

为了将现有环境转换为Terraform管理,需要执行Import命令。在先前通过Akamai CLI Terraform创建的文件中,已经准备好了import.sh。该文件中已经包含了目标参数,您可以选择将代码复制粘贴或执行.sh文件来进行导入操作。

$ 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

根据不同的环境需对其进行三处修正,请根据环境进行相应修正。

进行修正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

在进行导入时,由于产品ID为空,为了与传递的产品名称相匹配,需要在这个环境中添加prd_Download_Delivery。
确认产品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

再次执行计划

确认差异仅适用于akamai_property_activation。
根据以下命令输出,由于应用到暂存环境的版本不是最新的,因此在应用时将会将最新的版本应用到暂存环境中。

$ 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"

最终

使用Akamai CLI Terraform可以将现有的交付配置导出并进行Terraform的代码化,而不是从头开始创建代码。您还可以参考Akamai CLI Terraform自动输出的代码内容,了解Akamai Terraform代码的编写方式和参数指定方法。

在实际运用中,您还可以通过追加代码来增加功能,或者通过重新利用代码来创建新的交付设置。但是,您可以首先通过像本次这样的简单步骤来建立一个基础。

相关文章 jiā)

 

bannerAds