DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Keep Your Application Secrets Secret
  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Should an AI Agent Have Write Access to Your AWS Account?

Trending

  • How We Cut DynamoDB Ingestion Reads With an S3 Hash Pre-Filter
  • Spring Security JWT Validation Best Practices
  • Running an OpenStreetMap Tile Server for the Whole Planet on Modest Hardware
  • Building a Self-Healing Docker CI Gatekeeper With BuildKit

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Configure Cross-Account Access for CodeCommit Repositories

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.

By 
Muthukumaran Theerthan
·
Updated Oct. 22, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

You 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.

cloud admin


YAML
 




xxxxxxxxxx
1
21


 
1
AWSTemplateFormatVersion: 2010-09-09
2
Description: Creates code-commit repo for demo
3
Parameters:
4
  Project:
5
    Description: Project Name
6
    Type: String
7
Resources:
8
  CodeCommitRepository:
9
    Type: AWS::CodeCommit::Repository
10
    Properties:
11
        RepositoryName: !Join ['-', [!Ref 'Project',!Ref 'AWS::AccountId']]
12
        RepositoryDescription: This is a repository for my demo 
13
        Tags:
14
          - Key: Project
15
            Value: !Ref Project
16
Outputs:
17
  CodeCommitRepositoryArn:
18
    Description: repo Arn.
19
    Value: !GetAtt  CodeCommitRepository.Arn
20
    Export:
21
      Name: !Sub ${AWS::StackName}



crossaccount

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.

cloud admin

YAML
 




xxxxxxxxxx
1
72


 
1
AWSTemplateFormatVersion: 2010-09-09
2
Description: cross-account codecommit role to assume by developers
3
Parameters:
4
  Project:
5
    Description: Project Name
6
    Type: String
7
  CrossAccount:
8
    Description: Trusted Non-prod AWS account ID.
9
    Type: String
10
#Importfrom previous StackValue
11
  CodecommitRepoArn:
12
    Description: Code-commit Repo Arn.provide Arn (or) stack export value
13
    Type: String
14
Resources:
15
  CodeCommitAccessRole:
16
    Type: 'AWS::IAM::Role'
17
    DeletionPolicy: Retain
18
    Properties:
19
      AssumeRolePolicyDocument:
20
        Version: '2012-10-17'
21
        Statement:
22
          - Effect: Allow
23
            Principal:
24
              AWS:
25
                - !Ref 'CrossAccount'
26
            Action:
27
              - 'sts:AssumeRole'
28
      Path: /
29
#      ManagedPolicyArns:
30
#        - 'arn:aws:iam::aws:policy/AWSCodeCommitPowerUser'
31
      Policies:
32
        - PolicyName: CrossAccountExecPolicy
33
          PolicyDocument:
34
            Version: '2012-10-17'
35
            Statement:
36
              - Sid: CodeCommitAccess
37
                Effect: Allow
38
                Action:
39
                  - codecommit:BatchGet*
40
                  - codecommit:Create*
41
                  - codecommit:DeleteBranch
42
                  - codecommit:Get*
43
                  - codecommit:List*
44
                  - codecommit:Describe*
45
                  - codecommit:Put*
46
                  - codecommit:Post*
47
                  - codecommit:Merge*
48
                  - codecommit:Test*
49
                  - codecommit:Update*
50
                  - codecommit:GitPull
51
                  - codecommit:GitPush
52
                Resource:
53
#                  - !Ref 'CodecommitRepoArn'
54
                  - 'Fn::ImportValue':
55
                           !Ref 'CodecommitRepoArn'
56
              - Sid: listAccess
57
                Effect: Allow
58
                Action:
59
                  - codecommit:ListRepositories
60
                Resource: "*"
61
      RoleName: !Join ["-", [!Ref 'Project',CodeCommitAccessRole]]
62
      Tags:
63
        - Key: Project
64
          Value: !Ref Project
65
Outputs:
66
  CodeCommitAccessRoleName:
67
    Description: Code Commit Access Role Name
68
    Value: !Ref CodeCommitAccessRole
69
  CodeCommitAccessRoleArn:
70
    Description: CrossAccount Access Role Arn
71
    Value: !GetAtt  CodeCommitAccessRole.Arn



stacks

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.

stacks

YAML
 




xxxxxxxxxx
1
57


 
1
AWSTemplateFormatVersion: 2010-09-09
2
Description: This template creates developer group,user attach policy
3
Parameters:
4
  Project:
5
    Description: Project Name
6
    Type: String
7
  CrossAccountRole:
8
    Description: Cross-Account Role created in production account
9
    Type: String
10
Resources:
11
  DevManagedPolicy:
12
    Type: AWS::IAM::ManagedPolicy
13
    Properties:
14
      PolicyDocument:
15
        Version: '2012-10-17'
16
        Statement:
17
          - Sid: AssumeCrossAccountRole
18
            Effect: Allow
19
            Action:
20
              - sts:AssumeRole
21
            Resource:
22
              - !Ref 'CrossAccountRole'
23
      ManagedPolicyName: !Join ["-", [!Ref 'Project','AssumeCrossAccountRole']]
24
      Description: "Developers Assume CrossAccount Repository Role"
25
  DevGroup:
26
    Type: AWS::IAM::Group
27
    Properties:
28
      GroupName: !Join ["-", [!Ref 'Project','CrossAccountRepositoryAccess']]
29
      ManagedPolicyArns:
30
         - !Ref 'DevManagedPolicy'
31
  DevUser:
32
    Type: AWS::IAM::User
33
    Properties:
34
      UserName: !Join ["-", [!Ref 'Project','Dev']]
35
      Groups:
36
        - !Ref 'DevGroup'
37
      Tags:
38
        - Key: Project
39
          Value: !Ref Project
40
  DevAccessKey:
41
    Type: AWS::IAM::AccessKey
42
    Properties:
43
      UserName: !Ref 'DevUser'
44
      Status: Active
45
  DevAccessSecret:
46
    Type: 'AWS::SecretsManager::Secret'
47
    Properties:
48
      Name: !Join ["/", ['',!Ref 'Project','Dev','AccessKey']]
49
      Description: Dev user AccessKey secret
50
      SecretString:
51
        !Sub
52
          - '{"aws_access_key_id":"${AccessKey}","aws_secret_access_key":"${SecretAccess}"}'
53
          - AccessKey: !Ref 'DevAccessKey'
54
            SecretAccess: !GetAtt 'DevAccessKey.SecretAccessKey'
55
      Tags:
56
        - Key: Project
57
          Value: !Ref Project



4) Now Developer can use the access Key and login and switch the role access the repo.

Shell
 




xxxxxxxxxx
1


 
1
aws configure
2
### AWS Access Key ID [****************GE6U]: *******
3
### AWS Secret Access Key [****************Zm9P]: ********
4

          
5
# aws sts get-caller-identity
6
aws sts assume-role --role-arn "arn:aws:iam::*:role/*Role" --role-session-name CodeRepo
7
git clone https://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/*****



PS: Next part we will create a pipeline and assume the role.

Repository (version control) AWS

Opinions expressed by DZone contributors are their own.

Related

  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Keep Your Application Secrets Secret
  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Should an AI Agent Have Write Access to Your AWS Account?

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook