-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.tf
More file actions
90 lines (77 loc) · 2.26 KB
/
main.tf
File metadata and controls
90 lines (77 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
terraform {
required_version = ">= 0.12.5"
backend "s3" {}
}
provider "aws" {
region = var.region
}
data "terraform_remote_state" "state" {
backend = "s3"
config = {
bucket = var.terraform_state["bucket"]
region = var.region
key = var.terraform_state["key"]
encrypt = true
}
}
data "aws_caller_identity" "current_account_id" {}
module "lambda_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git"
namespace = var.namespace
stage = var.stage
name = var.name
attributes = ["lambda", var.region]
delimiter = var.delimiter
tags = var.tags
}
////////////////////// LAMBDA IAM:
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
sid = "HelloWorldLambdaTrustPolicy"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "lambda" {
statement {
sid = "HelloWorldLambdaPolicy"
effect = "Allow"
actions = [
"logs:PutLogEvents",
"logs:CreateLogGroup",
"logs:CreateLogStream",
]
resources = ["arn:aws:logs:${var.region}:${data.aws_caller_identity.current_account_id.account_id}:*"]
}
}
resource "aws_iam_role" "lambda" {
name = module.lambda_label.id
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
resource "aws_iam_policy" "lambda" {
name = module.lambda_label.id
policy = data.aws_iam_policy_document.lambda.json
}
resource "aws_iam_role_policy_attachment" "lambda" {
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.lambda.arn
}
////////////////////// LAMBDA FUNCTION:
data "archive_file" "lambda" {
type = "zip"
source_dir = "${path.module}/lambda/"
output_path = "${path.module}/lambda/hello_world.zip"
}
resource "aws_lambda_function" "lambda" {
description = "Hello World Lambda function"
filename = join("", data.archive_file.lambda.*.output_path)
function_name = "hello_world"
role = aws_iam_role.lambda.arn
handler = "hello_world.lambda_handler"
source_code_hash = join("", data.archive_file.lambda.*.output_base64sha256)
runtime = "python3.6"
}