8

I am using AWS CodeCommit which seems to be like a stripped down version of git.

If all these EC2 instances have the same role tag how do I accomplish this?

I don't want to do anything fancy, all I want is to be specify a tag, click a button and all those EC2 instances with that tag pull for CodeCommit. I want to do this from my local machine.

I know I need to put the SSH keys to access CodeCommit in each of my EC2 servers and install git on each of them (I will bake this into an AMI). I'm just not sure how to "trigger" each of the EC2 machines to do a git pull? Is there an AWS command?

I am not a dev ops guy and only know basic linux and php.

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
Hard worker
  • 3,916
  • 5
  • 44
  • 73

2 Answers2

13

For EC2 instances that are launched with an IAM role, you don't even have to bake in SSH keys. Git can get CodeCommit credentials from the EC2 instance metadata. Bake a Linux-based AMI with the latest AWS CLI package and the following lines in ~/.gitconfig:

[credential]
    helper = !aws --profile default codecommit credential-helper $@
    UseHttpPath = true

Launch the instance with an IAM role attached, and then you can clone your CodeCommit repo without any more setup.

You might want to look into Capistrano and Capify-EC2 if you want to run git commands across a fleet of EC2 instances based on tags.

Updated: If you're open to using AWS OpsWorks to deploy from CodeCommit, there's a recent blog article about how to do that. You can also use OpsWorks to run arbitrary commands across instances with Capistrano.

Clare Liguori
  • 1,564
  • 11
  • 10
  • I wish I had this answer back when I asked the question. In the end I used Fabric to run git clone commands across my EC2 fleet. – Hard worker Sep 05 '15 at 18:14
  • 1
    If anyone is trying this and having trouble with "profile default not found", try `''` to specify a blank profile. Your profile may/should be stored in an environment variable `$AWS_DEFAULT_PROFILE` on the EC2 instance. – scrowler Dec 08 '15 at 02:52
  • 2
    @RobbieAverill You can just leave out the `--profile default` bit entirely (and as you are saying, setting it to default doesn't work if you have no default profile) – Tom Wadley Dec 18 '15 at 16:22
4

The closest you'll get to it with AWS managed services is CodeDeploy. With it, you can orchestrate deployment in EC2 instances via command line or web console. But CodeDeploy just pulls artifacts from S3 or GitHub, so far. By now, CodeCommit seems to be completely isolated from other related Amazon services, like CodePipeline and CodeDeploy, so it doesn't seem to be a good choice. But, off course, Amazon roadmap is to integrate all them (not doing this would be pointless). So, righ now, you would be better using GitHub than CodeCommit.

But, considering you are not using GitHub, then you need a CI (continuous integration) solution between you repository and CodeDeploy, pulling code from the source, possibly building or running tests, pushing it to S3 and telling CodeDeploy about it. There's CodeShip, for example, which can do that, and integrates with a lot of external services. Or you could even have your own CI server, like Jenkins, doing that "glue" role for you. (Jenkins will probably be the most flexible one because it's open-source and may have plugins for everything.)

So, breaking down a little bit, you workflow would be something like that:

  • push code to your repository;
  • whenever there's an event (a commit on some branch, or a new tag, it should be configurable), your CI pulls it, run whatever you want or need (builds, tests, packs it), and pushes it to S3 (as a tarball file, usually);
  • depending on how you have set things, your CI tells Code Deploy to deploy it on your EC2 instances right away, or it just tells it there's a new version available, and lets you to trigger the deploy, manually, via CLI on web console, whenever you want.

(Actually, CodeDeploy doesn't push code to EC2 instances. Instead, each EC2 instance must run an agent which pools regularly CodeDeploy server in order to know if there's something new to be applied locally. Anyway, CodeDeploy coordinates and get feedback from the agents, so it just works as if it were 100% active on CodeDeploy side and 100% passive on the instances side.)

The most "clean" AWS solution would be CodeCommit -> CodePipeline -> CodeDeploy, or just CodeCommit -> CodeDeploy, but those services are not completely integrated by now.

In your case, the simplest and viable solution right now would be Github -> CodeDeploy. Anything different from that will demand some intermediate steps on the way, like the examples I provided (CodeShip, Jenkins, etc).

Edson Marquezani Filho
  • 2,436
  • 1
  • 14
  • 18