AWS CodePipeline Example which deploys to multiple AWS Accounts - Part1
Tags: aws, codepipeline, cloudformation
Published: 2020-07-01 10:11:18

---
title: AWS CodePipeline Example which deploys to multiple AWS Accounts - Part1
published: true
date: 2020-05-04 07:30:10 UTC
tags: aws, codepipeline, cloudformation
canonical_url: https://kbild.ch/blog/2020-5-4-cf_multiple_accounts_regions/
---

At [WebGate](https://webgate.biz), we’re using AWS CodePipeline heavily for CI/CD of our serverless apps and we usually do 3-tier deployments (Dev, Test, Prod).

Therefore we were looking for an example which describes how you have to build such a solution. Unfortunately we didn’t found a source which had a full blown solution matching our needs. Luckily we found some examples which gave us some clues on how to build such a Pipeline.

Especially following two sites helped us to get started:

[- Building a Secure Cross-Account Continuous Delivery Pipeline](https://aws.amazon.com/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/)  
[- How do I use CodePipeline to deploy an AWS CloudFormation stack in a different account?](https://aws.amazon.com/premiumsupport/knowledge-center/codepipeline-deploy-cloudformation/)

In this post I will show you an example which you can use for your own cross-account AWS CodePipelines.

We will have:

- 

"Central Account" → App Repos, Pipelines…​

- 

"Dev Account" → Development Account for App

- 

"Test Account" → Testing Account for App

- 

"Prod Account" → Production Account for App

* * *

## How to Deploy

You will find the source code of this example in [this Github repo](https://github.com/kbild/AWS_Cloudformation_Examples/tree/master/multipleAccountPipeline/), let’s first deploy the prerequisites and later the sample repo.

* * *

### 1. Prerequisites

There are 3 AWS CloudFormation templates which you will need to deploy this solution, let’s first have a look at them:

#### cloudformation/01central-prereqs.yaml

This [template](https://github.com/kbild/AWS_Cloudformation_Examples/blob/master/multipleAccountPipeline/cloudformation/01central-prereqs.yaml) will deploy all needed resources in the "Central Account":

[![01central-prereqs.yaml](https://kbild.ch/202005/arch1.png)](https://kbild.ch/202005/arch1.png)

* * *

- 

S3 Artifact Bucket

- 

KMS Key for encryption

- 

IAM Roles/Policies

- 

CodeCommit Repo for the App

- 

CodeBuild Project for App

#### cloudformation/02prereqs-accounts.yaml

This [template](https://github.com/kbild/AWS_Cloudformation_Examples/blob/master/multipleAccountPipeline/cloudformation/02prereqs-accounts.yaml) will deploy all needed resources in the "Dev Account", "Test Account" and "Prod Account":

| 

 ![01central-prereqs.yaml](https://kbild.ch/202005/arch2.png)

 | 

- 

IAM Roles/Policies

 |

#### cloudformation/03central-pipeline.yaml

This [template](https://github.com/kbild/AWS_Cloudformation_Examples/blob/master/multipleAccountPipeline/cloudformation/03central-pipeline.yaml) will deploy the actual Code Pipeline in the "Central Account".  
For simplicity I’m only deploying a simple "Hello World" Lambda function and an API Gateway and I’m only using the Build and Deploy Stages in the Pipeline:

[![AWS CodePipeline Example](https://kbild.ch/202005/arch3.png)](https://kbild.ch/202005/arch3.png)

* * *

* * *

### 2. How to Deploy the Prerequisites

#### Central Account

First logon to your **Central Account** and open up CloudFormation in the Region of choice.  
Now create a new stack with the template **01central-prereqs.yaml** and define:

[![01central-prereqs.yaml](https://kbild.ch/202005/cf1.png)](https://kbild.ch/202005/cf1.png)

* * *

- 

Stack Name i.e. _kbild-serverless-prereqs_

- 

AWS Account Numbers for the Dev, Test and Prod Accounts

- 

PreReqsOnAccounts, should stay "false"

- 

Project name i.e. _serverless_

Finish the stack deployment, it will take some minutes. When finished, open up the Outputs tab of the stack and take a note of the "ArtifactBucket" and "CMK" Key values.

[![01central-prereqs.yaml](https://kbild.ch/202005/cf5.png)](https://kbild.ch/202005/cf5.png)

* * *

#### Dev Account

Now logon to your **Dev Account** and open up CloudFormation in the same Region as used for the "Central Account"  
Here we create a new stack and we will use the template **02prereqs-accounts.yaml** :

[![01central-prereqs.yaml](https://kbild.ch/202005/cf2.png)](https://kbild.ch/202005/cf2.png)

* * *

- 

Stack Name i.e. _kbild-serverless-prereqs_

- 

CMKARN → Fill in the value of the "CMK" Key noted before

- 

CentralAccount → Fill in the account number of the "Central Account"

- 

Project name i.e. _serverless_

- 

S3Bucket → Fill in the value of the "ArtifactBucket" Key

Wait for the stack deployment to be finished.

#### Test Account / Prod Account

Now logon to your **Test Account** / **Prod Account** and repeat the steps for **02prereqs-accounts.yaml**

#### Central Account

Now you have to go back to your **Central Account**.

> Please ensure that the prerequisites are already deployed to the "Dev/Test Prod Account",otherwise the following update will fail!

Do an update on the Prereqs Stack which you created some minutes ago. Choose "Use current template" and change the value of parameter **PreReqsOnAccounts** from **false** to **true** and update the stack:

![01central-prereqs.yaml](https://kbild.ch/202005/cf4.png)

This will update the S3 Artifact bucket and KMS Policies and will add access for to the "Dev/Test Prod Accounts".

* * *

### 3. Deploy Pipeline and App

#### Pipeline

Again in the **Central Account** create a CF stack with the **03central-pipeline.yaml** template:

[![03central-pipeline.yaml](https://kbild.ch/202005/cf3.png)](https://kbild.ch/202005/cf3.png)

* * *

- 

Stack Name i.e. _kbild-serverless-pipeline_

- 

Project name i.e. _serverless_

- 

RepoBranch → The Repo branch to which the Pipeline Webhook will listen too

Again wait for the stack deployment to be finished.

Before we run our Code Pipeline for the first time, we have to add our "Hello World" app to the freshly created CodeCommit Repo.

#### App Deployment

First clone the newly created CodeCommit Repo locally to your machine.  
(If you have never used git with CodeCommit, go to the repo and click on "Clone URL" at the top → "Connection steps").  
I will use SSH:

```
git clone ssh://git-codecommit.eu-central-1.amazonaws.com/v1/repos/serverless-ProjectRepo
```

Now add the buildspec.yml and sam-app folder from the the [Github repo](https://github.com/kbild/AWS_Cloudformation_Examples/) to your local clone:

[![03central-pipeline.yaml](https://kbild.ch/202005/screen1.png)](https://kbild.ch/202005/screen1.png)

* * *

Now commit and push the new files to the CodeCommit Repo:

```
git add .
git commit -a
git push
```

This push should trigger the Code Pipeline, so go back to your AWS console of the "Central Account" and open up CodePipeline and the new serverless-Pipeline:

[![Pipeline](https://kbild.ch/202005/pipe1.png)](https://kbild.ch/202005/pipe1.png)

As you can see the pipeline was just triggered and you can follow how the Pipeline goes through all the stages:

- 

Build\_Templates

- 

Create\_Change\_Sets\_and\_Deploy\_to\_Dev

- 

Create\_Change\_Sets\_and\_Deploy\_to\_Test

- 

Create\_Change\_Sets\_and\_Deploy\_to\_Prod

If all stages have finished you can logon to your "Dev Account" and go to:

_CloudFormation → cicd-codepipeline-Dev Stack → Outputs_  
  
Click on the value for the Key "HelloWorldApi", this will open the API Gateway Endpoint URL and will show you the "Hello World" example app.

If everything worked as expected you should see:

[![HelloWorldApp](https://kbild.ch/202005/screen3.png)](https://kbild.ch/202005/screen3.png)

* * *

Now go to your Test/Prod Account and open the according API Gateway Endpoint URL as well, you should see environment specific "Hello World" pages:

[![HelloWorldApp](https://kbild.ch/202005/screen4.png)](https://kbild.ch/202005/screen4.png)

* * *

I hope that this example helps you on your future CodePipeline journey!  
  
In [Part 2](https://kbild.ch/blog/2020-5-8-cf_multiple_accounts_regions_part2/) of this post I will get into more details how the CloudFormation templates work and how you may customize them.