Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 2c9ec80

Browse files
committed
More progress on cross account deployments
1 parent 0e38d3f commit 2c9ec80

File tree

12 files changed

+95
-91
lines changed

12 files changed

+95
-91
lines changed

workshop/content/crossaccount/_index.en.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ title = "Cross Account Deployments"
33
date = 2019-10-02T16:10:44-07:00
44
weight = 40
55
chapter = true
6-
draft = true
7-
hidden = true
6+
draft = false
7+
hidden = false
88
pre = "<b>6. </b>"
99
+++
1010
# Cross Account Deployments
1111

12-
Cross-account deployments are useful for customers who separate their environments (Dev, Test, Prod) into different AWS accounts. In this chapter you will learn how to deploy across multiple accounts using AWS Code Pipeline.
12+
Cross-account deployments are useful for customers who separate their environments (Dev, Test, Prod) into different AWS accounts. In this chapter you will learn how to deploy a Serverless application across multiple accounts using Code Pipeline.
1313

14-
![DevWorkflow](/images/cross-account-chapter.png)
14+
![DevWorkflow](/images/chapter6/cross-account-chapter.png)

workshop/content/crossaccount/artifactstore/_index.en.md

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
+++
2+
title = "CDK prod environment"
3+
date = 2019-11-11T14:46:02-08:00
4+
weight = 35
5+
draft = false
6+
hidden = false
7+
+++
8+
9+
Before we define any resources in the stack, we need to configure the CDK project to deploy this stack in the Production account. For this, the CDK has the concept of [Environments](https://docs.aws.amazon.com/cdk/latest/guide/environments.html), which allows you to deploy Stacks in different AWS accounts from within the same CDK project.
10+
11+
Open the file `bin/pipeline.ts` and replace its content with the following snippet:
12+
13+
{{< highlight js "hl_lines=11 17" >}}
14+
#!/usr/bin/env node
15+
import 'source-map-support/register';
16+
import * as cdk from '@aws-cdk/core';
17+
import { PipelineStack } from '../lib/pipeline-stack';
18+
import { ProdIAMStack } from '../lib/prod-iam-stack';
19+
20+
const app = new cdk.App();
21+
22+
new PipelineStack(app, 'sam-app-cicd', {
23+
env: {
24+
account: '0123456789', // dev account
25+
}
26+
});
27+
28+
new ProdIAMStack(app, 'sam-app-iam-cross-account', {
29+
env: {
30+
account: '9876543210', // prod account
31+
}
32+
});
33+
{{< / highlight >}}
34+
35+
**NOTE**: Replace the account IDs corresponding to your dev and production AWS accounts.
36+
37+
{{% notice tip %}}
38+
The command `aws sts get-caller-identity` is an easy way to get your AWS Account ID. You can also check here for other forms to find it: https://docs.aws.amazon.com/IAM/latest/UserGuide/console_account-alias.html.
39+
{{% /notice%}}
40+
41+
### Configure prod credentials
42+
43+

workshop/content/crossaccount/howitworks/_index.en.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
+++
2-
title = "How does it work"
2+
title = "How it works"
33
date = 2019-11-11T14:46:02-08:00
44
weight = 20
5-
draft = true
6-
hidden = true
5+
draft = false
6+
hidden = false
77
+++
88

99
Before jumping to the implementation, we need to understand the different pieces that allow Code Pipeline to deploy across a different account. The following diagram shows a zoomed-in view of the services and roles involved in this process.
1010

11-
![CrossAccountDeploy](/images/cross-account-deploy.png)
11+
![CrossAccountDeploy](/images/chapter6/cross-account-deploy.png)
1212

1313
#### Explanation
1414

15-
The diagram above illustrates what happens when CodePipeline begins a deployment to the Production account. The first step is to assume the **IAM Pipeline Role** that exists in the Production account; This is possible because the role has a Trust Policy that allows the Development account to assume it. The second step is CodePipeline uses that role to trigger a deployment in CloudFormation by passing the **IAM Deployer Role**. This action is called [PassRole](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html) and is needed for CloudFormation to create resources on your behalf. Finally, as you learned in previous chapters, CloudFormation gets the deployment artifacts from S3 and decrypts them by using the KMS Customer Managed Key.
15+
The diagram above illustrates what happens when CodePipeline begins a deployment to the Production account. The first step is to assume the **IAM Cross Account Role** that exists in the Production account; This is possible because the role has a Trust Policy that allows the Dev account to assume it. The second step is CodePipeline uses that role to trigger a deployment in CloudFormation by passing the **IAM Deployer Role**. This action is called [PassRole](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html) and is needed for CloudFormation to create resources on your behalf. Finally, as you learned in previous chapters, CloudFormation gets the deployment artifacts from S3 and decrypts them by using the KMS Customer Managed Key.
1616

1717
#### Why encrypt the artifacts?
1818

19-
AWS CodePipeline *always* stores artifacts on S3 with encryption enabled and there is no way to disable it. The default behavior is to use the AWS Managed Key to encrypt them, but this approach doesn't work for granting access to S3 buckets across accounts. Therefore you [3] must create a KMS Customer Master Key and then give the IAM role in the Production account permissions to use it to decrypt the artifacts.
19+
AWS CodePipeline *always* stores artifacts on S3 with encryption-at-rest enabled and there is no way to disable it. The default behavior is to use the AWS Managed Key to encrypt them, but this approach doesn't work for granting access to S3 buckets across accounts. Therefore you [3] must create a KMS Customer Master Key and then give the IAM role in the Production account permissions to use it to decrypt the artifacts.
2020

2121
#### Additional reading
2222

workshop/content/crossaccount/intro/_index.en.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
title = "Introduction"
33
date = 2019-11-11T14:46:02-08:00
44
weight = 15
5-
draft = true
6-
hidden = true
5+
draft = false
6+
hidden = false
77
+++
88

99
### Environment separation
1010

11-
Separating environments (Dev, Test, Prod) into different AWS accounts is a very common practice among AWS customers. And the main motivation being the ability to give developers full administrative access to the Dev environment, so that they can innovate and iterate quickly, but give them _limited_ access to higher environments, like Production. There are other motivations as well, like managing [AWS Service Quotas](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html) separately and having billing details broken down per environment.
11+
Separating environments into different AWS accounts its a common practice among AWS customers. The most basic form of separation is to have 2 AWS accounts, one for non-production environments and one dedicated to Production. This gives developers the ability to have full admin permissions in the Dev environment, to iterate and experiment quickly, and have _limited_ permissions in the Production account.
1212

13-
The following diagram illustrates a simple account setup that many customers start with. And we will also be using it throughout this chapter. But the concepts that you will learn here can be applied to any form of multi-account setup.
13+
The following diagram illustrates a simple setup of 2 accounts, one for Dev and one for Prod. This is the setup we will be using as reference throughout this chapter, but the concepts learned can be applied to any form of multi-account setup.
1414

15-
![EnvironmentSeparation](/images/environment-separation.png)
15+
![EnvironmentSeparation](/images/chapter6/environment-separation.png)
1616

17-
As you can tell from the diagram, the Dev account hosts the Code Pipeline, Artifacts Bucket and the Code Repository. The CodePipeline then deploys across the Production account using an [assumed role](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html).
17+
As you can see from the diagram, the Dev account hosts the Code Pipeline, S3 bucket for artifacts and the code repository. The pipeline then deploys across the Production account using IAM [assumed roles](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html).
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
+++
2+
title = "Prod account setup"
3+
date = 2019-11-11T14:46:02-08:00
4+
weight = 30
5+
draft = false
6+
hidden = false
7+
+++
8+
9+
Let's get started creating the necessary IAM roles in the Production account to grant cross account access. We will do this as a separate stack within the same CDK project created in Chapter 4.
10+
11+
Run the following command to create a new file named _prod-iam-stack.ts_ inside the _lib_ drectory.
12+
13+
```
14+
cd ~/environment/sam-app/pipeline
15+
touch lib/prod-iam-stack.ts
16+
```
17+
18+
Paste to following content inside the file:
19+
20+
```js
21+
import * as cdk from '@aws-cdk/core';
22+
import iam = require('@aws-cdk/aws-iam');
23+
24+
export class ProdIAMStack extends cdk.Stack {
25+
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
26+
super(scope, id, props);
27+
28+
// Resource definition goes here
29+
}
30+
}
31+
```
32+
33+
It should look like this:
34+
35+
![NewIamProdStack](/images/chapter6/new-file-prod-stack.png)
36+
23.9 KB
Loading
50.2 KB
Loading

workshop/static/images/environment-separation.png renamed to workshop/static/images/chapter6/environment-separation.png

File renamed without changes.
132 KB
Loading

0 commit comments

Comments
 (0)