我正在使用 AWS 模块在 terraform 中构建 lambda,我的代码如下:
\nmodule "lambda_function" {\n\n# * Lambda module configs\n source = "terraform-aws-modules/lambda/aws"\n version = "3.0.0"\n\n # * Lambda Configs\n function_name = "${var.function_name}-${var.env}"\n description = "My Project"\n handler = local.constants.lambda.HANDLER\n runtime = local.constants.lambda.VERSION\n memory_size = 128\n cloudwatch_logs_retention_in_days = 14\n source_path = "./function/"\n timeout = local.constants.lambda.TIMEOUT\n create_async_event_config = true\n maximum_retry_attempts = local.constants.lambda.RETRIES_ATTEMPT\n \n layers = [\n data.aws_lambda_layer_version.layer_requests.arn\n ]\n\n environment_variables = {\n AWS_ACCOUNT = var.env\n SLACK_HOOK_CHANNEL = var.SLACK_HOOK_CHANNEL\n }\n\n tags = {\n Name = "${var.function_name}-${var.env}"\n }\n\n trusted_entities = …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-vpc aws-lambda terraform terraform-provider-aws
我还是个菜鸟,请对我温柔一点!
我有一个使用以下节点组配置运行的 eks 集群:
resource "aws_eks_node_group" "this" {
cluster_name = aws_eks_cluster.this.name
node_group_name = local.cluster_name
node_role_arn = aws_iam_role.eks_node.arn
subnet_ids = aws_subnet.this.*.id
instance_types = ["t2.micro"]
scaling_config {
desired_size = 2
max_size = 4
min_size = 2
}
# Optional: Allow external changes without Terraform plan difference
lifecycle {
ignore_changes = [scaling_config[0].desired_size]
}
depends_on = [
aws_iam_role_policy_attachment.eks_AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.eks_AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.eks_AmazonEC2ContainerRegistryReadOnly,
]
}
Run Code Online (Sandbox Code Playgroud)
我的缩放配置是:
scaling_config {
desired_size = 2
max_size = 4
min_size = 2
}
Run Code Online (Sandbox Code Playgroud)
我可以2使用以下配置成功部署 nginx 副本:
resource "kubernetes_deployment" "nginx" { …Run Code Online (Sandbox Code Playgroud) 我正在使用 terraform 并通过模块配置资源。在某些环境中我想创建一个模块,在其他环境中则不想,最好的方法是什么?尝试使用计数方法:
module "module_name" {
create = var.create_module
source = "../../modules/module"
}
Run Code Online (Sandbox Code Playgroud)
模块内部当然包含检查是否创建资源的资源:
resource "some resource" "main" {
count = var.create == true ? 1 : 0
Run Code Online (Sandbox Code Playgroud)
由于我在模块内使用 count,我在模型中的所有资源(数十个)上收到以下错误:
Because some_resource.main has "count" set, its attributes must
be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
some_resource.main[count.index]
Run Code Online (Sandbox Code Playgroud)
有没有办法在模块或根内部使用 count,而不重构 [count.index] 的整个代码(谁知道可能会导致许多其他问题)?
我使用 Terraform 代码创建了一个 ec2 实例并添加了一个标签。但我还想添加一个默认实例名称,以显示在下面屏幕截图所示的字段中。
我如何使用 Terraform 做到这一点?
现在,我尝试了default标签的论证,但似乎不起作用。我发现很难从文档中弄清楚。
我目前用来创建它的代码:
resource "aws_instance" "webserver" {
ami = "ami-09a41e26df464c548"
instance_type = "t2.micro"
tags = {
default = "my-ec2-instance"
"Terraform" = "Yes"
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用 Azure 市场中的 Terraform 创建 Sendgrid 订阅。目前我必须使用 UI,但最好创建订阅并自动检索 Terraform 中 Sendgrid 的访问令牌。
我正在尝试使用 Terraform 配置 AWS Aurora Postgres Serverless v2。我在这里阅读了文档,它说,它只需要这些参数:
\nengine_mode设置"provisioned"serverlessv2_scaling_configuration在集群中resource "aws_rds_cluster_instance"instance_class设置为"db.serverless"但我返回了这个错误
\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Unsupported block type\n\xe2\x94\x82 \n\xe2\x94\x82 on ../../modules/postgres_serverless_v2/main.tf line 70, in resource "aws_rds_cluster" "pg":\n\xe2\x94\x82 70: serverlessv2_scaling_configuration {\n\xe2\x94\x82 \n\xe2\x94\x82 Blocks of type "serverlessv2_scaling_configuration" are not expected here.\nRun Code Online (Sandbox Code Playgroud)\n当我尝试寻找解决方案时,我不太确定下一步该做什么,但每个人似乎对此都没有问题。
\n下面是我的代码片段:
\nresource "aws_rds_cluster" "pg" {\n cluster_identifier = "${var.namespace}-${var.env_name}-${var.name}-pg-v2"\n engine = "aurora-postgresql"\n engine_version = null\n engine_mode = "provisioned" # still using serverless. requirement …Run Code Online (Sandbox Code Playgroud) amazon-web-services terraform terraform-provider-aws aws-aurora-serverless
我的地形部署遇到了一个小问题。
新的任务定义是在部署期间创建的。然后我也在网络控制台中看到了这一点。ecs 现在应该实际使用这个新任务定义并更新它。相反,会再次部署最新的修订版。
我的期望是,如果我创建一个新的任务定义并将 arn 从该定义传递到 ecs,它将被部署。
不幸的是,我不知道从这里该去哪里。
我使用以下配置创建了一个 ecs 服务:
resource "aws_ecs_service" "service" {
name = "${var.appPrefix}-api-service-${var.envPrefix}"
cluster = var.ecsClusterId
task_definition = aws_ecs_task_definition.ecsTaskDefinition.arn
platform_version = "1.3.0"
desired_count = 1
deployment_minimum_healthy_percent = 50
deployment_maximum_percent = 200
launch_type = "FARGATE"
scheduling_strategy = "REPLICA"
wait_for_steady_state = true
force_new_deployment = true
network_configuration {
security_groups = [var.ecsSecurityGroupId]
subnets = [var.privateSubnet1Id, var.privateSubnet2Id]
assign_public_ip = false
}
load_balancer {
target_group_arn = var.targetGroupId
container_name = var.containerName
container_port = var.httpPort
}
lifecycle {
ignore_changes = [task_definition, desired_count]
} …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-ecs terraform aws-fargate terraform-provider-aws
我确实有 SQS 的默认策略,如下所示。参考了文档 - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy
如果需要 sns 订阅,我想将该策略附加到默认策略之上。
默认策略如下
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:SendMessage*"
],
"Resource": [
"${aws_sqs_queue.queue.arn}"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
附加政策如下
{
"Sid": "topic-subscription-arn-test",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "SQS:SendMessage",
"Resource": ["${aws_sqs_queue.queue.arn}"],
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn-test"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚尝试过,但政策被覆盖了。有什么想法可以使用 Terraform 附加策略吗?提前致谢。正在寻找这种方法的某种想法吗?可能有超过 2 个策略,因此我正在尝试附加这些策略。
amazon-sqs amazon-web-services terraform terraform-provider-aws
我有一个 Terraform 配置(以及其他资源),可以在 Google Cloud 上创建 Google Kubernetes Engine 集群。我使用kubectl提供程序为 ManagedCertificate 和 FrontendConfig 添加 YAML 清单,因为这些不是 kubernetes 或 google 提供程序的一部分。从本地计算机应用 Terraform 配置时,这可以按预期工作,但是当我尝试在 CI 管道中执行它时,这两个资源都会出现以下错误kubectl_manifest:
Error: failed to create kubernetes rest client for read of resource: Get "http://localhost/api?timeout=32s": dial tcp 127.0.0.1:80: connect: connection refused
Run Code Online (Sandbox Code Playgroud)
由于我只在 CI 期间遇到这个问题,我的第一个猜测是服务帐户缺少正确的范围,但据我所知,所有范围都存在。非常感谢任何建议和想法!
我正在尝试找出一种向资源添加递增数字的方法。
这是我的代码片段。我想让优先级成为一个递增的数字,而不是传递一个固定的数字。
当前代码
resource "azurerm_firewall_network_rule_collection" "netrc" {
for_each = {for network_rule_collection in var.network_rule_collections: network_rule_collection.name => network_rule_collection}
name = "netrc-${each.key}"
azure_firewall_name = var.afw_name
resource_group_name = var.resource_group_name
priority = var.priority
action = each.value.action
Run Code Online (Sandbox Code Playgroud)
基本上,我想看起来像这样:
priority = (each.index * 10) + 140
Run Code Online (Sandbox Code Playgroud)
我尝试使用each.key,但在这个模块中,each.key是一个字符串。
我也尝试了计数器,但不能将计数器与 for_each 循环结合起来。
有什么想法或建议吗?
编辑
我最终按照下面的建议使用了索引。
priority = (index(var.application_rule_collections, each.value) + 100)
Run Code Online (Sandbox Code Playgroud) terraform ×10
azure ×2
kubernetes ×2
amazon-ec2 ×1
amazon-ecs ×1
amazon-eks ×1
amazon-sqs ×1
amazon-vpc ×1
aws-fargate ×1
aws-lambda ×1
kubectl ×1
sendgrid ×1
terragrunt ×1
twilio ×1