gpt4 book ai didi

lambda - Terraform cloudwatch 保留逻辑

转载 作者:行者123 更新时间:2023-12-05 04:01:33 24 4
gpt4 key购买 nike

我正在尝试向我的 Terraform 脚本添加逻辑,以便在创建 lambda 时添加 CW 保留逻辑,以便我们的日志在 30 天后清除。我看到的是,当运行 terraform 以更新现有的 lambda(在添加我的新保留逻辑之前部署)时,我的工作失败并出现以下错误。

  • aws_cloudwatch_log_group.lambda-deploy: 1 error(s) occurred:

  • aws_cloudwatch_log_group.lambda-deploy: Creating CloudWatch Log Group failed: ResourceAlreadyExistsException: The specified log group already exists status code: 400, request id: e500eb50-4a81-11e9-9c08-7152b4a0ad31: The CloudWatch Log Group '/aws/lambda/{lambda-name}' already exists.

下面是我如何设置 Terraform 代码:​​

resource "aws_lambda_function" "lambda-deploy" {
filename = "${var.filename}"
function_name = "${var.functionname}"
role = "${var.role}"
handler = "${var.handler}"
runtime = "${var.runtime}"
publish = "${var.publish}"
memory_size = "${var.memory_size}"
timeout = "${var.timeout}"
description = "${var.description}"

layers = "${var.layers}"

environment {
variables = "${var.envVars}"
}

tags {
PLATFORM = "${var.tag_PLATFORM}"
BUSINESS_UNIT = "${var.tag_BUSINESS_UNIT}"
CLIENT = "${var.tag_CLIENT}"
BUSINESS_REGION = "${var.tag_BUSINESS_REGION}"
}

vpc_config {
subnet_ids = "${var.subnet_ids}"
security_group_ids = "${var.security_group_ids}"
}
}

#Below logic will add cloud watch retention logic so logs rotate after 30 days.
resource "aws_cloudwatch_log_group" "lambda-deploy" {
name = "/aws/lambda/${aws_lambda_function.lambda-deploy.function_name}"
retention_in_days = "30"
}

我的问题是,aws_cloudwatch_log_group 资源是否可以检查是否已创建 cloudwatch 组并仅更新保留策略而不是尝试创建日志组?

最佳答案

  1. 注释掉名称参数

以上面的例子为例:

resource "aws_cloudwatch_log_group" "lambda-deploy" {
name = "/aws/lambda/${aws_lambda_function.lambda-deploy.function_name}"
retention_in_days = "30"
}

变成:

resource "aws_cloudwatch_log_group" "lambda-deploy" {
#name = "/aws/lambda/${aws_lambda_function.lambda-deploy.function_name}"
retention_in_days = "30"
}
  1. 导入资源

    terraform import aws_cloudwatch_log_group.lambda-deploy/aws/lambda/${aws_lambda_function.lambda-deploy.function_name}

其中 ${aws_lambda_function.lambda-deploy.function_name} 是您在 AWS 中拥有的内容。

  1. 取消注释您的“名称”参数并运行 terraform plan,您的日志组现在由 terraform 管理......呸!

关于lambda - Terraform cloudwatch 保留逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com