generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Allows deploying custom Lambda functions * This creates an S3 bucket where zipped lambda functions can be placed, and referenced to deploy as a Lambda function. * Policies can also be placed in the S3 bucket and referenced, which will then be created and attached to the Lambda role.
- Loading branch information
Showing
9 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
resource "aws_s3_bucket" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = "${local.resource_prefix_hash}-lambda-custom-functions" | ||
} | ||
|
||
resource "aws_s3_bucket_policy" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.lambda_custom_functions_store[0].id | ||
policy = templatefile( | ||
"${path.module}/policies/s3-bucket-policy.json.tpl", | ||
{ | ||
statement = <<EOT | ||
[ | ||
${templatefile("${path.root}/policies/s3-bucket-policy-statements/enforce-tls.json.tpl", | ||
{ | ||
bucket_arn = aws_s3_bucket.lambda_custom_functions_store[0].arn | ||
} | ||
)} | ||
] | ||
EOT | ||
} | ||
) | ||
} | ||
|
||
resource "aws_s3_bucket_public_access_block" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.lambda_custom_functions_store[0].id | ||
block_public_acls = true | ||
block_public_policy = true | ||
ignore_public_acls = true | ||
restrict_public_buckets = true | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.lambda_custom_functions_store[0].id | ||
|
||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_logging" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.lambda_custom_functions_store[0].id | ||
|
||
target_bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
target_prefix = "s3/cloudformation-custom-stack-templates" | ||
} | ||
|
||
resource "aws_s3_bucket_server_side_encryption_configuration" "lambda_custom_functions_store" { | ||
count = local.enable_lambda_functions_s3_store ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.lambda_custom_functions_store[0].id | ||
|
||
rule { | ||
apply_server_side_encryption_by_default { | ||
kms_master_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
sse_algorithm = local.infrastructure_kms_encryption ? "aws:kms" : "AES256" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
resource "aws_security_group" "custom_lambda" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? { | ||
for k, custom_lambda in local.custom_lambda_functions : k => custom_lambda if custom_lambda["launch_in_infrastructure_vpc"] == true | ||
} : {} | ||
|
||
name = "${local.resource_prefix}-custom-lambda-${each.key}" | ||
description = "${local.resource_prefix} custom lambda ${each.key}" | ||
vpc_id = aws_vpc.infrastructure[0].id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
resource "aws_cloudwatch_log_group" "lambda_custom_functions" { | ||
for_each = local.custom_lambda_functions | ||
|
||
name = "/aws/lambda/${local.project_name}-${each.key}-custom-lambda" | ||
kms_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
retention_in_days = each.value["log_retention"] | ||
} | ||
|
||
resource "aws_iam_role" "lambda_custom_functions" { | ||
for_each = local.custom_lambda_functions | ||
|
||
name = "${local.resource_prefix_hash}-${substr(sha512("${each.key}-custom-lambda"), 0, 6)}" | ||
description = "${local.resource_prefix}-${each.key}-custom-lambda" | ||
assume_role_policy = templatefile( | ||
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl", | ||
{ services = jsonencode(["lambda.amazonaws.com"]) } | ||
) | ||
} | ||
|
||
resource "aws_iam_policy" "lambda_custom_functions" { | ||
for_each = local.custom_lambda_functions | ||
|
||
name = "${local.resource_prefix}-${each.key}-custom-lambda" | ||
policy = templatefile( | ||
"${path.root}/policies/lambda-default.json.tpl", | ||
{ | ||
region = local.aws_region | ||
account_id = local.aws_account_id | ||
function_name = "${local.project_name}-${each.key}-custom-lambda" | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "lambda_custom_functions" { | ||
for_each = local.custom_lambda_functions | ||
|
||
role = aws_iam_role.lambda_custom_functions[0].name | ||
policy_arn = aws_iam_policy.lambda_custom_functions[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "lambda_custom_functions_additional" { | ||
for_each = { | ||
for k, custom_lambda in local.custom_lambda_functions : k => custom_lambda if custom_lambda["s3_function_store_policy_key"] != null | ||
} | ||
|
||
name = "${local.resource_prefix}-${each.key}-custom-lambda-additional" | ||
policy = data.aws_s3_object.lambda_custom_functions_policy[each.key].body | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "lambda_custom_functions_additional" { | ||
for_each = { | ||
for k, custom_lambda in local.custom_lambda_functions : k => custom_lambda if custom_lambda["s3_function_store_policy_key"] != null | ||
} | ||
|
||
role = aws_iam_role.lambda_custom_functions[0].name | ||
policy_arn = aws_iam_policy.lambda_custom_functions_additional[0].arn | ||
} | ||
|
||
resource "aws_lambda_function" "custom" { | ||
for_each = local.custom_lambda_functions | ||
|
||
s3_bucket = data.aws_s3_object.lambda_custom_functions[each.key].bucket | ||
s3_key = data.aws_s3_object.lambda_custom_functions[each.key].key | ||
s3_object_version = data.aws_s3_object.lambda_custom_functions[each.key].version_id | ||
|
||
function_name = "${local.resource_prefix}-custom-${each.key}" | ||
description = "${local.resource_prefix} Custom ${each.key}" | ||
handler = each.value["handler"] | ||
runtime = each.value["runtime"] | ||
role = aws_iam_role.lambda_custom_functions[0].name | ||
memory_size = each.value["memory"] | ||
package_type = "Zip" | ||
timeout = each.value["timeout"] | ||
|
||
environment { | ||
variables = each.value["environment_variables"] | ||
} | ||
|
||
dynamic "vpc_config" { | ||
for_each = each.value["launch_in_infrastructure_vpc"] == true ? [1] : [] | ||
content { | ||
security_group_ids = [aws_security_group.custom_lambda[each.key].id] | ||
subnet_ids = local.infrastructure_vpc_network_enable_private ? [for subnet in aws_subnet.infrastructure_private : subnet.id] : local.infrastructure_vpc_network_enable_public ? [for subnet in aws_subnet.infrastructure_public : subnet.id] : null | ||
} | ||
} | ||
|
||
tracing_config { | ||
mode = "Active" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters