Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS Lambda log retention config #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.kotless.gen.factory.resource.dynamic

import io.kotless.resource.Lambda
import io.kotless.gen.GenerationContext
import io.kotless.gen.GenerationFactory
import io.kotless.gen.factory.info.InfoFactory
import io.kotless.hcl.HCLEntity
import io.kotless.hcl.HCLTextField
import io.kotless.hcl.ref
import io.kotless.resource.Lambda
import io.kotless.terraform.functions.*
import io.kotless.terraform.provider.aws.data.iam.iam_policy_document
import io.kotless.terraform.provider.aws.resource.cloudwatch.cloudwatch_log_group
import io.kotless.terraform.provider.aws.resource.iam.iam_role
import io.kotless.terraform.provider.aws.resource.iam.iam_role_policy
import io.kotless.terraform.provider.aws.resource.lambda.lambda_function
Expand Down Expand Up @@ -61,6 +62,11 @@ object LambdaFactory : GenerationFactory<Lambda, LambdaFactory.Output> {
policy = policy_document::json.ref
}

val cloudwatch_log_group = cloudwatch_log_group(context.names.tf(entity.name)) {
name = "/aws/lambda/${context.names.aws(entity.name)}"
retention_in_days = context.schema.config.terraform.aws.logRetentionInDays ?: 0
}

val lambda = lambda_function(context.names.tf(entity.name)) {
function_name = context.names.aws(entity.name)

Expand Down Expand Up @@ -88,10 +94,12 @@ object LambdaFactory : GenerationFactory<Lambda, LambdaFactory.Output> {
}
}
}

depends_on = arrayOf(link(cloudwatch_log_group.hcl_ref))
}

return GenerationFactory.GenerationResult(
Output(lambda::arn.ref, lambda::function_name.ref), obj, lambda, assume, iam_role, policy_document, role_policy
Output(lambda::arn.ref, lambda::function_name.ref), obj, lambda, assume, iam_role, policy_document, role_policy, cloudwatch_log_group
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.kotless.terraform.provider.aws.resource.cloudwatch

import io.kotless.terraform.TFFile
import io.kotless.terraform.TFResource

/**
* Terraform aws_cloudwatch_log_group resource.
*
* @see <a href="https://www.terraform.io/docs/providers/aws/r/aws_cloudwatch_log_group.html">aws_cloudwatch_log_group</a>
*/
class CloudWatchLogGroup(id: String) : TFResource(id, "aws_cloudwatch_log_group") {
var name by text()
var retention_in_days by int()
}

fun cloudwatch_log_group(id: String, configure: CloudWatchLogGroup.() -> Unit) = CloudWatchLogGroup(id).apply(configure)

fun TFFile.cloudwatch_log_group(id: String, configure: CloudWatchLogGroup.() -> Unit) {
add(CloudWatchLogGroup(id).apply(configure))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ internal fun KotlessDSL.toSchema(): KotlessConfig {
KotlessConfig.Terraform.AWSProvider(
provider.version,
provider.profile ?: profile,
provider.region ?: region
provider.region ?: region,
provider.logRetentionInDays
)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class KotlessConfig(project: Project) : Serializable {
var profile: String? = null

var region: String? = null

var logRetentionInDays: Int? = null
}

internal val provider = AWSProvider()
Expand Down
2 changes: 1 addition & 1 deletion schema/src/main/kotlin/io/kotless/KotlessConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data class KotlessConfig(
* @param profile AWS profile from a local machine to use for Terraform operations authentication
* @param region AWS region in context of which all Terraform operations should be performed
*/
data class AWSProvider(val version: String, val profile: String, val region: String) : Visitable
data class AWSProvider(val version: String, val profile: String, val region: String, val logRetentionInDays: Int?) : Visitable

override fun visit(visitor: (Any) -> Unit) {
aws.visit(visitor)
Expand Down