Configure Cross-Account Access for CodeCommit Repositories
You might want to use a single Codecommit repository for a project and uses them across different AWS account Ex: prod, and non-prod.
Join the DZone community and get the full member experience.
Join For FreeYou might want to use a single Codecommit repository for a project and uses them across different AWS account Ex: prod, and non-prod.
What to Do?
For this tutorial, let's assume we have 2 AWS account one for Production and another for non-production.CodeCommit repository is hosted on a production account.
The developer is going to create a simple cloudformation (CFN) template which will provision a DynamoDB table employee(id, name, job) and create a secret and stores it in AWS Secrets Manager.
The CFN template will be saved in codecommit repo under the default master branch. Using AWS Codepipeline CFN will be deployed across multiple AWS accounts.
Steps to Follow
1) Cloud Admin will create a code-commit repo in the production AWS account.

xxxxxxxxxx
AWSTemplateFormatVersion2010-09-09
DescriptionCreates code-commit repo for demo
Parameters
Project
DescriptionProject Name
TypeString
Resources
CodeCommitRepository
TypeAWSCodeCommitRepository
Properties
RepositoryName!Join '-' !Ref 'Project'!Ref 'AWS::AccountId'
RepositoryDescriptionThis is a repository for my demo
Tags
KeyProject
Value!Ref Project
Outputs
CodeCommitRepositoryArn
Descriptionrepo Arn.
Value!GetAtt CodeCommitRepository.Arn
Export
Name!Sub $AWSStackName

2) Cloud Admin in the production AWS account, creates a role that can be assumed by developers from a non-prod AWS account. Also creates a policy that grants access to the repository and attaches the policy to the role.

xxxxxxxxxx
AWSTemplateFormatVersion2010-09-09
Descriptioncross-account codecommit role to assume by developers
Parameters
Project
DescriptionProject Name
TypeString
CrossAccount
DescriptionTrusted Non-prod AWS account ID.
TypeString
#Importfrom previous StackValue
CodecommitRepoArn
DescriptionCode-commit Repo Arn.provide Arn (or) stack export value
TypeString
Resources
CodeCommitAccessRole
Type'AWS::IAM::Role'
DeletionPolicyRetain
Properties
AssumeRolePolicyDocument
Version'2012-10-17'
Statement
EffectAllow
Principal
AWS
!Ref 'CrossAccount'
Action
'sts:AssumeRole'
Path/
# ManagedPolicyArns:
# - 'arn:aws:iam::aws:policy/AWSCodeCommitPowerUser'
Policies
PolicyNameCrossAccountExecPolicy
PolicyDocument
Version'2012-10-17'
Statement
SidCodeCommitAccess
EffectAllow
Action
codecommit:BatchGet*
codecommit:Create*
codecommit:DeleteBranch
codecommit:Get*
codecommit:List*
codecommit:Describe*
codecommit:Put*
codecommit:Post*
codecommit:Merge*
codecommit:Test*
codecommit:Update*
codecommit:GitPull
codecommit:GitPush
Resource
# - !Ref 'CodecommitRepoArn'
'Fn::ImportValue':
!Ref 'CodecommitRepoArn'
SidlistAccess
EffectAllow
Action
codecommit:ListRepositories
Resource"*"
RoleName!Join "-" !Ref 'Project'CodeCommitAccessRole
Tags
KeyProject
Value!Ref Project
Outputs
CodeCommitAccessRoleName
DescriptionCode Commit Access Role Name
Value!Ref CodeCommitAccessRole
CodeCommitAccessRoleArn
DescriptionCrossAccount Access Role Arn
Value!GetAtt CodeCommitAccessRole.Arn

3) Cloud Admin in the non-production AWS account, creates a group and attach the policy to assume the production cross-account codecommit Role. New User is created and attached to the group. Access Key and secret for the user is generated and stored in the secret manager.

xxxxxxxxxx
AWSTemplateFormatVersion2010-09-09
DescriptionThis template creates developer group,user attach policy
Parameters
Project
DescriptionProject Name
TypeString
CrossAccountRole
DescriptionCross-Account Role created in production account
TypeString
Resources
DevManagedPolicy
TypeAWSIAMManagedPolicy
Properties
PolicyDocument
Version'2012-10-17'
Statement
SidAssumeCrossAccountRole
EffectAllow
Action
sts:AssumeRole
Resource
!Ref 'CrossAccountRole'
ManagedPolicyName!Join "-" !Ref 'Project''AssumeCrossAccountRole'
Description"Developers Assume CrossAccount Repository Role"
DevGroup
TypeAWSIAMGroup
Properties
GroupName!Join "-" !Ref 'Project''CrossAccountRepositoryAccess'
ManagedPolicyArns
!Ref 'DevManagedPolicy'
DevUser
TypeAWSIAMUser
Properties
UserName!Join "-" !Ref 'Project''Dev'
Groups
!Ref 'DevGroup'
Tags
KeyProject
Value!Ref Project
DevAccessKey
TypeAWSIAMAccessKey
Properties
UserName!Ref 'DevUser'
StatusActive
DevAccessSecret
Type'AWS::SecretsManager::Secret'
Properties
Name!Join "/" ''!Ref 'Project''Dev''AccessKey'
DescriptionDev user AccessKey secret
SecretString
!Sub
'{"aws_access_key_id":"${AccessKey}","aws_secret_access_key":"${SecretAccess}"}'
AccessKey!Ref 'DevAccessKey'
SecretAccess!GetAtt 'DevAccessKey.SecretAccessKey'
Tags
KeyProject
Value!Ref Project
4) Now Developer can use the access Key and login and switch the role access the repo.
xxxxxxxxxx
aws configure
### AWS Access Key ID [****************GE6U]: *******
### AWS Secret Access Key [****************Zm9P]: ********
# aws sts get-caller-identity
aws sts assume-role --role-arn "arn:aws:iam::*:role/*Role" --role-session-name CodeRepo
git clone https://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/*****
PS: Next part we will create a pipeline and assume the role.
Opinions expressed by DZone contributors are their own.
Comments