Skip to content

Commit 1d9e57b

Browse files
committed
Adding aws lambda terraform code
1 parent b97f41e commit 1d9e57b

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

aws-lambda/lambda.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def lambda_handler(event, context):
2+
print ("Hello from terraform world")
3+
return "hello from terraform world"

aws-lambda/lambda.tf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
provider "aws" {
2+
region = "us-west-2"
3+
}
4+
5+
#IAM Role for Lambda Function
6+
7+
resource "aws_iam_role" "iam_for_lambda" {
8+
name = "iam_for_lambda"
9+
10+
assume_role_policy = <<EOF
11+
{
12+
"Version": "2012-10-17",
13+
"Statement": [
14+
{
15+
"Action": "sts:AssumeRole",
16+
"Principal": {
17+
"Service": "lambda.amazonaws.com"
18+
},
19+
"Effect": "Allow",
20+
"Sid": ""
21+
}
22+
]
23+
}
24+
EOF
25+
}
26+
27+
resource "aws_iam_policy" "lambda_logging" {
28+
name = "lambda_logging"
29+
path = "/"
30+
description = "IAM policy for logging from a lambda"
31+
32+
policy = <<EOF
33+
{
34+
"Version": "2012-10-17",
35+
"Statement": [
36+
{
37+
"Action": [
38+
"logs:CreateLogStream",
39+
"logs:PutLogEvents"
40+
],
41+
"Resource": "arn:aws:logs:*:*:*",
42+
"Effect": "Allow"
43+
}
44+
]
45+
}
46+
EOF
47+
}
48+
49+
resource "aws_iam_role_policy_attachment" "lambda_logs" {
50+
role = "${aws_iam_role.iam_for_lambda.name}"
51+
policy_arn = "${aws_iam_policy.lambda_logging.arn}"
52+
}
53+
54+
resource "aws_cloudwatch_log_group" "example" {
55+
name = "/aws/lambda/${aws_lambda_function.test_lambda.function_name}"
56+
retention_in_days = 14
57+
}
58+
59+
60+
resource "aws_lambda_function" "test_lambda" {
61+
filename = "lambda.zip"
62+
function_name = "lambda_handler"
63+
role = "${aws_iam_role.iam_for_lambda.arn}"
64+
handler = "lambda.lambda_handler"
65+
66+
# The filebase64sha256() function is available in Terraform 0.11.12 and later
67+
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
68+
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
69+
source_code_hash = "${base64sha256("lambda.zip")}"
70+
runtime = "python2.7"
71+
}

aws-lambda/lambda.zip

253 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)