是否有任何脚本可以自动格式化永久性磁盘并将其附加到 Google Cloud VM 实例,而不是执行格式化和安装步骤?
永久性磁盘是使用 Terraform 创建的,它还会创建一个 VM 并使用attached_disk命令将磁盘附加到它。
我希望在 VM 实例启动时运行一个简单的脚本:
google-compute-engine google-cloud-platform terraform terraform-provider-gcp
这是一个新手问题,但我刚刚开始使用 Terraform / Terragrunt 进行 GCP 配置,我发现获取 GCP 凭据的工作流程非常混乱。我来自只使用 AWS,在那里获取凭证并在 AWS CLI 中配置它们非常简单。
基本上,Google Cloud Provider 文档指出您应该provider像这样定义一个块:
provider "google" {
credentials = "${file("account.json")}"
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
}
Run Code Online (Sandbox Code Playgroud)
该credentials字段显示我(显然)必须生成一个服务帐户,并在我的文件系统上的某处保留一个 JSON。
但是,如果我运行该命令gcloud auth application-default login,则会生成一个位于~/.config/gcloud/application_default_credentials.json;的令牌。或者我也可以使用gcloud auth login <my-username>. 从那里我可以使用gcloud命令从命令行访问 Google API(这也是 Terraform 在幕后所做的)。
那么为什么 Terraform 提供程序需要服务帐户的 JSON 文件呢?为什么不能只使用gcloudCLI 工具已经在使用的凭据?
顺便说一句,如果我将 Terraform 配置为指向该application_default_credentials.json文件,则会出现以下错误:
正在初始化模块...
正在初始化后端...
错误:无法获取现有工作区:查询 Cloud Storage 失败:获取 …
google-cloud-platform terraform terragrunt terraform-provider-gcp
我正在尝试使用 Terraform 创建 GKE 节点池
resource "google_container_node_pool" "node_pool" {
provider = google-beta
name = var.node_pool_name
location = var.region
cluster = var.cluster_name
node_count = var.k8s_workers_count
node_config {
machine_type = var.k8s_workers_shape
image_type = "COS"
service_account = google_service_account.sa.email
labels = {
name = var.node_pool_name
}
metadata = {
disable-legacy-endpoints = "true"
}
workload_metadata_config {
node_metadata = "GKE_METADATA_SERVER"
}
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email"
]
}
management {
auto_repair = true
auto_upgrade = true
}
}
Run Code Online (Sandbox Code Playgroud)
3m TF 控制台返回错误信息后
Error: Error reading NodePool "pool1" …Run Code Online (Sandbox Code Playgroud) 我正在尝试让 terraform 将“A”记录添加到 GCP 中的 dns 区域。这样做会导致错误:“未设置更新服务器”。此处描述了类似的错误。所以我从那里发表的评论中收集到,我需要在我的 dns 提供程序中更新项目。我尽职尽责地试图提供。
provider "dns" {
update {
server = "xxx.xxx.x.x"
}
}
Run Code Online (Sandbox Code Playgroud)
除了我不知道那里有什么 IP,而且我的第一次尝试失败了。
我需要其他设置吗?
我在文档中注意到以下格式...
provider "dns" {
update {
server = "192.168.0.1"
key_name = "example.com."
key_algorithm = "hmac-md5"
key_secret = "3VwZXJzZWNyZXQ="
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白这些设置从何而来。
更新:
马丁的建议(下面接受的答案)很有魅力。
在接下来的拥有这个挣扎,诀窍是使用google_dns_record_set代替dns_a_record_set。
我尝试使用 Terraform 在 GCP 上自动创建共享 VPC。我已启用对我的服务帐户的所有访问权限(组织管理员、XpnAdmin、存储管理员、计算管理员、计费管理员)
但是当我执行 terraform apply 时,它抛出以下错误:
缺少“billingAccounts/CXXXXXXXXXXXXXXXXXX”的权限:billing.resourceAssociations.create
我指的是此演示的Google 提供商Github 代码。
当我尝试跑步时
steps:
- id: Plan Terraform
name: hashicorp/terraform:light
args:
- plan
Run Code Online (Sandbox Code Playgroud)
在 Cloud Build 中,我收到错误:
Error: Error reading Project Service foo/cloudbuild.googleapis.com: googleapi: Error 403: Cloud Resource Manager API has not been used in project 123456789 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=123456789 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured
Run Code Online (Sandbox Code Playgroud)
由于相同的 terraform 定义正在我的本地计算机上运行,我认为错误消息有点误导,它实际上是一个凭据问题。
根据 Google Cloud 文档,我应用了以下内容:
resource "google_project_iam_binding" "cloudbuild" …Run Code Online (Sandbox Code Playgroud) google-cloud-platform terraform devops terraform-provider-gcp google-cloud-build
我正在尝试为 terraform 模块添加 kubectl 提供程序,我遵循Terraform kubectl的文档。我运行terraform init并成功安装了提供程序,但是当我尝试添加示例配置时,例如:(或来自此处的其他人)
resource "kubectl_server_version" "current" {}
Run Code Online (Sandbox Code Playgroud)
并运行terraform plan我得到以下消息:
Error: Could not load plugin
Failed to instantiate provider "registry.terraform.io/hashicorp/kubectl" to
obtain schema: unknown provider "registry.terraform.io/hashicorp/kubectl"
Run Code Online (Sandbox Code Playgroud)
当我调整时terraform init(资源在模块 k8s 中就位)
Error: Failed to install provider
Error while installing hashicorp/kubectl: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/kubectl
Run Code Online (Sandbox Code Playgroud)
一些输出:
$terraform plugins
??? provider[registry.terraform.io/hashicorp/kubernetes] 1.13.2
??? provider[registry.terraform.io/gavinbunney/kubectl] 1.9.1
??? module.k8s
? ??? provider[registry.terraform.io/hashicorp/kubectl]
? ??? provider[registry.terraform.io/hashicorp/kubernetes]
$terraform …Run Code Online (Sandbox Code Playgroud) 我想使用 Terraform 在 GCP 中使用 slack 设置警报。
我的代码是:
resource "google_monitoring_notification_channel" "slack" {
display_name = "Prod Slack Alerts"
type = "slack"
labels = {
"channel_name" = "#alert-channel"
}
sensitive_labels {
auth_token = "one"
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是我如何获得auth_token?我看了这篇文章,但这个解决方案已经过时并且不起作用。另外,没有这方面的文档。我已经将 Google Cloud Monitoring 应用集成到我组织的 Slack 中。
如何配置 Terraform Cloud 以通过 Google Cloud Platform (GCP) 进行身份验证?
现在,我的配置如下所示:
provider "google" {
project = "my-project-id"
region = "europe-west3"
zone = "europe-west3-a"
}
resource "google_storage_bucket" "my-bucket" {
name = "my-bucket"
location = "EUROPE-WEST3"
force_destroy = true
uniform_bucket_level_access = true
}
Run Code Online (Sandbox Code Playgroud)
这给我带来了一个错误:
错误:尝试加载应用程序默认凭据,因为提供程序块中既没有设置
credentials也没有设置。access_token未加载凭据。要使用您的 gcloud 凭据,请运行“gcloud auth application-default login”。原始错误:谷歌:找不到默认凭据。有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials 。
google-cloud-storage google-cloud-platform terraform devops terraform-provider-gcp
我使用 Terraform 来管理 Google Cloud Functions 的资源。但是,虽然云函数的初始部署有效,但当sourcecode.zip我在更新源存档后使用时,未重新部署更改后的云函数源代码(源存档)的进一步部署terraform apply。
存储桶对象被更新,但这不会触发云函数资源的更新/重新部署。
这是提供商的错误吗?当代码更改时,有没有办法在 terraform 中重新部署函数?
我正在使用的简化源代码:
resource "google_storage_bucket" "cloud_function_source_bucket" {
name = "${local.project}-function-bucket"
location = local.region
uniform_bucket_level_access = true
}
resource "google_storage_bucket_object" "function_source_archive" {
name = "sourcecode.zip"
bucket = google_storage_bucket.cloud_function_source_bucket.name
source = "./../../../sourcecode.zip"
}
resource "google_cloudfunctions_function" "test_function" {
name = "test_func"
runtime = "python39"
region = local.region
project = local.project
available_memory_mb = 256
source_archive_bucket = google_storage_bucket.cloud_function_source_bucket.name
source_archive_object = google_storage_bucket_object.function_source_archive.name
trigger_http = true
entry_point = "trigger_endpoint"
service_account_email = google_service_account.function_service_account.email
vpc_connector …Run Code Online (Sandbox Code Playgroud) google-cloud-platform terraform google-cloud-functions terraform-provider-gcp