简体   繁体   English

如何在我的 Terraform 项目中重新使用配置(本地模块)?

[英]How can I re-use a configuration (local module) with my Terraform project?

I'm quite new to Terraform, so I guess I consider Terraform modules as "functions" that I can re-use but that's wrong.我对 Terraform 很陌生,所以我想我认为 Terraform模块是我可以重复使用的“功能”,但这是错误的。 I had a scenario where I had to deploy a static web site to cloudfront and s3 bucket .我有一个场景,我必须将 static web 站点部署到cloudfronts3 bucket At first, I configured this as raw files in my project: https://github.com/tal-rofe/tf-old/tree/main/terraform/core - you can see I have s3.tf and cloudfront.tf files, raw in my project.起初,我在我的项目中将其配置为原始文件: https://github.com/tal-rofe/tf-old/tree/main/terraform/core - 你可以看到我有s3.tfcloudfront.tf文件,在我的项目中是原始的。

But then I had to deploy an another static web application.但后来我不得不部署另一个 static web 应用程序。 So, because now I need 2 applications to be deployed, I could duplicate my code and create x-cloudfront.tf , x-s3.tf and y-cloudfront.tf , y-s3.tf files with same exact configuration, just differ with the domains I guess.所以,因为现在我需要部署 2 个应用程序,所以我可以复制我的代码并创建x-cloudfront.tfx-s3.tfy-cloudfront.tfy-s3.tf具有完全相同配置的文件,只是不同我猜是域名。 So instead of doing so, I tried to create a module which creates s3 and cloudfront resources, and then in my project, I could re-use this module to create 2 web applications.因此,我没有这样做,而是尝试创建一个创建s3cloudfront资源的模块,然后在我的项目中,我可以重新使用该模块来创建 2 web 应用程序。

So I have this project: https://github.com/tal-rofe/tf-new所以我有这个项目: https://github.com/tal-rofe/tf-new

And here I created a module: https://github.com/tal-rofe/tf-new/tree/main/terraform/modules/static-app在这里我创建了一个模块: https://github.com/tal-rofe/tf-new/tree/main/terraform/modules/static-app

As you can see I have variables.tf file:如您所见,我有variables.tf文件:

variable "domain_name" {
  description = "The domain name of the application"
  type        = string
}

variable "zone_id" {
  description = "The zone identifier to set domain of the application in"
  type        = string
}

variable "acm_certificate_arn" {
  description = "The certificate ARN"
  type        = string
}

variable "s3_bucket_name" {
  description = "The bucket name of the S3 bucket for the application"
  type        = string
}

variable "common_tags" {
  description = "The tags for all created resources"
  type        = map(string)
  default     = {}
}

variable "cloudfront_tags" {
  description = "The tags for Cloudfront resource"
  type        = map(string)
}

variable "www_redirect_bucket_tags" {
  description = "The tags for a bucket to redirect www to non-www"
  type        = map(string)
}

variable "s3_bucket_tags" {
  description = "The tags for a bucket to redirect www to non-www"
  type        = map(string)
}

and output.tf file:output.tf文件:

output "cloudfront_distribution_id" {
  description = "The distribution ID of deployed Cloudfront"
  value       = module.cdn.cloudfront_distribution_id
}

And all other files within this module are dedicated for setting up the relevant resources, using another public modules released .此模块中的所有其他文件专用于设置相关资源,使用另一个已发布的公共模块 So If I do so, I could, in my project, to use this module twice , provide different inputs in each declaration, and get the cloudfront_distribution_id output from each.因此,如果这样做,我可以在我的项目中两次使用此模块,在每个声明中提供不同的输入,并从每个声明中获取cloudfront_distribution_id output。

This is why I have these 2 files, using this module: https://github.com/tal-rofe/tf-new/blob/main/terraform/core/docs-static.tf and https://github.com/tal-rofe/tf-new/blob/main/terraform/core/frontend-static.tf这就是为什么我有这两个文件,使用这个模块: https://github.com/tal-rofe/tf-new/blob/main/terraform/core/docs-static.tfhttps://github.com /tal-rofe/tf-new/blob/main/terraform/core/frontend-static.tf

And then, I want to output from my project the 2 created cloudfront distributions IDs:然后,我想从我的项目 output 中获取 2 个创建的云端分发 ID:

output "frontend_cloudfront_distribution_id" {
  description = "The distribution ID of deployed Cloudfront frontend"
  value       = module.frontend-static.cloudfront_distribution_id
}

output "docs_cloudfront_distribution_id" {
  description = "The distribution ID of deployed Cloudfront docs"
  value       = module.docs-static.cloudfront_distribution_id
}

So when I start applying this whole project with terraform, I don't get these 2 outputs, but I only get one output, called cloudfront_distribution_id .因此,当我开始使用 terraform 应用整个项目时,我没有得到这两个输出,但我只得到一个 output,称为cloudfront_distribution_id So it seems like I get the output of the custom module I created.所以我好像得到了我创建的自定义模块的 output。 But I want to get the outputs of my main project.但我想获得我的主要项目的输出。

So I don't understand what did I do wrong in providing this custom module?所以我不明白我在提供这个自定义模块时做错了什么?

I apply my configuration using GitHub action with these steps:我通过以下步骤使用 GitHub 操作应用我的配置:

            - name: Terraform setup
              uses: hashicorp/setup-terraform@v2
              with:
                terraform_wrapper: false

            - name: Terraform core init
              env:
                  TERRAFORM_BACKEND_S3_BUCKET: ${{ secrets.TERRAFORM_BACKEND_S3_BUCKET }}
                  TERRAFORM_BACKEND_DYNAMODB_TABLE: ${{ secrets.TERRAFORM_BACKEND_DYNAMODB_TABLE }}
              run: |
                terraform -chdir="./terraform/core" init \
                -backend-config="bucket=$TERRAFORM_BACKEND_S3_BUCKET" \
                -backend-config="dynamodb_table=$TERRAFORM_BACKEND_DYNAMODB_TABLE" \
                -backend-config="region=$AWS_REGION"
                
            - name: Terraform core plan
              run: terraform -chdir="./terraform/core" plan -no-color -out state.tfplan

            - name: Terraform core apply
              run: terraform -chdir="./terraform/core" apply state.tfplan

You are running terraform apply from the terraform/modules/static-app folder of your project.您正在从项目的terraform/modules/static-app文件夹运行terraform apply You need to be running it from the terraform/core folder.您需要从terraform/core文件夹运行它。

You should always run terraform apply from within the core/root/base folder of your Terraform code.您应该始终从 Terraform 代码的 core/root/base 文件夹中运行terraform apply

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 terraform 中的“公共变量”重新用于 Lambda 环境变量 - Re-use "common variables" in terraform for Lambda environment variables GCP Terraform cloud_router 模块——如何在我的 nat 配置中引用单个 su.network 的 self_link? - GCP Terraform cloud_router module --how can I reference the self_link of a single subnetwork in my nat configuration? 如何使用 Terraform 的模块 output 作为另一个 Terraform 模块的输入? - How can I use a Terraform's modules output as an input to another Terraform module? 调用数据/资源时如何使用terraform模块中的变量? - How can I use variables in terraform module when call data/resources? 如何将 Terraform 中的逻辑用于 Azure? - How can i use logic in Terraform for Azure? 我怎样才能获得后续的 terraform 部署以使用在兄弟模块中创建的 vpc - how can i get a subsequent terraform deployment to use a vpc created in sibling module 使用 Terraform,如何将用户列表从一个模块传递到另一个模块 - Using Terraform, how can I pass in a list of users from module to module 如何配置 Terraform 以在不销毁和重新创建的情况下更新 GCP 计算引擎实例模板? - How can I configure Terraform to update a GCP compute engine instance template without destroying and re-creating? Terraform CDKTF:如何在现有的 terraform 项目中使用/导入 CDKTF 代码? - Terraform CDKTF: How to use/import CDKTF code in existing terraform project? 如何在 View 的不同页面上重复使用 firestore 的数据库? - How to re-use the firestore's db accross the different page in View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM