/
cloudwatch-event.tf
71 lines (62 loc) · 1.69 KB
/
cloudwatch-event.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
resource "aws_cloudwatch_event_rule" "codecommit" {
name = "trigger-pipeline"
description = "Trigger Pipeline on CodeCommit change"
event_pattern = <<EOF
{
"source": [
"aws.codecommit"
],
"detail-type": [
"CodeCommit Repository State Change"
],
"resources": [
"arn:aws:codecommit:us-east-1:076858682202:WebApplication"
],
"detail": {
"referenceType": [
"branch"
],
"referenceName": [
"master"
]
}
}
EOF
}
resource "aws_cloudwatch_event_target" "pipeline" {
rule = aws_cloudwatch_event_rule.codecommit.name
target_id = "TriggerCodePipeline"
arn = aws_codepipeline.codepipeline.arn
role_arn = aws_iam_role.codepipeline.arn
}
## Trust Policy
data "aws_iam_policy_document" "pipeline_trust" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
## Policy to allow execution of codepipeline
data "aws_iam_policy_document" "pipeline" {
statement {
effect = "Allow"
actions = ["codepipeline:StartPipelineExecution"]
resources = ["${aws_codepipeline.codepipeline.arn}"]
}
}
resource "aws_iam_policy" "pipeline-policy" {
name = "CWEvent-Role-Codepipeline-Policy"
policy = data.aws_iam_policy_document.pipeline.json
}
# IAM Role that has Trust Policy and policy to allow execution of codepipeline
resource "aws_iam_role" "codepipeline" {
name = "CWEvent-Role-Codepipeline"
assume_role_policy = data.aws_iam_policy_document.pipeline_trust.json
}
resource "aws_iam_role_policy_attachment" "test-attach" {
role = aws_iam_role.codepipeline.name
policy_arn = aws_iam_policy.pipeline-policy.arn
}