-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.tf
138 lines (124 loc) · 4 KB
/
main.tf
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# This HACK installs NPM packages on every run so we get lambda function ready to be published
resource "null_resource" "pull_and_install_github_repo" {
triggers {
force_run = "${uuid()}"
}
provisioner "local-exec" {
command = "cd ${path.module}/aws-codepipeline-slack-integration && npm install -production"
}
}
# Zip up Lambda function
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "${path.module}/aws-codepipeline-slack-integration"
output_path = "${path.module}/tmp/aws-codepipeline-slack-integration.zip"
depends_on = ["null_resource.pull_and_install_github_repo"]
}
# Role for Lambda function
resource "aws_iam_role" "lambda_role" {
name = "${var.APP_NAME}-slack-integration-lambda-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
# IAM Policy for Lambda function
resource "aws_iam_role_policy" "lambda_role_policy" {
name = "${var.APP_NAME}-slack-integration-lambda-role-policy"
role = "${aws_iam_role.lambda_role.id}"
policy = <<EOF
{
"Version" : "2012-10-17",
"Statement" : [{
"Sid": "WriteLogsToCloudWatch",
"Effect" : "Allow",
"Action" : [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource" : "arn:aws:logs:*:*:*"
}, {
"Sid": "AllowAccesstoPipeline",
"Effect" : "Allow",
"Action" : [
"codepipeline:GetPipeline",
"codepipeline:GetPipelineState",
"codepipeline:GetPipelineExecution",
"codepipeline:ListPipelineExecutions",
"codepipeline:ListActionTypes",
"codepipeline:ListPipelines"
],
"Resource" : ${jsonencode(formatlist("arn:aws:codepipeline:*:*:%s", var.PIPELINE_NAMES))}
}
]
}
EOF
}
resource "aws_lambda_function" "lambda" {
filename = "${data.archive_file.lambda_zip.output_path}"
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
description = "Posts a message to Slack channel '${var.SLACK_CHANNEL}' every time there is an update to codepipeline execution."
function_name = "${var.APP_NAME}-slack-integration-lambda"
role = "${aws_iam_role.lambda_role.arn}"
handler = "handler.handle"
runtime = "nodejs12.x"
timeout = "${var.LAMBDA_TIMEOUT}"
memory_size = "${var.LAMBDA_MEMORY_SIZE}"
environment {
variables = {
"SLACK_WEBHOOK_URL" = "${var.SLACK_WEBHOOK_URL}"
"SLACK_CHANNEL" = "${var.SLACK_CHANNEL}"
"RELEVANT_STAGES" = "${var.RELEVANT_STAGES}"
}
}
}
# Alias pointing to latest for Lambda function
resource "aws_lambda_alias" "lambda_alias" {
name = "latest"
function_name = "${aws_lambda_function.lambda.arn}"
function_version = "$LATEST"
}
# Cloudwatch event rule
resource "aws_cloudwatch_event_rule" "pipeline_state_update" {
name = "${var.APP_NAME}-slack-integration-pipeline-updated"
description = "Capture state changes in pipelines '${join(", ", var.PIPELINE_NAMES)}'"
event_pattern = <<PATTERN
{
"detail": {
"pipeline": ${jsonencode(var.PIPELINE_NAMES)}
},
"detail-type": [
"CodePipeline Pipeline Execution State Change"
],
"source": [
"aws.codepipeline"
]
}
PATTERN
}
# Allow Cloudwatch to invoke Lambda function
resource "aws_lambda_permission" "allow_cloudwatch" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.pipeline_state_update.arn}"
qualifier = "${aws_lambda_alias.lambda_alias.name}"
}
# Map event rule to trigger lambda function
resource "aws_cloudwatch_event_target" "lambda_trigger" {
rule = "${aws_cloudwatch_event_rule.pipeline_state_update.name}"
arn = "${aws_lambda_alias.lambda_alias.arn}"
}