我已经编写了一个 terraform 模块来创建一个 Lambda,但我无法弄清楚如何在预构建的 ZIP 文件上计算 source_code_hash。这将在管道中,因此每次都会构建 ZIP 文件,并且在我到达 terraform 步骤之前可能会有所不同。
我正在使用 gulp 构建 ZIP 文件(这是一个 NodeJS 应用程序)并假设它是在目录 build/myLambda.zip 中预先构建的
基本上我想这样做。文件名是一个 terraform 变量,我希望 source_code_hash 计算必须引用该文件。
module my_lambda {
filename = "${var.my_zip_file}"
}
Run Code Online (Sandbox Code Playgroud)
该模块的相关部分是:
resource "aws_lambda_function" "lambda" {
filename = "${var.filename}"
source_code_hash = "${filebase64sha256(file("${var.filename}"))}"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行 terraform plan 时,出现此错误:
Error: Error in function call
on modules\lambda\main.tf line 16, in resource "aws_lambda_function" "lambda":
16: source_code_hash = "${filebase64sha256(file("${var.filename}"))}"
|----------------
| var.filename is "build/myLambda.zip"
Call to function "file" failed: contents of
build/myLambda.zip are not …Run Code Online (Sandbox Code Playgroud) 我希望你能帮助我解决我的问题。我正在尝试自动让我的 ec2 实例使用我的 terraform 脚本加入一个广告域。由于 Terraform 不支持任何“域加入目录”选项,我想尝试创建一个 SSM 文档,让 Systems Manager 为我制作。实际上我得到了以下代码:
resource "aws_directory_service_directory" "ad" {
name = "active-directory-service.com"
password = "${var.ad-password}"
edition = "Standard"
size = "Small"
type = "MicrosoftAD"
vpc_settings {
vpc_id = "${aws_vpc.vpc.id}"
subnet_ids = ["${aws_subnet.ds-subnet.0.id}",
"${aws_subnet.ds-subnet.1.id}"
]
}
}
resource "aws_vpc_dhcp_options" "vpc-dhcp-options" {
domain_name = "${var.dir_domain_name}"
domain_name_servers = aws_directory_service_directory.ad.dns_ip_addresses
}
resource "aws_vpc_dhcp_options_association" "dns_resolver" {
vpc_id = aws_vpc.vpc.id
dhcp_options_id = aws_vpc_dhcp_options.vpc-dhcp-options.id
}
resource "aws_ssm_document" "ad-server-domain-join-document" {
name = "myapp_dir_default_doc"
document_type = "Command"
content = <<DOC
{
"schemaVersion": …Run Code Online (Sandbox Code Playgroud) 我正在使用 terraform 进行 AWS 资源配置。我需要自我参考“mySG”。从 Terraform 文档我可以使用
ingress {
from_port = 0
to_port = 0
protocol = -1
self = true
}
Run Code Online (Sandbox Code Playgroud)
但是不同的协议呢?使用控制台 有以下历史入站规则可用:
Type Protocol PortRange Source
1. All TCP TCP 0-65535 mySG
2. All UDP UDP 0-65535 mySG
3. Custom TCP TCP 1856 mySG
Run Code Online (Sandbox Code Playgroud)
(是否需要第三个条目?考虑所有端口的第一个条目)上述入口规则是否处理所有 3 个条目?如果不是什么应该是 terraform 语法。
amazon-web-services aws-cli aws-security-group terraform-provider-aws aws-cloudformation-custom-resource
我对 terraform 有一个不寻常的问题。
我创建了两个 VPC,并在同一个 terraform 脚本中添加了一个私有托管区域。
我执行以下操作:
data "aws_vpcs" "foo" {}
这让我获得了在该地区创建的 VPC。
通常我可以输出 VPC 的 ID,例如:
output "test" {
value = data.aws_vpcs.foo.ids
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个列表,如:
[ "vpc-0c8446a2164b7d0af", "vpc-0e7c63c3f383d115d", ]
现在,从这个列表中,我想获得第一个 VPC id:“vpc-0c8446a2164b7d0af”
问题是它不起作用。我尝试使用元素函数,如:
element(data.aws_vpcs.foo.ids, 0)
element([data.aws_vpcs.foo.ids], 0)
我也尝试将它分配给一个值,如:data.aws_vpcs.foo.ids[0]
它不起作用,我在 terraform 上找不到任何其他选项来帮助我解决这个问题。
我想使用第一个 VPC id 创建资源:
resource "aws_route53_zone" "private" {
name = "example.com"
vpc {
vpc_id = data.aws_vpcs.foo.ids[0]
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以从我在该地区获得的 VPC 列表中获得第一个 VPC(顺序无关紧要)。
当我运行 terraform plan 时出现错误:
Error: Invalid index
on main.tf line 25, in resource "aws_route53_zone" "private":
25: …Run Code Online (Sandbox Code Playgroud) 我在 terraform apply 期间遇到以下错误。
Error: timeout while waiting for plugin to start
Debug LOG:
2020-07-09T13:15:46.551-0400 [DEBUG] plugin: plugin process exited: path=/.terraform/plugins/darwin_amd64/terraform-provider-kubernetes_v1.11.3_x4 pid=48631
2020-07-09T13:15:46.551-0400 [DEBUG] plugin: plugin exited
2020-07-09T13:15:46.555-0400 [WARN] plugin: plugin failed to exit gracefully
2020-07-09T13:15:46.558-0400 [DEBUG] plugin: plugin process exited: path=/.terraform/plugins/darwin_amd64/terraform-provider-null_v2.1.2_x4 pid=48389
2020-07-09T13:15:46.558-0400 [DEBUG] plugin: plugin exited
Run Code Online (Sandbox Code Playgroud) iaas kubernetes terraform terraform-provider-aws terraform0.12+
terraform plan被中止,现在无法获取状态锁。我正在尝试手动释放它,但出现错误:
terraform force-unlock -force xxx-xxx-xx-dddd
Failed to unlock state: failed to retrieve lock info:
unexpected end of JSON input
Run Code Online (Sandbox Code Playgroud)
状态文件看起来完整并成功通过了 json 语法验证。
如何解决?
Terraform v0.12.x
这是我另一篇文章如何使用 Terraform 模块进行代码重用的后续问题?.
我有 2 个模块,旨在重用其他模块。我的目录结构是...
/terraform/
/terraform/blue/main.tf
/terraform/green/main.tf
/terraform/module_snapshot/main.tf
/terraform/module_ebs/main.tf
Run Code Online (Sandbox Code Playgroud)
我想module_ebs/main.tf在两次部署之间重复使用,blue/main.tf并且green/main.tf. 它只是
resource "aws_ebs_volume" "ebs" {
availability_zone = "us-east-1a"
snapshot_id = "sn-123456abcded"
size = 500
type = "gp2"
tags = {
Name = "test-ebs"
}
}
output "ebs_id" {
value = aws_ebs_volume.ebs.id
description = "Volume id of the EBS volume"
}
Run Code Online (Sandbox Code Playgroud)
这个想法是使用green/main.tf创建一个 EBS 卷module_ebs/main.tf(它有一个名为 的输出ebs_id)。
provider "aws" {
region = "us-east-1" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Terraform 创建一个 PostgreSQL RDS 实例。这是我的配置的外观:
resource "aws_db_subnet_group" "postgres" {
name = "postgres-subnets"
subnet_ids = ["mysub1","mysub2"]
}
resource "aws_db_instance" "myrds" {
engine = "postgresql"
engine_version = "12.4"
instance_class = "db.t2.micro"
identifier = "myrds"
username = "myuser"
password = "*******"
allocated_storage = 10
storage_type = "gp2"
db_subnet_group_name = "${aws_db_subnet_group.postgres.id}"
}
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下错误:
Error: Error creating DB Instance: InvalidParameterValue: Invalid DB engine
Run Code Online (Sandbox Code Playgroud) postgresql amazon-web-services amazon-rds terraform terraform-provider-aws
我正在编写一个 tf 脚本来创建一个角色并将 AWS 托管策略附加到该角色。如果我的策略是自定义策略,这有效,如果我需要使用 AWS 托管策略,我如何将我的策略附加到角色。有没有办法使用“数据”从 AWS 推断角色 ARN
resource "aws_iam_role" "CloudWatchAgentServerRole" {
name = "CloudWatchAgentServerRole"
description = "Role created to allow Memory metrics to CloudWatch "
assume_role_policy = file("role.json")
}
#Create Policy - Not Required as its an AWS Managed Policy
/*resource "aws_iam_policy" "CloudWatchAgentServerPolicy" {
name = "CloudWatchAgentServerPolicy"
description = "Permissions required to use AmazonCloudWatchAgent on servers"
policy = file("policy.json")
}*/
# Attach Policy
resource "aws_iam_role_policy_attachment" "CloudWatchAgentServer" {
role = aws_iam_role.CloudWatchAgentServerRole.name
policy_arn = aws_iam_policy.CloudWatchAgentServerPolicy.arn
}
Run Code Online (Sandbox Code Playgroud)
谢谢
所以下面是我的项目文件结构:
??? main.tf
??? tunnel
? ??? main.tf
? ??? variables.tf
??? variables.tf
Run Code Online (Sandbox Code Playgroud)
我正在尝试按照此处所述在 Terraform 0.15.1 中使用多个提供程序 -> https://www.terraform.io/docs/language/modules/develop/providers.html
按照示例操作后,我无法使其正常工作。我现在已经简化了我的代码,只使用一个提供程序别名(尽可能简单)。我得到的错误是:
?
? Error: Missing required argument
?
? The argument "region" is required, but was not set.
?
Run Code Online (Sandbox Code Playgroud)
我在根目录中的 main.tf 文件:
module "tunnel" {
source = "./tunnel"
providers = {
aws.r = aws.requester
}
}
Run Code Online (Sandbox Code Playgroud)
我在根目录中的 variables.tf:
provider "aws" {
alias = "requester"
region = "ap-southeast-2"
profile = "benchmark"
}
Run Code Online (Sandbox Code Playgroud)
我的隧道/variables.tf 文件:
terraform {
required_providers {
aws = {
source …Run Code Online (Sandbox Code Playgroud) terraform ×9
amazon-iam ×1
amazon-rds ×1
aws-cli ×1
aws-cloudformation-custom-resource ×1
iaas ×1
kubernetes ×1
postgresql ×1
ssm ×1