This repository was archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlambda-healthcheck.tf
More file actions
102 lines (88 loc) · 2.33 KB
/
Copy pathlambda-healthcheck.tf
File metadata and controls
102 lines (88 loc) · 2.33 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
91
92
93
94
95
96
97
98
99
100
101
102
# brew update && brew install terraform
# terraform init
# ** make changes **
# terraform fmt
# terraform plan -out plan.out
# zip -r part.zip part_1.py
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
provider "aws" {
profile = "us_staging_devops_interviewee"
region = "us-east-2"
}
data "aws_iam_policy" "lambda" {
arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
data "aws_iam_policy" "cloudwatch_put_metric" {
arn = "arn:aws:iam::092841053073:policy/cloudwatch-put-metric"
}
resource "aws_iam_role" "lambda_healthcheck" {
name = "lambda-healthcheck"
path = "/service-role/"
tags = {
created-by = "terraform"
}
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "metrics_role" {
role = aws_iam_role.lambda_healthcheck.name
policy_arn = data.aws_iam_policy.lambda.arn
}
resource "aws_iam_role_policy_attachment" "put_metrics_role" {
role = aws_iam_role.lambda_healthcheck.name
policy_arn = data.aws_iam_policy.cloudwatch_put_metric.arn
}
resource "aws_lambda_function" "healthcheck" {
runtime = "python3.8"
handler = "part_1"
function_name = "healthcheck"
filename = "part.zip"
role = aws_iam_role.lambda_healthcheck.arn
publish = true
layers = []
tags = {
Name = "healthcheck"
}
}
resource "aws_cloudwatch_event_rule" "healthcheck_cron" {
name = "healthcheck-schedule"
schedule_expression = "cron(*/30 * * * ? *)"
is_enabled = false
description = "run the healthcheck every 5 minutes"
tags = {
Name = "healthcheck"
}
depends_on = [
aws_lambda_function.healthcheck,
]
}
resource "aws_lambda_permission" "allow_cloudwatch_invoke_healthcheck" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.healthcheck.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.healthcheck_cron.arn
depends_on = [
aws_lambda_function.healthcheck,
aws_cloudwatch_event_rule.healthcheck_cron,
]
}