【发布时间】:2023-04-02 03:45:02
【问题描述】:
我正在 coursera 上学习 Python 和 AWS 课程。然而,我使用的是 terraform,而不是使用 boto3 创建 s3 、 api gateway 和其他人。到目前为止一切都很好但是我遇到了一个问题。下面是我的 lambda 目录结构
每个 lambda 都有不同的目录结构,我必须在每个目录中 cd 才能使用 terraform apply 应用更改。
例子
下面是我在 terraform 中用于 lambda 函数之一的 lambda 代码。>
provider "aws" {
region = "us-east-2"
}
terraform {
required_version = "> 0.14"
required_providers {
aws = "~> 3.0"
}
backend "s3" {
bucket = "nyeisterraformstatedata2"
key = "api_gateway/lambda_function/terraform_api_gateway_lambda_validate.tfstate"
region = "us-east-2"
dynamodb_table = "terraform-up-and-running-locks-2"
encrypt = true
}
}
data "archive_file" "zip_file" {
type = "zip"
source_dir = "${path.module}/lambda_dependency_and_function"
output_path = "${path.module}/lambda_dependency_and_function.zip"
}
resource "aws_lambda_function" "get_average_rating_lambda" {
filename = "lambda_dependency_and_function.zip"
function_name = "validate"
role = data.aws_iam_role.lambda_role_name.arn
handler = "validate.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash = filebase64sha256(data.archive_file.zip_file.output_path)
runtime = "python3.8"
depends_on = [data.archive_file.zip_file]
}
>
data "aws_iam_role" "lambda_role_name" {
name = "common_lambda_role_s3_api_gateway_2"
}
根据下面的评论,我使用以下代码创建了一个 main.tf
provider "aws" {
region = "us-east-2"
}
module "test" {
source = "../validate"
}
但我正在尝试使用 import 语句导入它给我一个错误,我无法弄清楚如何解决它
terraform import module.test.aws_lambda_function.test1 get_average_rating_lambda
Warning: Backend configuration ignored
│
│ on ../validate/validate.tf line 10, in terraform:
│ 10: backend "s3" {
│
│ Any selected backend applies to the entire configuration, so Terraform expects provider configurations only in the root module.
│
│ This is a warning rather than an error because it's sometimes convenient to temporarily call a root module as a child module for testing purposes, but this backend configuration block will have no
│ effect.
╵
Error: resource address "module.test.aws_lambda_function.test1" does not exist in the configuration.
Before importing this resource, please create its configuration in module.test. For example:
resource "aws_lambda_function" "test1" {
# (resource arguments)
}
所以我的问题是 terraform 有一种方法可以告诉哪些所有文件已更改并一次性应用它们而不是一个一个地应用它们。因为我也是 terraform 的新手,所以如果有人认为这是错误的构建方式该项目请告诉我。谢谢
【问题讨论】:
标签: amazon-web-services terraform terraform-provider-aws