Skip to content

Instantly share code, notes, and snippets.

@tnayanam
Created October 23, 2020 20:33
Show Gist options
  • Save tnayanam/85b0336720f0f0986acd17463e4811ce to your computer and use it in GitHub Desktop.
Save tnayanam/85b0336720f0f0986acd17463e4811ce to your computer and use it in GitHub Desktop.
// DONE
resource "aws_lambda_function" "lambda_tf" {
function_name = "FunctionHandler"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "Terra5::Terra5.Function::FunctionHandler"
runtime = "dotnetcore3.1"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda.zip"))}"
# source_code_hash = "${filebase64sha256("Terra7.zip")}"
source_code_hash = "${sha256("Terra7.zip")}"
depends_on = ["aws_iam_role.iam_for_lambda"]
s3_bucket = "test-tanuj1"
s3_key = "test/Terra7.zip"
}
// DONE
// A rold for Lambda
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
// DONE
#See also the following AWS managed policy: AWSLambdaBasicExecutionRole
resource "aws_iam_policy" "lambda_logging" {
name = "lambda_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
},
{
"Action": [
"dynamodb:*",
"dax:*",
"application-autoscaling:DeleteScalingPolicy",
"application-autoscaling:DeregisterScalableTarget",
"application-autoscaling:DescribeScalableTargets",
"application-autoscaling:DescribeScalingActivities",
"application-autoscaling:DescribeScalingPolicies",
"application-autoscaling:PutScalingPolicy",
"application-autoscaling:RegisterScalableTarget",
"cloudwatch:DeleteAlarms",
"cloudwatch:DescribeAlarmHistory",
"cloudwatch:DescribeAlarms",
"cloudwatch:DescribeAlarmsForMetric",
"cloudwatch:GetMetricStatistics",
"cloudwatch:ListMetrics",
"cloudwatch:PutMetricAlarm",
"datapipeline:ActivatePipeline",
"datapipeline:CreatePipeline",
"datapipeline:DeletePipeline",
"datapipeline:DescribeObjects",
"datapipeline:DescribePipelines",
"datapipeline:GetPipelineDefinition",
"datapipeline:ListPipelines",
"datapipeline:PutPipelineDefinition",
"datapipeline:QueryObjects",
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"iam:GetRole",
"iam:ListRoles",
"kms:DescribeKey",
"kms:ListAliases",
"sns:CreateTopic",
"sns:DeleteTopic",
"sns:ListSubscriptions",
"sns:ListSubscriptionsByTopic",
"sns:ListTopics",
"sns:Subscribe",
"sns:Unsubscribe",
"sns:SetTopicAttributes",
"lambda:CreateFunction",
"lambda:ListFunctions",
"lambda:ListEventSourceMappings",
"lambda:CreateEventSourceMapping",
"lambda:DeleteEventSourceMapping",
"lambda:GetFunctionConfiguration",
"lambda:DeleteFunction",
"resource-groups:ListGroups",
"resource-groups:ListGroupResources",
"resource-groups:GetGroup",
"resource-groups:GetGroupQuery",
"resource-groups:DeleteGroup",
"resource-groups:CreateGroup",
"tag:GetResources"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
// DONE
#attach lambda iam role with lambda logging
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = "${aws_iam_role.iam_for_lambda.name}"
policy_arn = "${aws_iam_policy.lambda_logging.arn}"
depends_on = ["aws_iam_role.iam_for_lambda", "aws_iam_policy.lambda_logging"]
}
#print out the lambda function properties
output "lambdafunction-details" {
value = "${aws_lambda_function.lambda_tf}"
}
# DDB
// DONE
resource "aws_dynamodb_table" "example9" {
name = "example9"
hash_key = "TestTableHashKey"
billing_mode = "PAY_PER_REQUEST"
stream_enabled = true
stream_view_type = "NEW_AND_OLD_IMAGES"
attribute {
name = "TestTableHashKey"
type = "S"
}
}
resource "aws_lambda_event_source_mapping" "exam4" {
event_source_arn = "${aws_dynamodb_table.example9.stream_arn}"
function_name = "${aws_lambda_function.lambda_tf.arn}"
starting_position = "LATEST"
depends_on = ["aws_dynamodb_table.example9", "aws_lambda_function.lambda_tf"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment