From 9bb28d09bae5b07fa6aa3dde69e210d487ad9305 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:23:46 +0700 Subject: [PATCH 1/6] issue-161: initialize the lamda function --- .../.terraform.lock.hcl | 19 ++++++++ .../assets/lamda_function.py | 11 +++++ .../serverless-basic-application/lamda.tf | 45 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tutorial/serverless-basic-application/assets/lamda_function.py create mode 100644 tutorial/serverless-basic-application/lamda.tf diff --git a/tutorial/serverless-basic-application/.terraform.lock.hcl b/tutorial/serverless-basic-application/.terraform.lock.hcl index 983f763..65f6468 100644 --- a/tutorial/serverless-basic-application/.terraform.lock.hcl +++ b/tutorial/serverless-basic-application/.terraform.lock.hcl @@ -1,6 +1,25 @@ # This file is maintained automatically by "terraform init". # Manual edits may be lost in future updates. +provider "registry.terraform.io/hashicorp/archive" { + version = "2.4.0" + hashes = [ + "h1:EtN1lnoHoov3rASpgGmh6zZ/W6aRCTgKC7iMwvFY1yc=", + "zh:18e408596dd53048f7fc8229098d0e3ad940b92036a24287eff63e2caec72594", + "zh:392d4216ecd1a1fd933d23f4486b642a8480f934c13e2cae3c13b6b6a7e34a7b", + "zh:655dd1fa5ca753a4ace21d0de3792d96fff429445717f2ce31c125d19c38f3ff", + "zh:70dae36c176aa2b258331ad366a471176417a94dd3b4985a911b8be9ff842b00", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7d8c8e3925f1e21daf73f85983894fbe8868e326910e6df3720265bc657b9c9c", + "zh:a032ec0f0aee27a789726e348e8ad20778c3a1c9190ef25e7cff602c8d175f44", + "zh:b8e50de62ba185745b0fe9713755079ad0e9f7ac8638d204de6762cc36870410", + "zh:c8ad0c7697a3d444df21ff97f3473a8604c8639be64afe3f31b8ec7ad7571e18", + "zh:df736c5a2a7c3a82c5493665f659437a22f0baf8c2d157e45f4dd7ca40e739fc", + "zh:e8ffbf578a0977074f6d08aa8734e36c726e53dc79894cfc4f25fadc4f45f1df", + "zh:efea57ff23b141551f92b2699024d356c7ffd1a4ad62931da7ed7a386aef7f1f", + ] +} + provider "registry.terraform.io/hashicorp/aws" { version = "5.5.0" hashes = [ diff --git a/tutorial/serverless-basic-application/assets/lamda_function.py b/tutorial/serverless-basic-application/assets/lamda_function.py new file mode 100644 index 0000000..f511897 --- /dev/null +++ b/tutorial/serverless-basic-application/assets/lamda_function.py @@ -0,0 +1,11 @@ +# import the JSON utility package since we will be working with a JSON object +import json +# define the handler function that the Lambda service will use as an entry point +def lambda_handler(event, context): +# extract values from the event object we got from the Lambda service + name = event['firstName'] +' '+ event['lastName'] +# return a properly formatted JSON object + return { + 'statusCode': 200, + 'body': json.dumps('Hello from Lambda, ' + name) + } \ No newline at end of file diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf new file mode 100644 index 0000000..23be7dc --- /dev/null +++ b/tutorial/serverless-basic-application/lamda.tf @@ -0,0 +1,45 @@ +// Saving sourcecode to s3 +data "archive_file" "lambda_hello_world" { + type = "zip" + + source_dir = "${path.module}/assets/lamda_function.py" + output_path = "${path.module}/assets/lamda_function.zip" +} + +resource "aws_s3_bucket" "tungbq_lamda_source" { + bucket = "tungbq-lamda-source" + + tags = { + Name = "S3 bucket to store website" + Environment = "Dev" + } +} + + +resource "aws_s3_object" "lambda_hello_world" { + bucket = aws_s3_bucket.tungbq_lamda_source.id + + key = "lamda_function.zip" + source = data.archive_file.lambda_hello_world.output_path + + etag = filemd5(data.archive_file.lambda_hello_world.output_path) +} + +resource "aws_lambda_function" "hello_world" { + function_name = "HelloWorld" + + s3_bucket = aws_s3_bucket.tungbq_lamda_source.id + s3_key = aws_s3_object.lambda_hello_world.key + + runtime = "python3.8" + + source_code_hash = data.archive_file.lambda_hello_world.output_base64sha256 + + # role = aws_iam_role.lambda_exec.arn +} + +resource "aws_cloudwatch_log_group" "hello_world" { + name = "/aws/lambda/${aws_lambda_function.hello_world.function_name}" + + retention_in_days = 30 +} From acd5fc188eab919d5e9ad4b15b28fb54b84e2b69 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:26:07 +0700 Subject: [PATCH 2/6] issue-161: fix missing role --- .../serverless-basic-application/lamda.tf | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf index 23be7dc..5a54f12 100644 --- a/tutorial/serverless-basic-application/lamda.tf +++ b/tutorial/serverless-basic-application/lamda.tf @@ -35,7 +35,7 @@ resource "aws_lambda_function" "hello_world" { source_code_hash = data.archive_file.lambda_hello_world.output_base64sha256 - # role = aws_iam_role.lambda_exec.arn + role = aws_iam_role.lambda_exec.arn } resource "aws_cloudwatch_log_group" "hello_world" { @@ -43,3 +43,25 @@ resource "aws_cloudwatch_log_group" "hello_world" { retention_in_days = 30 } + +resource "aws_iam_role" "lambda_exec" { + name = "serverless_lambda" + + assume_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [{ + Action = "sts:AssumeRole" + Effect = "Allow" + Sid = "" + Principal = { + Service = "lambda.amazonaws.com" + } + } + ] + }) +} + +resource "aws_iam_role_policy_attachment" "lambda_policy" { + role = aws_iam_role.lambda_exec.name + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" +} \ No newline at end of file From 982cf3948f33e7b754d93f59819e3457fea59a74 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:34:54 +0700 Subject: [PATCH 3/6] Fix zip issue --- tutorial/serverless-basic-application/lamda.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf index 5a54f12..668815b 100644 --- a/tutorial/serverless-basic-application/lamda.tf +++ b/tutorial/serverless-basic-application/lamda.tf @@ -2,7 +2,7 @@ data "archive_file" "lambda_hello_world" { type = "zip" - source_dir = "${path.module}/assets/lamda_function.py" + source_dir = "${path.module}/assets" output_path = "${path.module}/assets/lamda_function.zip" } From fa6bf19f75a87c8ef55633962ced2374b22d4391 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:38:54 +0700 Subject: [PATCH 4/6] issue-161: Add lamda handler --- tutorial/serverless-basic-application/lamda.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf index 668815b..d80824f 100644 --- a/tutorial/serverless-basic-application/lamda.tf +++ b/tutorial/serverless-basic-application/lamda.tf @@ -32,6 +32,7 @@ resource "aws_lambda_function" "hello_world" { s3_key = aws_s3_object.lambda_hello_world.key runtime = "python3.8" + handler = "lamda_function.lambda_handler" source_code_hash = data.archive_file.lambda_hello_world.output_base64sha256 From 95594280f289f0e827e0ef22063d0a881312b044 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:55:37 +0700 Subject: [PATCH 5/6] i161: adding output file --- tutorial/serverless-basic-application/outputs.tf | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tutorial/serverless-basic-application/outputs.tf diff --git a/tutorial/serverless-basic-application/outputs.tf b/tutorial/serverless-basic-application/outputs.tf new file mode 100644 index 0000000..3fb7f0a --- /dev/null +++ b/tutorial/serverless-basic-application/outputs.tf @@ -0,0 +1,5 @@ +output "function_name" { + description = "Name of the Lambda function." + + value = aws_lambda_function.hello_world.function_name +} From d6798133c4f1523c541e7325e467821954dafad0 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 11:58:18 +0700 Subject: [PATCH 6/6] i161: update lamda description --- tutorial/serverless-basic-application/lamda.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf index d80824f..482c545 100644 --- a/tutorial/serverless-basic-application/lamda.tf +++ b/tutorial/serverless-basic-application/lamda.tf @@ -10,7 +10,7 @@ resource "aws_s3_bucket" "tungbq_lamda_source" { bucket = "tungbq-lamda-source" tags = { - Name = "S3 bucket to store website" + Name = "S3 bucket to store lamda source code" Environment = "Dev" } } @@ -65,4 +65,4 @@ resource "aws_iam_role" "lambda_exec" { resource "aws_iam_role_policy_attachment" "lambda_policy" { role = aws_iam_role.lambda_exec.name policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" -} \ No newline at end of file +}