使用 Terraform 在 Heroku 上构建 Ruboty-Gacha 应用程序

简而言之

总结来说

我們將使用 Terraform 在 Heroku 上構建 Ruboty-Gacha 應用程式。

在中国翻译英文文本属于一种困难的任务,但在与英文对话中将汉语母语翻译成中文应该是相对容易的。

请参考下面的文章了解关于Ruboty-Gacha的相关内容

Sinatra + Heroku で Ruboty の Plugin の中からランダムで1つ選んでツイートする Web アプリケーションを作成 – tbpgr – Qiita

有关Terraform的信息

请参考以下文章了解Terraform

ハシモトさんの頭に広がる宇宙 Terraform とは?

安装

Terraform 是一款由 Golang 开发的 CLI 工具,只需下载二进制文件并设置环境变量,即可使用。本次将使用 Windows(64位)版本。

从下面的页面中获取适合自己环境的二进制压缩文件。

Terraform Download

解压已下载的压缩文件

解压文件后,您可以看到许多exe文件。

% ls
terraform.exe                        terraform-provider-google.exe
terraform_0.5.1_windows_amd64.zip    terraform-provider-heroku.exe
terraform-provider-atlas.exe         terraform-provider-mailgun.exe
terraform-provider-aws.exe           terraform-provider-null.exe
terraform-provider-cloudflare.exe    terraform-provider-openstack.exe
terraform-provider-cloudstack.exe    terraform-provider-template.exe
terraform-provider-consul.exe        terraform-provider-terraform.exe
terraform-provider-digitalocean.exe  terraform-provisioner-chef.exe
terraform-provider-dme.exe           terraform-provisioner-file.exe
terraform-provider-dnsimple.exe      terraform-provisioner-local-exec.exe
terraform-provider-docker.exe        terraform-provisioner-remote-exec.exe

将各个exe文件移动到路径正确的位置。

请根据情况移动。

请确认通畅

% terraform help
usage: terraform [--version] [--help] <command> [<args>]

Available commands are:
    apply      Builds or changes infrastructure
    destroy    Destroy Terraform-managed infrastructure
    get        Download and install modules for the configuration
    graph      Create a visual graph of Terraform resources
    init       Initializes Terraform configuration from a module
    output     Read an output from a state file
    plan       Generate and show an execution plan
    push       Upload this Terraform module to Atlas to run
    refresh    Update local state file against real resources
    remote     Configure remote state storage
    show       Inspect Terraform state or plan
    taint      Manually mark a resource for recreation
    version    Prints the Terraform version

% terraform version
Terraform v0.5.2

样品

创建一个名为 heroku.tf 的文件。

在.tf文件中写入配置。
有关.tf文件的详细配置方法,请参阅Configuration – Docs – Terraform。

variable "heroku_email" {}
variable "heroku_api_key" {}

provider "heroku" {
  email = "${var.heroku_email}"
  api_key = "${var.heroku_api_key}"
}

resource "heroku_app" "web" {
  name = "ruboty-gacha"
  region = "us"
  stack = "cedar-14"
  config_vars {
    BUILDPACK_URL="https://github.com/tbpgr/ruboty-gacha.git"
  }
}

干跑

使用terraform plan进行DRY Run。

% terraform plan \
    -var heroku_email=$HEROKU_EMAIL \
    -var heroku_api_key=$HEROKU_API_KEY
Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ heroku_app.web
    all_config_vars.#:           "" => "<computed>"
    config_vars.#:               "" => "1"
    config_vars.0.#:             "" => "1"
    config_vars.0.BUILDPACK_URL: "" => "https://github.com/tbpgr/ruboty-gacha.git"
    git_url:                     "" => "<computed>"
    heroku_hostname:             "" => "<computed>"
    name:                        "" => "ruboty-gacha"
    region:                      "" => "us"
    stack:                       "" => "cedar-14"
    web_url:                     "" => "<computed>"

安装

使用Terraform apply命令执行设置。

% terraform apply \
    -var heroku_email=$HEROKU_EMAIL \
    -var heroku_api_key=$HEROKU_API_KEY
heroku_app.web
    all_config_vars.#: "" => "<computed>"
    git_url:           "" => "<computed>"
    heroku_hostname:   "" => "<computed>"
    name:              "" => "ruboty-gacha"
    region:            "" => "us"
    stack:             "" => "cedar-14"
    web_url:           "" => "<computed>"

heroku_app.web: Creation complete

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

从Heroku仪表板上确认应用程序已添加。

heroku.png

在 Git 的 remote 中添加 heroku

% git remote -v
origin  https://github.com/tbpgr/ruboty-gacha (fetch)
origin  https://github.com/tbpgr/ruboty-gacha (push)
% git remote add heroku git@heroku.com:ruboty-gacha.git
% git remote -v
heroku  git@heroku.com:ruboty-gacha.git (fetch)
heroku  git@heroku.com:ruboty-gacha.git (push)
origin  https://github.com/tbpgr/ruboty-gacha (fetch)
origin  https://github.com/tbpgr/ruboty-gacha (push)

将代码推送到 Heroku

$ git push heroku master

部署结果

请上网查看

ruboty-gacha.gif

确认动态生成的推文内容

ruboty-gacha.png

图形输出

使用terraform graph命令生成dot文件。

% terraform graph > graph.dot
% cat graph.dot
digraph {
        compound = "true"
        newrank = "true"
        subgraph "root" {
                "[root] heroku_app.web" [label = "heroku_app.web", shape = "box"]
                "[root] provider.heroku" [label = "provider.heroku", shape = "diamond"]
                "[root] heroku_app.web" -> "[root] provider.heroku"
        }
}

使用 dot 命令生成 png 文件。

使用Graphviz的dot命令可以从dot文件生成图像。

% dot -Tpng -o ruboty-gacha_graph.png graph.dot

请确认输出图像。

ruboty-gacha_graph.png

外部数据

Configuration – Docs – Terraform

TerraformでHerokuアプリのセットアップ – SOTA

tbpgr/ruboty-gacha – GitHub

广告
将在 10 秒后关闭
bannerAds