Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Added terraform files to create cross-account codepipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
robbytaylor committed Sep 24, 2019
1 parent 533610f commit 846456e
Show file tree
Hide file tree
Showing 26 changed files with 520 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terraform.tfstate*
.terraform/
.build-harness
build-harness
13 changes: 13 additions & 0 deletions conf.auto.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
accounts = {
pipeline = "111222333444"
target = "555666777888"
}

github = {
owner = ""
repo = ""
branch = ""
}

region = "eu-west-1"
terraform_role = "TerraformUser"
29 changes: 29 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module "pipeline" {
source = "./pipeline-account"

github = var.github
region = var.region
target_role_arn = module.target.target_role_arn
target_bucket_name = var.target_bucket_name
target_kms_key_arn = module.target.kms_key_arn

providers = {
"aws" = "aws.pipeline"
}
}

module "target" {
source = "./target-account"

artifact_bucket_arn = module.pipeline.artifact_bucket_arn
bucket_name = var.target_bucket_name
codepipeline_role_arn = module.pipeline.codepipeline_role_arn

providers = {
"aws" = "aws.target"
}
}

output "test" {
value = module.pipeline.pipeline_arn
}
56 changes: 56 additions & 0 deletions pipeline-account/codepipeline.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
resource "aws_codepipeline" "pipeline" {
name = var.pipeline_name
role_arn = aws_iam_role.codepipeline.arn

artifact_store {
location = aws_s3_bucket.artifact-bucket.bucket
type = "S3"

encryption_key {
id = aws_kms_key.artifacts.arn
type = "KMS"
}
}

stage {
name = "Source"

action {
category = "Source"
name = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"

output_artifacts = ["SourceArtifact"]

configuration = {
Owner = var.github["owner"]
Repo = var.github["repo"]
Branch = var.github["branch"]
}
}
}

stage {
name = "Deploy"

action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "S3"
version = "1"
run_order = "1"
role_arn = var.target_role_arn
input_artifacts = ["SourceArtifact"]

configuration = {
ObjectKey = "DeployedArtifacts"
Extract = "false"
BucketName = var.target_bucket_name
KMSEncryptionKeyARN = var.target_kms_key_arn
}
}
}
}
1 change: 1 addition & 0 deletions pipeline-account/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "aws_caller_identity" "current" {}
3 changes: 3 additions & 0 deletions pipeline-account/iam_policy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_iam_policy" "codepipeline" {
policy = data.aws_iam_policy_document.codepipeline.json
}
5 changes: 5 additions & 0 deletions pipeline-account/iam_policy_attachment.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_iam_policy_attachment" "codepipeline" {
name = "codepipeline-role-attachment"
roles = [aws_iam_role.codepipeline.name]
policy_arn = aws_iam_policy.codepipeline.arn
}
129 changes: 129 additions & 0 deletions pipeline-account/iam_policy_document.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
data "aws_iam_policy_document" "kms-usage" {
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"

actions = [
"kms:*"
]

principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}

resources = ["*"]
}

statement {
sid = "Allow use of the key"
effect = "Allow"

actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
]

principals {
type = "AWS"
identifiers = [
# local.pipeline_arn
"*"
]
}

resources = ["*"]
}

statement {
sid = "Allow attachment of persistent resources"
effect = "Allow"

actions = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
]

principals {
type = "AWS"
identifiers = [
# local.pipeline_arn
"*"
]
}

resources = ["*"]

condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = ["true"]
}
}
}

data "aws_iam_policy_document" "codepipeline" {
statement {
actions = [
"sts:AssumeRole",
]

resources = [
var.target_role_arn,
]
}

statement {
actions = [
"s3:GetObject",
"s3:PutObject"
]

resources = [
aws_s3_bucket.artifact-bucket.arn,
"${aws_s3_bucket.artifact-bucket.arn}/*",
]
}

statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]

resources = ["*"]
}

statement {
effect = "Allow"

actions = [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
]

resources = [
"arn:aws:s3:::${var.target_bucket_name}/*",
"arn:aws:s3:::${var.target_bucket_name}"
]
}

statement {
effect = "Allow"

actions = [
"kms:Encrypt",
"kms:Decrypt"
]

resources = [
var.target_kms_key_arn,
]
}
}
25 changes: 25 additions & 0 deletions pipeline-account/iam_role.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_iam_role" "codepipeline" {
name = "ExamplePipeline"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codepipeline.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
3 changes: 3 additions & 0 deletions pipeline-account/kms_key.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_kms_key" "artifacts" {
policy = data.aws_iam_policy_document.kms-usage.json
}
15 changes: 15 additions & 0 deletions pipeline-account/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
output "artifact_bucket_arn" {
value = aws_s3_bucket.artifact-bucket.arn
}

output "codepipeline_role_arn" {
value = aws_iam_role.codepipeline.arn
}

output "kms_policy" {
value = data.aws_iam_policy_document.kms-usage.json
}

output "pipeline_arn" {
value = local.pipeline_arn
}
1 change: 1 addition & 0 deletions pipeline-account/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
provider "aws" {}
8 changes: 8 additions & 0 deletions pipeline-account/s3_bucket.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "aws_s3_bucket" "artifact-bucket" {
bucket = var.artifact_bucket_name
acl = "private"

versioning {
enabled = true
}
}
33 changes: 33 additions & 0 deletions pipeline-account/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "github" {
type = "map"
}

variable "region" {
type = "string"
}

variable "target_role_arn" {
type = "string"
}

variable "target_bucket_name" {
type = "string"
}

variable "target_kms_key_arn" {
type = "string"
}

variable "artifact_bucket_name" {
type = "string"
default = "example-codepipeline-cross-account-pipeline-artifact-bucket"
}

variable "pipeline_name" {
type = "string"
default = "ExamplePipeline"
}

locals {
pipeline_arn = "arn:aws:codepipeline:${replace(var.region, "-", "")}:${data.aws_caller_identity.current.account_id}:${var.pipeline_name}"
}
17 changes: 17 additions & 0 deletions providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
provider "aws" {
alias = "target"
region = "${var.region}"

assume_role {
role_arn = "arn:aws:iam::${var.accounts["target"]}:role/${var.terraform_role}"
}
}

provider "aws" {
alias = "pipeline"
region = "${var.region}"

assume_role {
role_arn = "arn:aws:iam::${var.accounts["pipeline"]}:role/${var.terraform_role}"
}
}
1 change: 1 addition & 0 deletions target-account/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "aws_caller_identity" "current" {}
3 changes: 3 additions & 0 deletions target-account/iam_policy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_iam_policy" "target" {
policy = data.aws_iam_policy_document.target.json
}
5 changes: 5 additions & 0 deletions target-account/iam_policy_attachment.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_iam_policy_attachment" "target" {
name = "target-role-attachment"
roles = [aws_iam_role.target.name]
policy_arn = aws_iam_policy.target.arn
}
Loading

0 comments on commit 846456e

Please sign in to comment.