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

Add parameter to power-tune only cold starts #1

Closed
wants to merge 5 commits into from
Closed
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
66 changes: 66 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ locals {
"${path.module}/state_machines/aws_lambda_power_tuning_state_machine.json",
{
initializerArn = aws_lambda_function.initializer.arn,
publisherArn = aws_lambda_function.publisher.arn,
executorArn = aws_lambda_function.executor.arn,
cleanerArn = aws_lambda_function.cleaner.arn,
analyzerArn = aws_lambda_function.analyzer.arn,
Expand Down Expand Up @@ -42,6 +43,13 @@ locals {
}
)

publisher_template = templatefile(
"${path.module}/json_files/publisher.json",
{
account_id = var.aws_account_id
}
)

optimizer_template = templatefile(
"${path.module}/policies/optimizer.json",
{
Expand Down Expand Up @@ -93,6 +101,13 @@ resource "aws_iam_role" "initializer_role" {
assume_role_policy = file("${path.module}/policies/lambda.json")
}

resource "aws_iam_role" "publisher_role" {
name = "${var.lambda_function_prefix}-publisher_role"
permissions_boundary = var.permissions_boundary
path = local.role_path
assume_role_policy = file("${path.module}/json_files/lambda.json")
}

resource "aws_iam_role" "cleaner_role" {
name = "${var.lambda_function_prefix}-cleaner_role"
permissions_boundary = var.permissions_boundary
Expand Down Expand Up @@ -144,6 +159,20 @@ resource "aws_iam_policy_attachment" "initializer-attach" {
policy_arn = aws_iam_policy.initializer_policy.arn
}

resource "aws_iam_policy" "publisher_policy" {
name = "${var.lambda_function_prefix}_publisher-policy"
description = "Lambda power tuning policy - Publisher - Terraform"

policy = local.publisher_template
}

resource "aws_iam_policy_attachment" "publisher-attach" {
name = "publisher-attachment"
roles = [aws_iam_role.publisher_role.name]
policy_arn = aws_iam_policy.publisher_policy.arn
}


resource "aws_iam_policy" "cleaner_policy" {
name = "${var.lambda_function_prefix}_cleaner-policy"
description = "Lambda power tuning policy - Cleaner - Terraform"
Expand Down Expand Up @@ -335,6 +364,43 @@ resource "aws_lambda_function" "initializer" {
depends_on = [aws_lambda_layer_version.lambda_layer]
}

resource "aws_lambda_function" "publisher" {
filename = ".aws-lambda-power-tuning/src/app.zip"
function_name = "${var.lambda_function_prefix}-publisher"
role = aws_iam_role.publisher_role.arn
handler = "publisher.handler"
layers = [aws_lambda_layer_version.lambda_layer.arn]
memory_size = 128
timeout = 300

# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash = data.archive_file.app.output_base64sha256

runtime = "nodejs20.x"

dynamic "vpc_config" {
for_each = var.vpc_subnet_ids != null && var.vpc_security_group_ids != null ? [true] : []
content {
security_group_ids = var.vpc_security_group_ids
subnet_ids = var.vpc_subnet_ids
}
}

environment {
variables = {
defaultPowerValues = local.defaultPowerValues,
minRAM = local.minRAM,
baseCosts = local.baseCosts,
sfCosts = local.sfCosts,
visualizationURL = local.visualizationURL
}
}

depends_on = [aws_lambda_layer_version.lambda_layer]
}

resource "aws_lambda_function" "optimizer" {
filename = ".aws-lambda-power-tuning/src/app.zip"
function_name = "${var.lambda_function_prefix}-optimizer"
Expand Down
7 changes: 1 addition & 6 deletions policies/initializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
{
"Effect": "Allow",
"Action": [
"lambda:GetAlias",
"lambda:GetFunctionConfiguration",
"lambda:PublishVersion",
"lambda:UpdateFunctionConfiguration",
"lambda:CreateAlias",
"lambda:UpdateAlias"
"lambda:GetFunctionConfiguration"
],
"Resource": "arn:aws:lambda:*:${account_id}:function:*"
}
Expand Down
17 changes: 17 additions & 0 deletions policies/publisher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:GetAlias",
"lambda:GetFunctionConfiguration",
"lambda:PublishVersion",
"lambda:UpdateFunctionConfiguration",
"lambda:CreateAlias",
"lambda:UpdateAlias"
],
"Resource": "arn:aws:lambda:*:${account_id}:function:*"
}
]
}
42 changes: 30 additions & 12 deletions state_machines/aws_lambda_power_tuning_state_machine.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,47 @@
"Initializer": {
"Type": "Task",
"Resource": "${initializerArn}",
"Next": "Branching",
sfloresk marked this conversation as resolved.
Show resolved Hide resolved
"ResultPath": "$.powerValues",
"Next": "Publisher",
"ResultPath": "$.lambdaConfigurations",
"TimeoutSeconds": 600
},
"Publisher": {
"Type": "Task",
"Resource": "${publisherArn}",
"Next": "IsCountReached",
"ResultPath": "$.lambdaConfigurations",
"TimeoutSeconds": 600,
"Catch": [{
"ErrorEquals": [ "States.ALL" ],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
"ErrorEquals": [ "States.ALL" ],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}]
},
"IsCountReached": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.lambdaConfigurations.iterator.continue",
"BooleanEquals": true,
"Next": "Publisher"
}
],
"Default": "Branching"
},
"Branching": {
"Type": "Map",
"Next": "Cleaner",
"ItemsPath": "$.powerValues",
"ItemsPath": "$.lambdaConfigurations.powerValues",
"ResultPath": "$.stats",
"ItemSelector": {
"input.$": "$",
"value.$": "$$.Map.Item.Value"
"input.$": "$",
"value.$": "$$.Map.Item.Value"
},
"MaxConcurrency": 0,
"Catch": [{
"ErrorEquals": ["States.ALL"],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
"ErrorEquals": ["States.ALL"],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}],
"Iterator": {
"StartAt": "Executor",
Expand Down Expand Up @@ -77,4 +95,4 @@
"End": true
}
}
}
}