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 3 commits
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
22 changes: 3 additions & 19 deletions build/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,10 @@ version: 0.2
phases:
pre_build:
commands:
- pip install awscli --upgrade --user
- echo `aws --version`
- echo Logging into Amazon ECR...
- $(aws ecr get-login --region ${region} --no-include-email)
- REPOSITORY_URI=${repository_url}
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- echo Entered the pre_build phase...
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:latest -f ${dockerfile} ${dockerfile_path}
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
- apt-get update && apt-get install jq -y
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"${container_name}","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- REPOSITORY_URI=$(cat imageDetail.json | jq .ImageURI)
- printf '[{"name":"${container_name}","imageUri":%s}]' $REPOSITORY_URI > imagedefinitions.json
artifacts:
files: imagedefinitions.json
8 changes: 2 additions & 6 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

## Usage

First update both `repo_name` and `repo_owner` with a Github repository and valid username respectively

> This probably will change in the future by setting the source as the [ECR repository](https://aws.amazon.com/about-aws/whats-new/2018/11/the-aws-developer-tools-improve-continuous-delivery-support-for-aws-fargate-and-amazon-ecs/) instead of a Github repository

And then, to run this example you need to execute:
To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ GITHUB_TOKEN=<token> terraform apply
$ terraform apply
```

Note that this example create resources which can cost money (AWS Fargate Services, for example). Run `terraform destroy` when you don't need these resources.
Expand Down
6 changes: 0 additions & 6 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ module "fargate" {

name = "basic-example"

repo_name = "<github_repo_name>" # CHANGE THIS
repo_owner = "<github_username>" # CHANGE THIS

services = {
api = {
task_definition = "api.json"
Expand All @@ -26,9 +23,6 @@ module "fargate" {

registry_retention_count = 15 # Optional. 20 by default
logs_retention_days = 14 # Optional. 30 by default

dockerfile = "Dockerfile" # Optional. Dockerfile by default
dockerfile_path = "." # Optional. '.' by default
}
}
}
31 changes: 16 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ data "template_file" "buildspec" {
template = "${file("${path.module}/build/buildspec.yml")}"

vars {
container_name = "${element(keys(var.services), count.index)}"
repository_url = "${element(aws_ecr_repository.this.*.repository_url, count.index)}"
region = "${var.region}"
dockerfile = "${lookup(var.services[element(keys(var.services), count.index)], "dockerfile", var.dockerfile_default_name)}"
dockerfile_path = "${lookup(var.services[element(keys(var.services), count.index)], "dockerfile_path", var.dockerfile_default_path)}"
container_name = "${element(keys(var.services), count.index)}"
}
}

Expand Down Expand Up @@ -306,30 +302,37 @@ resource "aws_codebuild_project" "this" {

# CODEPIPELINE
resource "aws_iam_role" "codepipeline" {
name = "${var.name}-${terraform.workspace}-codepipeline-role"
count = "${length(var.services) > 0 ? length(var.services) : 0}"

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

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

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

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

vars {
aws_s3_bucket_arn = "${aws_s3_bucket.this.arn}"
aws_s3_bucket_arn = "${aws_s3_bucket.this.arn}"
ecr_repository_arn = "${element(aws_ecr_repository.this.*.arn, count.index)}"
}
}

resource "aws_iam_role_policy" "codepipeline" {
name = "${var.name}-${terraform.workspace}-codepipeline-role-policy"
count = "${length(var.services) > 0 ? length(var.services) : 0}"

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

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

name = "${var.name}-${terraform.workspace}-${element(keys(var.services), count.index)}-pipeline"
role_arn = "${aws_iam_role.codepipeline.arn}"
role_arn = "${element(aws_iam_role.codepipeline.*.arn, count.index)}"

artifact_store {
location = "${aws_s3_bucket.this.bucket}"
Expand All @@ -342,15 +345,13 @@ resource "aws_codepipeline" "this" {
action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
owner = "AWS"
provider = "ECR"
version = "1"
output_artifacts = ["source"]

configuration {
Owner = "${var.repo_owner}"
Repo = "${var.repo_name}"
Branch = "${terraform.workspace == "default" ? "master" : terraform.workspace}"
RepositoryName = "${element(aws_ecr_repository.this.*.name, count.index)}"
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions policies/codepipeline-role-policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ecr:DescribeImages"
],
"Resource": [
"${ecr_repository_arn}"
]
},
{
"Action": [
"ecs:*",
Expand Down
19 changes: 0 additions & 19 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ variable "region" {
default = "us-east-1"
}

variable "repo_name" {
description = "Name of the repository. Needed for Continious Deployment for ECS services"
}

variable "repo_owner" {
description = "Owner of the repository"
default = "strvcom"
}

variable "development_mode" {
description = "Whether or not create a most robust production-ready infrastructure with ALBs and more than 1 replica"
default = false
Expand Down Expand Up @@ -61,13 +52,3 @@ variable "ecr_default_retention_count" {
variable "services" {
type = "map"
}

## Docker

variable "dockerfile_default_name" {
default = "Dockerfile"
}

variable "dockerfile_default_path" {
default = "."
}