diff --git a/terraform/modules/lambda/iam.tf b/terraform/modules/lambda/iam.tf new file mode 100644 index 0000000..ca1e5cd --- /dev/null +++ b/terraform/modules/lambda/iam.tf @@ -0,0 +1,33 @@ +data "aws_iam_policy_document" "lambda_assume_role" { + statement { + effect = "Allow" + + principals { + type = "Service" + identifiers = ["lambda.amazonaws.com"] + } + + actions = ["sts:AssumeRole"] + } +} + +resource "aws_iam_role" "iam_for_lambda" { + name = "LambdaIam" + assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json +} + +data "aws_iam_policy_document" "lambda_policy_doc" { + statement { + effect = "Allow" + actions = var.lambda_iam_actions + + resources = var.lambda_iam_resources + } +} + +resource "aws_iam_policy" "lambda_permissions" { + name = "lambda_permissions" + path = "/" + description = "IAM policy for Lambda" + policy = data.aws_iam_policy_document.lambda_policy_doc.json +} \ No newline at end of file diff --git a/terraform/modules/lambda/main.tf b/terraform/modules/lambda/main.tf new file mode 100644 index 0000000..39e669d --- /dev/null +++ b/terraform/modules/lambda/main.tf @@ -0,0 +1,18 @@ +resource "aws_lambda_function" "test_lambda" { + # If the file is not in the current working directory you will need to include a + # path.module in the filename. + filename = "lambda_function_payload.zip" + function_name = "lambda_function_name" + role = aws_iam_role.iam_for_lambda.arn + handler = "index.test" + + source_code_hash = data.archive_file.lambda.output_base64sha256 + + runtime = "nodejs18.x" + + environment { + variables = { + foo = "bar" + } + } +} \ No newline at end of file diff --git a/terraform/modules/lambda/outputs.tf b/terraform/modules/lambda/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/terraform/modules/lambda/provider.tf b/terraform/modules/lambda/provider.tf new file mode 100644 index 0000000..00a3d3b --- /dev/null +++ b/terraform/modules/lambda/provider.tf @@ -0,0 +1,5 @@ +provider "aws" { + region = var.region + access_key = var.access_key + secret_key = var.secret_key +} \ No newline at end of file diff --git a/terraform/modules/lambda/variables.tf b/terraform/modules/lambda/variables.tf new file mode 100644 index 0000000..b9a4a08 --- /dev/null +++ b/terraform/modules/lambda/variables.tf @@ -0,0 +1,30 @@ +variable access_key { + type = string +} + +variable secret_key { + type = string +} + +variable region { + type = string +} + +variable lambda_name { + type = string +} + +variable env_vars { + type = list(object({ + name = string + value = string + })) +} + +variable lambda_iam_actions { + type = list(string) +} + +variable lambda_iam_resources { + type = list(string) +} \ No newline at end of file