Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CodePipeline Source trigger from GitHub to ECR #9

Merged
merged 4 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cloudwatch/ecr-source-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"source": [
"aws.ecr"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"ecr.amazonaws.com"
],
"eventName": [
"PutImage"
],
"requestParameters": {
"repositoryName": [
"${ecr_repository_name}"
],
"imageTag": [
"latest"
robertrossmann marked this conversation as resolved.
Show resolved Hide resolved
]
}
}
}
2 changes: 1 addition & 1 deletion examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
}

provider "aws" {
version = "~> 1.52.0"
version = "~> 1.54.0"
region = "us-east-1"
profile = "playground"
}
Expand Down
62 changes: 62 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ resource "aws_codepipeline" "this" {

configuration {
RepositoryName = "${element(aws_ecr_repository.this.*.name, count.index)}"
ImageTag = "latest"
}
}
}
Expand Down Expand Up @@ -395,3 +396,64 @@ resource "aws_codepipeline" "this" {

depends_on = ["aws_iam_role_policy.codebuild", "aws_ecs_service.this"]
}

### Remove after ECR as CodePipeline Source gets fully integrated with AWS Provider

resource "aws_iam_role" "events" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

name = "${var.name}-${terraform.workspace}-${element(keys(var.services), count.index)}-events-role"

assume_role_policy = "${file("${path.module}/policies/events-role.json")}"
}

data "template_file" "events" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

template = "${file("${path.module}/policies/events-role-policy.json")}"

vars {
codepipeline_arn = "${element(aws_codepipeline.this.*.arn, count.index)}"
}
}

resource "aws_iam_role_policy" "events" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

name = "${var.name}-${terraform.workspace}-${element(keys(var.services), count.index)}-events-role-policy"
role = "${element(aws_iam_role.events.*.id, count.index)}"
policy = "${element(data.template_file.events.*.rendered, count.index)}"
}

data "template_file" "ecr_event" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

template = "${file("${path.module}/cloudwatch/ecr-source-event.json")}"

vars {
ecr_repository_name = "${element(aws_ecr_repository.this.*.name, count.index)}"
}
}

resource "aws_cloudwatch_event_rule" "this" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

name = "${var.name}-${terraform.workspace}-${element(keys(var.services), count.index)}-ecr-event"
description = "Amazon CloudWatch Events rule to automatically start your pipeline when a change occurs in the Amazon ECR image tag."

event_pattern = "${element(data.template_file.ecr_event.*.rendered, count.index)}"

depends_on = ["aws_codepipeline.this"]
}

resource "aws_cloudwatch_event_target" "this" {
count = "${length(var.services) > 0 ? length(var.services) : 0}"

rule = "${element(aws_cloudwatch_event_rule.this.*.name, count.index)}"
target_id = "${var.name}-${terraform.workspace}-${element(keys(var.services), count.index)}-codepipeline"
arn = "${element(aws_codepipeline.this.*.arn, count.index)}"
role_arn = "${element(aws_iam_role.events.*.arn, count.index)}"
}

### End Remove

14 changes: 14 additions & 0 deletions policies/events-role-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"codepipeline:StartPipelineExecution"
],
"Resource": [
"${codepipeline_arn}"
]
}
]
}
12 changes: 12 additions & 0 deletions policies/events-role.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}