|
| 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 | +``` |
0 commit comments