Skip to content

Commit e5c3979

Browse files
committed
feat(ci): Terraform module to monitor CodeCommit repo and trigger CodePipeline
0 parents  commit e5c3979

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

locals.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
locals {
2+
git_branch = var.branch_to_monitor
3+
codepipeline_arn = var.codepipeline_arn
4+
repo_arn = var.codecommit_repo_arn
5+
}

main.tf

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Listen for activity on the CodeCommit repo and trigger the CodePipeline
2+
resource "aws_cloudwatch_event_rule" "codecommit_activity" {
3+
name = "${var.tag}-codecommit-activity"
4+
description = "Detect commits to CodeCommit repo of ${var.tag}"
5+
6+
event_pattern = <<PATTERN
7+
{
8+
"source": [ "aws.codecommit" ],
9+
"detail-type": [ "CodeCommit Repository State Change" ],
10+
"resources": [ "${local.repo_arn}" ],
11+
"detail": {
12+
"event": [
13+
"referenceCreated",
14+
"referenceUpdated"
15+
],
16+
"referenceType":["branch"],
17+
"referenceName": ["${local.git_branch}"]
18+
}
19+
}
20+
PATTERN
21+
}
22+
23+
resource "aws_cloudwatch_event_target" "cloudwatch_triggers_pipeline" {
24+
target_id = "${var.tag}-commits-trigger-pipeline"
25+
rule = aws_cloudwatch_event_rule.codecommit_activity.name
26+
arn = local.codepipeline_arn
27+
role_arn = aws_iam_role.cloudwatch_ci_role.arn
28+
}
29+
30+
# Allows the CloudWatch event to assume roles
31+
resource "aws_iam_role" "cloudwatch_ci_role" {
32+
name_prefix = "${var.tag}-cloudwatch-ci-"
33+
34+
assume_role_policy = <<DOC
35+
{
36+
"Version": "2012-10-17",
37+
"Statement": [
38+
{
39+
"Sid": "",
40+
"Effect": "Allow",
41+
"Principal": {
42+
"Service": "events.amazonaws.com"
43+
},
44+
"Action": "sts:AssumeRole"
45+
}
46+
]
47+
}
48+
DOC
49+
}
50+
data "aws_iam_policy_document" "cloudwatch_ci_iam_policy" {
51+
statement {
52+
actions = [
53+
"iam:PassRole"
54+
]
55+
resources = [
56+
"*"
57+
]
58+
}
59+
statement {
60+
# Allow CloudWatch to start the Pipeline
61+
actions = [
62+
"codepipeline:StartPipelineExecution"
63+
]
64+
resources = [
65+
local.codepipeline_arn
66+
]
67+
}
68+
}
69+
resource "aws_iam_policy" "cloudwatch_ci_iam_policy" {
70+
name_prefix = "${var.tag}-cloudwatch-ci-"
71+
policy = data.aws_iam_policy_document.cloudwatch_ci_iam_policy.json
72+
}
73+
resource "aws_iam_role_policy_attachment" "cloudwatch_ci_iam" {
74+
policy_arn = aws_iam_policy.cloudwatch_ci_iam_policy.arn
75+
role = aws_iam_role.cloudwatch_ci_role.name
76+
}

outputs.tf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
output "cloudwatch_rule_id" {
2+
value = aws_cloudwatch_event_rule.codecommit_activity.id
3+
}
4+
output "cloudwatch_target_id" {
5+
value = aws_cloudwatch_event_target.cloudwatch_triggers_pipeline.id
6+
}

variables.tf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "codepipeline_arn" {
2+
description = "Trigger the following CodePipeline"
3+
}
4+
variable "codecommit_repo_arn" {
5+
description = "The repo which will be monitored"
6+
}
7+
variable "branch_to_monitor" {
8+
description = "Monitor changes on this branch of CodeCommit"
9+
}
10+
variable "tag" {
11+
description = "Tags the resources with this name"
12+
}

0 commit comments

Comments
 (0)