Slide 1

Slide 1 text

Con f igure cross-account deployment using CDK Takuya Hashimoto / @hassaku_63 CDK Day 2023 29th September 2023

Slide 2

Slide 2 text

Speaker Developer, IT system admin X(Twitter): @hassaku_63 GitHub: hassaku63 SpeakerDeck: hassaku63 Takuya Hashimoto (Serverworks Co., Ltd.) Serverworks is; System Integrator & AWS Premier Tier Service Partner (since 2014) 1st Japanese Company to acquire MSP Competency in AWS Partner Network (APN)

Slide 3

Slide 3 text

Contents 1. Overview of the “CDK Security and Safety Dev Guide” 2. What “cdk bootstrap” command actually doing 3. Con f igure cross-account deployment for CDK project This is my f irst time outputting in English. I'm looking forward to discussing with all of you today, but I apologize if my English is hard to understand.

Slide 4

Slide 4 text

Goal The architecture we are assuming for this session Actor (pipeline account) Target account(s) Trigger on push cdk deploy

Slide 5

Slide 5 text

Overview of the “CDK Security and Safety Dev Guide”

Slide 6

Slide 6 text

(QUESTION) What should CDK beginner to learn after going through the tutorial?

Slide 7

Slide 7 text

Fundamental documents about CDK https://github.com/aws/aws-cdk/wiki/Security-And-Safety-Dev-Guide 1. CDK Concepts 2. Security And Safety Dev Guide https://docs.aws.amazon.com/cdk/v2/guide/core_concepts.html

Slide 8

Slide 8 text

CDK deployments and IAM permission #Controlling the permissions used by CDK deployments [2] DefaultStackSynthesizer CDK’s default approach Fewer permissions are required for the principal who is initiating CDK deployment action than [1] [1] CliCredentialsStackSynthesizer Simple deployment pattern Do not use AssumeRole for deployment

Slide 9

Slide 9 text

Permissions and Roles in DefaultStackSynthesizer

Slide 10

Slide 10 text

Permissions and Roles in DefaultStackSynthesizer The principal who starts CDK Deployment action (It invokes cfn ExecuteChangeSet or Deploy API internally)

Slide 11

Slide 11 text

Permissions and Roles in DefaultStackSynthesizer IAM Roles and Permissions actually used during CDK deployment

Slide 12

Slide 12 text

Permissions and Roles in DefaultStackSynthesizer “Assets” publishing (Container images) “Assets” publishing (static f iles like such as Lambda function’s packages)

Slide 13

Slide 13 text

Permissions and Roles in DefaultStackSynthesizer Executes cfn deployment

Slide 14

Slide 14 text

Permissions and Roles in DefaultStackSynthesizer Executes cfn deployment IAM Role for Invoke cfn deploy API IAM Role for used when constructs resources

Slide 15

Slide 15 text

Permissions and Roles in DefaultStackSynthesizer AssumeRole permission are required for the principal (do not need other)

Slide 16

Slide 16 text

Permissions and Roles in DefaultStackSynthesizer Account for starting deployment action Deployment target account(s) (1) CDK’s default approach makes it easier to migrate to cross-account architecture because deployment are based on AssumeRole & PassRole (2) These resources (in orange box) are constructed ”cdk bootstrap” (3) Need to con f igure IAM Roles to “trust” the principals in another account if con f igure cross-account deployment (by execute “cdk bootstrap” command)

Slide 17

Slide 17 text

What “cdk bootstrap” command actually doing

Slide 18

Slide 18 text

CDK bootstrap (revision) Deploy a cfn stack which is called “bootstrap stack” CDK Developer Guide - Concepts; Bootstraping https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html

Slide 19

Slide 19 text

CDK bootstrap $ cdk bootstrap —show-template > bootstrap-template.yml De f ined resources are; (only covering those relevant to my talk theme) Allow AssumeRole to AWS Account principal if “TrustedAccouts” given IAM Role resource de f ined in bootstrap template 1. S3 Bucket for storing f ile assets 2. ECR Repository for upload container assets 3. IAM Role for Upload f ile assets 4. IAM Role for push container assets 5. IAM Role for lookup resources

Slide 20

Slide 20 text

Con f igure cross-account deployment for CDK project

Slide 21

Slide 21 text

Con f iguration for cross-account deployment Overview hassaku63/cdk-cross-account-deployment-example 1. bootstrap on Pipeline account 2. bootstrap on Target account 
 (trust “pipeline account” principal) 3. Deploy pipeline stack 4. Deploy to target (by git push) Pipeline account Target account(s)

Slide 22

Slide 22 text

Con f iguration for cross-account deployment Overview hassaku63/cdk-cross-account-deployment-example Pipeline account Target account(s) 1. bootstrap on Pipeline account 2. bootstrap on Target account 
 (trust “pipeline account” principal) 3. Deploy pipeline stack 4. Deploy to target (by git push)

Slide 23

Slide 23 text

Con f iguration for cross-account deployment Bootstrap on target account with “trust” option Execute on the all target accounts $ npx cdk bootstrap \ —trust “” \ —cloudformation-execution-policies \ ‘arn:aws:iam::aws:policy/AdministratorAccess’ Pipeline account Target account(s)

Slide 24

Slide 24 text

Con f iguration for cross-account deployment Con f igure service role for CodeBuild Allow AssumeRole action to the Roles that are constructed by bootstrap Pipeline account Target account(s)

Slide 25

Slide 25 text

Conclusion

Slide 26

Slide 26 text

Conclusion Learn about … 1. What resources are constructed by “cdk bootstrap” command 2. Use “trust” option with bootstrap command to con f igure cross-account deployment 3. Allow the actor that starts deployment (such as CodeBuild Project) to execute 
 the AssumeRole action to the Roles that are constructed by bootstrap

Slide 27

Slide 27 text

Slide Published on https://speakerdeck.com/hassaku63