-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
160 lines (128 loc) · 3.28 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
variable "bucket" {
description = "The bucket that will contain the feed"
}
variable "key" {
description = "The key within the bucket that will contain the feed"
default = "cc.xml"
}
resource "aws_s3_bucket" "ccxml" {
bucket = var.bucket
acl = "public-read"
website {
index_document = "cc.xml"
}
}
output "website" {
value = "http://${aws_s3_bucket.ccxml.website_endpoint}/${var.key}"
}
module "ccxml" {
source = "howdio/lambda/aws//modules/package"
name = "ccxml"
path = path.cwd
}
resource "null_resource" "cctest" {
provisioner "local-exec" {
command = "go get && GOOS=linux GOARCH=amd64 go build -o ccxml"
}
triggers = {
source_file = base64sha256("${path.cwd}/src/main.go")
}
}
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.cwd}/ccxml"
output_path = "${path.cwd}/package.zip"
depends_on = [null_resource.cctest]
}
resource "aws_lambda_function" "ccxml" {
filename = data.archive_file.lambda_zip.output_path
function_name = "ccxml"
handler = "ccxml"
description = "Handler that responds to CodePipeline events by updating a CCTray XML feed"
memory_size = 128
timeout = 20
runtime = "go1.x"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
role = aws_iam_role.ccxml.arn
environment {
variables = {
BUCKET = var.bucket
KEY = var.key
}
}
}
data "aws_iam_policy_document" "ccxml_assume_role_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
}
}
resource "aws_iam_role" "ccxml" {
name = "ccxml"
assume_role_policy = data.aws_iam_policy_document.ccxml_assume_role_policy.json
}
data "aws_iam_policy_document" "ccxml_role_policy" {
statement {
effect = "Allow"
actions = [
"codepipeline:ListPipelines",
"codepipeline:GetPipelineState",
]
resources = [
"*",
]
}
statement {
effect = "Allow"
actions = [
"s3:PutObject",
"s3:PutObjectAcl",
]
resources = [
"arn:aws:s3:::${var.bucket}/${var.key}",
]
}
statement {
effect = "Allow"
actions = [
"logs:*",
]
resources = [
"*",
]
}
}
resource "aws_iam_role_policy" "ccxml" {
role = aws_iam_role.ccxml.id
policy = data.aws_iam_policy_document.ccxml_role_policy.json
}
locals {
event_pattern = {
source = ["aws.codepipeline"]
detail-type = ["CodePipeline Stage Execution State Change"]
}
event_pattern_json = jsonencode(local.event_pattern)
}
resource "aws_cloudwatch_event_rule" "ccxml" {
name = "ccxml"
description = "Rule that matches CodePipeline State Execution State Changes"
event_pattern = local.event_pattern_json
}
resource "aws_cloudwatch_event_target" "ccxml" {
target_id = "ccxml"
rule = aws_cloudwatch_event_rule.ccxml.name
arn = aws_lambda_function.ccxml.arn
}
resource "aws_lambda_permission" "ccxml" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ccxml.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ccxml.arn
}