AWS CodeCommit sample for AWS Cloud9
This sample enables you to set up an AWS Cloud9 development environment to interact with a remote code repository in CodeCommit. CodeCommit is a source code control service that enables you to privately store and manage Git repositories in the AWS Cloud. For more information about CodeCommit, see the AWS CodeCommit User Guide.
Creating this sample might result in charges to your AWS account. These include possible
charges for services such as Amazon EC2 and CodeCommit. For more information, see Amazon EC2 Pricing
Prerequisites
Before you use this sample, make sure your setup meets the following requirements:
-
You must have an existing AWS Cloud9 EC2 development environment. This sample assumes you already have an EC2 environment that is connected to an Amazon EC2 instance running Amazon Linux or Ubuntu Server. If you have a different type of environment or operating system, you might need to adapt this sample's instructions to set up related tools. See Creating an environment in AWS Cloud9 for details.
-
You have the AWS Cloud9 IDE for the existing environment already open. When you open an environment, AWS Cloud9 opens the IDE for that environment in your web browser. See Opening an environment in AWS Cloud9 for details.
Step 1: Set up your IAM group with required access permissions
If your AWS credentials are associated with an IAM administrator user in your AWS account, and you want to use that user to work with CodeCommit, skip ahead to Step 2: Create a Repository in AWS CodeCommit.
You can complete this step using the AWS Management Console or the AWS Command Line Interface (AWS CLI).
Set up your IAM group with required access permissions using the console
-
Sign in to the AWS Management Console, if you are not already signed in.
For this step, we recommend you sign in using credentials for an IAM administrator user in your AWS account. If you cannot do this, check with your AWS account administrator.
-
Open the IAM console. To do this, in the console's navigation bar, choose Services. Then choose IAM.
-
Choose Groups.
-
Choose the group's name.
-
On the Permissions tab, for Managed Policies, choose Attach Policy.
-
In the list of policy names, select one of the following boxes:
-
Select AWSCodeCommitPowerUser for access to all of the functionality of CodeCommit and repository-related resources, except it does not allow deletion of CodeCommit repositories or create or delete repository-related resources in other AWS services, such as Amazon CloudWatch Events.
-
Select AWSCodeCommitFullAccess for full control over CodeCommit repositories and related resources in the AWS account, including the ability to delete repositories.
(If you don't see either of these policy names in the list, type the policy name in the Filter box to display it.)
-
-
Choose Attach Policy.
To see the list of access permissions that these AWS managed policies give to a group, see AWS Managed (Predefined) Policies for AWS CodeCommit in the AWS CodeCommit User Guide.
Skip ahead to Step 2: Create a Repository in AWS CodeCommit.
Set up your IAM group with required access permissions using the AWS CLI
Run the IAM attach-group-policy
command, specifying the group's name and
the Amazon Resource Name (ARN) of the AWS managed policy that describes the required
access permissions, for example:
aws iam attach-group-policy --group-name MyGroup --policy-arn POLICY_ARN
In the preceding command, replace MyGroup
with the name of the group.
Replace POLICY_ARN
with the ARN of the AWS managed policy, as
follows:
-
arn:aws:iam::aws:policy/AWSCodeCommitPowerUser
for access to all of the functionality of CodeCommit and repository-related resources, except it does not allow deletion of CodeCommit repositories or create or delete repository-related resources in other AWS services, such as Amazon CloudWatch Events. -
arn:aws:iam::aws:policy/AWSCodeCommitFullAccess
for full control over CodeCommit repositories and related resources in the AWS account, including the ability to delete repositories.
To see the list of access permissions that these AWS managed policies give to a group, see AWS Managed (Predefined) Policies for AWS CodeCommit in the AWS CodeCommit User Guide.
Step 2: Create a repository in CodeCommit
In this step, you create a remote code repository in CodeCommit by using the CodeCommit console.
If you already have a CodeCommit repository, skip ahead to Step 3: Connect Your Environment to the Remote Repository.
You can complete this step using the AWS Management Console or the AWS Command Line Interface (AWS CLI).
Create a repository in CodeCommit using the console
-
If you are signed in to the AWS Management Console as an IAM administrator user from the previous step, and you do not want to use the IAM administrator user to create the repository, sign out of the AWS Management Console.
-
Open the CodeCommit console, at https://console.aws.amazon.com/codecommit
. -
In the console's navigation bar, use the region selector to choose the AWS Region you want to create the repository in (for example, US East (Ohio)).
-
If a welcome page is displayed, choose Get started. Otherwise, choose Create repository.
-
On the Create repository page, for Repository name, type a name for your new repository, for example
MyDemoCloud9Repo
. If you choose a different name, substitute it throughout this sample. -
(Optional) For Description, type something about the repository, for example
This is a demonstration repository for the AWS Cloud9 sample.
-
Choose Create repository. A Connect to your repository pane is displayed. Choose Close, as you will connect to your repository in a different way later in this topic.
Skip ahead to Step 3: Connect Your Environment to the Remote Repository.
Create a repository in CodeCommit using the AWS CLI
Run the AWS CodeCommit create-repository
command, specifying the
repository's name, an optional description, and the AWS Region to create the repository
in, for example:
aws codecommit create-repository --repository-name MyDemoCloud9Repo --repository-description "This is a demonstration repository for the AWS Cloud9 sample." --region us-east-2
In the preceding command, replace us-east-2
with the ID of the AWS
Region to create the repository in. For a list of supported regions, see AWS CodeCommit in the Amazon Web Services General Reference.
If you choose to use a different repository name, substitute it throughout this sample.
Step 3: Connect your environment to the remote repository
In this step, you use the AWS Cloud9 IDE to connect to the CodeCommit repository you created or identified in the previous step.
If you prefer working with Git through a visual interface, you can clone the remote repository and then add files using the Git panel feature that's available in the IDE.
Complete one of the following sets of procedures, depending on the type of AWS Cloud9 development environment you have.
Environment type | Follow these procedures |
---|---|
EC2 environment |
|
SSH environment |
|
Step 4: Clone the remote repository into your environment
In this step, you use the AWS Cloud9 IDE to clone the remote repository in CodeCommit into your environment.
To clone the repository, run the
git clone
command, supplying the repository's clone URL, shown here as
CLONE_URL
.
git clone CLONE_URL
For an EC2 environment, you supply an HTTPS clone URL that starts with https://
.
For an SSH environment, you supply an SSH clone URL that starts with ssh://
.
To get the repository's full clone URL, see Use the AWS CodeCommit Console to View Repository Details in the AWS CodeCommit User Guide.
If your repository doesn't have any files in it, a warning message is displayed, such as
You appear to have cloned an empty repository.
This is expected behavior,
which you will address later.
Step 5: Add files to the repository
In this step, you create three simple files in the cloned repository in your AWS Cloud9 environment. Then you add the files to the Git staging area in your cloned repository, commit the staged files, and push the commit to your remote repository in CodeCommit.
If the cloned repository already has files in it, you're done and can skip the rest of this sample.
To add files to the repository
-
Create a new file. On the menu bar, choose File, New File.
-
Type the following content into the file, and then choose File, Save to save the file as
bird.txt
in theMyDemoCloud9Repo
directory in your AWS Cloud9 environment.bird.txt -------- Birds are a group of endothermic vertebrates, characterized by feathers, toothless beaked jaws, the laying of hard-shelled eggs, a high metabolic rate, a four-chambered heart, and a lightweight but strong skeleton.
Note To confirm you are saving this file in the correct directory, in the Save As dialog box, choose the
MyDemoCloud9Repo
folder, and be sure Folder displays/MyDemoCloud9Repo
. -
Create two more files, named
insect.txt
andreptile.txt
, with the following content, and saving them in the sameMyDemoCloud9Repo
directory.insect.txt ---------- Insects are a class of invertebrates within the arthropod phylum that have a chitinous exoskeleton, a three-part body (head, thorax, and abdomen), three pairs of jointed legs, compound eyes, and one pair of antennae.
reptile.txt ----------- Reptiles are tetrapod (four-limbed vertebrate) animals in the class Reptilia, comprising today's turtles, crocodilians, snakes, amphisbaenians, lizards, tuatara, and their extinct relatives.
-
In the terminal, run the
cd
command to switch to theMyDemoCloud9Repo
directory.cd MyDemoCloud9Repo
-
Confirm the files were successfully saved in the
MyDemoCloud9Repo
directory by running thegit status
command. All three files will be listed as untracked files.Untracked files: (use "git add <file>..." to include in what will be committed) bird.txt insect.txt reptile.txt
-
Add the files to the Git staging area by running the
git add
command.git add --all
-
Confirm the files were successfully added to the Git staging area by running the
git status
command again. All three files are now listed as changes to commit.Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: bird.txt new file: insect.txt new file: reptile.txt
-
Commit the staged files by running the
git commit
command.git commit -m "Added information about birds, insects, and reptiles."
-
Push the commit to your remote repository in CodeCommit by running the
git push
command.git push -u origin master
-
Confirm whether the files were successfully pushed. Open the CodeCommit console, if it isn't already open, at https://console.aws.amazon.com/codecommit
. -
In the top navigation bar, near the right edge, choose the AWS Region where you created the repository (for example, US East (Ohio)).
-
On the Dashboard page, choose MyDemoCloud9Repo. The three files are displayed.
To continue experimenting with your CodeCommit repository, see Browse the Contents of Your Repository in the AWS CodeCommit User Guide.
If you're new to Git and you don't want to mess up your CodeCommit repository, experiment
with a sample Git repository on the Try Git
Step 6: Clean up
To prevent ongoing charges to your AWS account after you're done using this sample, you should delete the CodeCommit repository. For instructions, see Delete an AWS CodeCommit Repository in the AWS CodeCommit User Guide.
You should also delete the environment. For instructions, see Deleting an Environment.