-
Notifications
You must be signed in to change notification settings - Fork 14
/
trigger.tf
79 lines (69 loc) · 1.88 KB
/
trigger.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
resource "aws_cloudwatch_event_rule" "this" {
name = "${var.service_name}-ecr-trigger"
description = "Capture ECR push events."
tags = merge(var.tags, {
tf_module = basename(path.module)
})
event_pattern = <<PATTERN
{
"detail-type": [
"ECR Image Action"
],
"source": [
"aws.ecr"
],
"detail": {
"action-type": [
"PUSH"
],
"image-tag": [
"${var.ecr_image_tag}"
],
"repository-name": [
"${var.ecr_repository_name}"
],
"result": [
"SUCCESS"
]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "trigger" {
rule = aws_cloudwatch_event_rule.this.name
target_id = "CodePipeline"
arn = aws_codepipeline.codepipeline.arn
role_arn = aws_iam_role.trigger.arn
}
resource "aws_iam_role" "trigger" {
name = "${var.service_name}-${data.aws_region.current.name}-ecr-trigger"
path = "/ecs/deployment/"
assume_role_policy = data.aws_iam_policy_document.trigger-assume-role-policy.json
tags = merge(var.tags, {
tf_module = basename(path.module)
})
}
data "aws_iam_policy_document" "trigger-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "trigger" {
name = "${var.service_name}-${data.aws_region.current.name}-ecr-trigger"
path = "/ecs/deployment/"
policy = data.aws_iam_policy_document.trigger-permissions.json
}
data "aws_iam_policy_document" "trigger-permissions" {
statement {
actions = ["codepipeline:StartPipelineExecution"]
resources = [aws_codepipeline.codepipeline.arn]
}
}
resource "aws_iam_role_policy_attachment" "trigger" {
policy_arn = aws_iam_policy.trigger.arn
role = aws_iam_role.trigger.name
}