Skip to content

Commit

Permalink
Merge pull request #170 from tungbq/issue-161
Browse files Browse the repository at this point in the history
Issue 161: Create lamda function
  • Loading branch information
tungbq authored Jul 1, 2023
2 parents a6da3a8 + d679813 commit f97dad4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tutorial/serverless-basic-application/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tutorial/serverless-basic-application/assets/lamda_function.py
Original file line number Diff line number Diff line change
@@ -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)
}
68 changes: 68 additions & 0 deletions tutorial/serverless-basic-application/lamda.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Saving sourcecode to s3
data "archive_file" "lambda_hello_world" {
type = "zip"

source_dir = "${path.module}/assets"
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 lamda source code"
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"
handler = "lamda_function.lambda_handler"

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
}

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"
}
5 changes: 5 additions & 0 deletions tutorial/serverless-basic-application/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output "function_name" {
description = "Name of the Lambda function."

value = aws_lambda_function.hello_world.function_name
}

0 comments on commit f97dad4

Please sign in to comment.