[英]Re-use "common variables" in terraform for Lambda environment variables
[英]How can I re-use a configuration (local module) with my Terraform project?
我對 Terraform 很陌生,所以我想我認為 Terraform模塊是我可以重復使用的“功能”,但這是錯誤的。 我有一個場景,我必須將 static web 站點部署到cloudfront
和s3 bucket
。 起初,我在我的項目中將其配置為原始文件: https://github.com/tal-rofe/tf-old/tree/main/terraform/core - 你可以看到我有s3.tf
和cloudfront.tf
文件,在我的項目中是原始的。
但后來我不得不部署另一個 static web 應用程序。 所以,因為現在我需要部署 2 個應用程序,所以我可以復制我的代碼並創建x-cloudfront.tf
、 x-s3.tf
和y-cloudfront.tf
、 y-s3.tf
具有完全相同配置的文件,只是不同我猜是域名。 因此,我沒有這樣做,而是嘗試創建一個創建s3
和cloudfront
資源的模塊,然后在我的項目中,我可以重新使用該模塊來創建 2 web 應用程序。
所以我有這個項目: https://github.com/tal-rofe/tf-new
在這里我創建了一個模塊: https://github.com/tal-rofe/tf-new/tree/main/terraform/modules/static-app
如您所見,我有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)
}
和output.tf
文件:
output "cloudfront_distribution_id" {
description = "The distribution ID of deployed Cloudfront"
value = module.cdn.cloudfront_distribution_id
}
此模塊中的所有其他文件專用於設置相關資源,使用另一個已發布的公共模塊。 因此,如果這樣做,我可以在我的項目中兩次使用此模塊,在每個聲明中提供不同的輸入,並從每個聲明中獲取cloudfront_distribution_id
output。
這就是為什么我有這兩個文件,使用這個模塊: 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
然后,我想從我的項目 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
}
因此,當我開始使用 terraform 應用整個項目時,我沒有得到這兩個輸出,但我只得到一個 output,稱為cloudfront_distribution_id
。 所以我好像得到了我創建的自定義模塊的 output。 但我想獲得我的主要項目的輸出。
所以我不明白我在提供這個自定義模塊時做錯了什么?
我通過以下步驟使用 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
您正在從項目的terraform/modules/static-app
文件夾運行terraform apply
。 您需要從terraform/core
文件夾運行它。
您應該始終從 Terraform 代碼的 core/root/base 文件夾中運行terraform apply
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.