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

Commit b8e43c8

Browse files
committed
cross account roles
1 parent 2c9ec80 commit b8e43c8

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ new ProdIAMStack(app, 'sam-app-iam-cross-account', {
3838
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.
3939
{{% /notice%}}
4040

41-
### Configure prod credentials
41+
### Test deployment
42+
43+
```
44+
cd ~/environment/sam-app/pipeline
45+
npm run build
46+
cdk deploy sam-app-iam-cross-account --profile prod
47+
```
4248

4349

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
+++
2+
title = "Cross account roles"
3+
date = 2019-11-11T14:46:02-08:00
4+
weight = 40
5+
draft = false
6+
hidden = false
7+
+++
8+
9+
Now that we have verified that we can deploy an empty stack to production, lets create the actual cross account roles in the `lib/prod-iam-stack.ts` file we created earlier. Add the following content to the file:
10+
11+
{{< highlight js "hl_lines=26" >}}
12+
import * as cdk from '@aws-cdk/core';
13+
import iam = require('@aws-cdk/aws-iam');
14+
15+
export class ProdIAMStack extends cdk.Stack {
16+
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
17+
super(scope, id, props);
18+
19+
/**
20+
* IAM Deployer Role. Will be used to deploy
21+
* the serverless app. Needs admin permissions.
22+
*/
23+
const deployerRole = new iam.Role(this, 'DeployerRole', {
24+
assumedBy: new iam.ServicePrincipal('cloudformation.amazonaws.com')
25+
});
26+
27+
deployerRole.addManagedPolicy(
28+
iam.ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess")
29+
);
30+
31+
/**
32+
* IAM Cross Account Access Role. Will be assumed by
33+
* the CodePipeline in the dev account, needs permissions
34+
* to pass the Deployer role defined above.
35+
*/
36+
const crossAccountRole = new iam.Role(this, 'CrossAccountRole', {
37+
assumedBy: new iam.AccountPrincipal("REPLACE ME"), // replace with dev account id
38+
});
39+
40+
// Needs CloudFormation permissions
41+
crossAccountRole.addManagedPolicy(
42+
iam.ManagedPolicy.fromAwsManagedPolicyName("AWSCloudFormationFullAccess")
43+
);
44+
45+
// Needs permissions to pass the deployer role defined above
46+
crossAccountRole.addToPolicy(new iam.PolicyStatement({
47+
actions: ['iam:PassRole'],
48+
resources: [deployerRole.roleArn]
49+
}));
50+
51+
// Needs S3 permissions to access artifacts in the DEV account
52+
crossAccountRole.addToPolicy(new iam.PolicyStatement({
53+
actions: ['s3:GetObject'],
54+
resources: ['*']
55+
}));
56+
57+
// Needs KMS permissions to decrypt objects in the DEV account
58+
crossAccountRole.addToPolicy(new iam.PolicyStatement({
59+
actions: ['kms:Decrypt'],
60+
resources: ['*']
61+
}));
62+
63+
/**
64+
* Outputs
65+
*/
66+
new cdk.CfnOutput(this, 'ProdCrossAccountRoleArn', {
67+
value: crossAccountRole.roleArn,
68+
});
69+
70+
new cdk.CfnOutput(this, 'DeployerRoleArn', {
71+
value: deployerRole.roleArn,
72+
});
73+
74+
}
75+
}
76+
{{< / highlight >}}
77+
78+
**NOTE:** Replace the highlighted line with the corresponding DEV account id.
79+
80+
### Deploy stack
81+
82+
```
83+
npm run build
84+
cdk deploy sam-app-iam-cross-account --profile prod
85+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
+++
2+
title = "Create Prod credentials"
3+
date = 2019-11-11T14:46:02-08:00
4+
weight = 22
5+
draft = false
6+
hidden = false
7+
+++
8+
9+
In this chapter you will be deploying to both, the DEV account and the PROD account from your Cloud9 environment, for that to work you need to have valid IAM credentials and profiles configured in your ~/.aws/credentials file.
10+
11+
**TODO: ADD STEPS TO CONFIGURE CREDENTIALS**

0 commit comments

Comments
 (0)