Skip to content

Commit

Permalink
add option to schedule project lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Dec 2, 2021
1 parent b71b78b commit 427fbf6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions cli/controller/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (d *Deploy) workspaceFunction2dto(w domain.Function) dto.Function {
MemorySize: w.MemorySize,
Timeout: w.Timeout,
Env: w.Env,
Cron: w.Cron,
}
}

Expand Down
4 changes: 4 additions & 0 deletions domain/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FunctionConfiguration struct {
MemorySize int `yaml:"memory_size" jsonschema:"minimum=128,maximum=10240"`
Timeout int `yaml:"timeout" jsonschema:"minimum=1,maximum=900"`
Env map[string]string `yaml:"env" jsonschema:"nullable"`
Cron string `yaml:"cron,omitempty"`
}

// merge function configuration from multiple sources ordered by priority
Expand All @@ -56,6 +57,9 @@ func (fc *FunctionConfiguration) merge(sources ...FunctionConfiguration) bool {
if s.Timeout != 0 {
merged.Timeout = s.Timeout
}
if s.Cron != "" {
merged.Cron = s.Cron
}
for k, v := range s.Env {
if merged.Env == nil {
merged.Env = make(map[string]string)
Expand Down
6 changes: 4 additions & 2 deletions domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ type FunctionEnvironmentConfig struct {
FunctionConfiguration `yaml:",inline"`
}

const environmentConfigExample = `# Here you can define various configuration parameters
# for functions such as environment variables, memory size and timeout duration.
const environmentConfigExample = `# Here you can define various configuration parameters for functions
# such as environment variables, memory size, execution schedule and timeout duration.
# These can be defined on a project, stage or function level. If the same parameter is
# defined on multiple levels the lowest level will take precedence.
# For example, uncommenting the config below will result in
# the function ping having the following configuration:
# memory_size: 512
# timeout: 60
# cron: "* * * * ? *"
# env:
# KEY: project
# KEY2: stage
Expand All @@ -237,6 +238,7 @@ const environmentConfigExample = `# Here you can define various configuration pa
# functions:
# - name: ping
# memory_size: 512
# cron: "* * * * ? *"
# env:
# KEY3: function
`
Expand Down
1 change: 1 addition & 0 deletions node/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Function struct {
MemorySize int
Timeout int
Env map[string]string
Cron string
}

type DeployResponse struct {
Expand Down
29 changes: 29 additions & 0 deletions node/terraform/modules/functions-node/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ data "aws_iam_policy_document" "deploy" {
"arn:aws:dynamodb:*:*:table/*-${var.suffix}",
]
}
statement {
effect = "Allow"
actions = [
"events:TagResource",
"events:ListTagsForResource",
"events:PutRule",
"events:DescribeRule",
"events:DeleteRule",
"events:PutTargets",
"events:ListTargetsByRule",
"events:RemoveTargets",
]
resources = [
"arn:aws:events:*:*:rule/*-${var.suffix}",
]
}
}

data "aws_iam_policy_document" "security" {
Expand Down Expand Up @@ -273,4 +289,17 @@ data "aws_iam_policy_document" "destroy" {
"*"
]
}
statement {
effect = "Allow"
actions = [
"events:ListTagsForResource",
"events:DescribeRule",
"events:DeleteRule",
"events:ListTargetsByRule",
"events:RemoveTargets",
]
resources = [
"arn:aws:events:*:*:rule/*-${var.suffix}",
]
}
}
21 changes: 21 additions & 0 deletions node/terraform/modules/functions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ locals {
path : try(f.path, k) // default path is function's name
architecture : try(f.architecture, "arm64") // default architecture is arm64
env : length(try(f.env, {})) == 0 ? null : try(f.env, {})
cron : try(f.cron, "")
layers : try(f.layers, [])
policy : try(f.policy, jsonencode({
Version = "2012-10-17"
Expand Down Expand Up @@ -57,3 +58,23 @@ resource "aws_cloudwatch_log_group" "functions_log_groups" {
name = "/aws/lambda/${each.value.function_name}"
retention_in_days = 14
}

resource "aws_cloudwatch_event_rule" "cron" {
for_each = { for k, v in local.functions : k => v if v.cron != "" }
name = each.value.function_name
schedule_expression = "cron(${each.value.cron})"
}

resource "aws_cloudwatch_event_target" "cron" {
for_each = aws_cloudwatch_event_rule.cron
rule = each.value.name
arn = aws_lambda_function.functions[each.key].arn
}

resource "aws_lambda_permission" "cron" {
for_each = aws_cloudwatch_event_rule.cron
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.functions[each.key].function_name}"
principal = "events.amazonaws.com"
source_arn = "${each.value.arn}"
}
1 change: 1 addition & 0 deletions node/terraform/templates/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ locals {
{{$key}} = "{{$value}}"
{{- end}}
}
cron = "{{.Cron}}"
}
{{- end}}
}
Expand Down
1 change: 1 addition & 0 deletions node/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestRenderProject(t *testing.T) {
{
Name: "function1",
S3Key: "function1.zip",
Cron: "* * * * ? *",
},
{
Name: "function2",
Expand Down
2 changes: 2 additions & 0 deletions node/terraform/testdata/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ locals {
timeout = 0
env = {
}
cron = "* * * * ? *"
}
function2 = {
s3_key = "function2.zip"
Expand All @@ -21,6 +22,7 @@ locals {
timeout = 0
env = {
}
cron = ""
}
}
ws_env = {
Expand Down

0 comments on commit 427fbf6

Please sign in to comment.