I'm trying to add logic to my terraform script so that when a lambda is created a CW retention logic is added, so our logs clear after 30 days. What I'm seeing is that when terraform is run to update an existing lambda (that was deployed prior to my new retention logic being added) my job fails with the below error.
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.
Below is how I have my terraform code set up:
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"
}
My question is, is it possible for the aws_cloudwatch_log_group resource to check if a cloudwatch group has been created and just update the retention policy instead of trying to create the Log Group?