[英]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 站点部署到cloudfront
和s3 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.tf
和cloudfront.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.tf
、 x-s3.tf
和y-cloudfront.tf
、 y-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.因此,我没有这样做,而是尝试创建一个创建s3
和cloudfront
资源的模块,然后在我的项目中,我可以重新使用该模块来创建 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.tf和https://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.