-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
76 lines (72 loc) · 1.94 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
# Listen for activity on the CodeCommit repo and trigger the CodePipeline
resource "aws_cloudwatch_event_rule" "codecommit_activity" {
name_prefix = "${var.tag}-${var.branch_to_monitor}-activity"
description = "Detect commits to CodeCommit repo of ${var.tag} on branch ${var.branch_to_monitor}"
event_pattern = <<PATTERN
{
"source": [ "aws.codecommit" ],
"detail-type": [ "CodeCommit Repository State Change" ],
"resources": [ "${local.repo_arn}" ],
"detail": {
"event": [
"referenceCreated",
"referenceUpdated"
],
"referenceType":["branch"],
"referenceName": ["${local.git_branch}"]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "cloudwatch_triggers_pipeline" {
target_id = "${var.tag}-commits-trigger-pipeline"
rule = aws_cloudwatch_event_rule.codecommit_activity.name
arn = local.codepipeline_arn
role_arn = aws_iam_role.cloudwatch_ci_role.arn
}
# Allows the CloudWatch event to assume roles
resource "aws_iam_role" "cloudwatch_ci_role" {
name_prefix = "${var.tag}-cloudwatch-ci-"
assume_role_policy = <<DOC
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
DOC
}
data "aws_iam_policy_document" "cloudwatch_ci_iam_policy" {
statement {
actions = [
"iam:PassRole"
]
resources = [
"*"
]
}
statement {
# Allow CloudWatch to start the Pipeline
actions = [
"codepipeline:StartPipelineExecution"
]
resources = [
local.codepipeline_arn
]
}
}
resource "aws_iam_policy" "cloudwatch_ci_iam_policy" {
name_prefix = "${var.tag}-cloudwatch-ci-"
policy = data.aws_iam_policy_document.cloudwatch_ci_iam_policy.json
}
resource "aws_iam_role_policy_attachment" "cloudwatch_ci_iam" {
policy_arn = aws_iam_policy.cloudwatch_ci_iam_policy.arn
role = aws_iam_role.cloudwatch_ci_role.name
}