15

Is there a way to export existing CodePipeline/CodeBuild projects to Cloudformation?

I'm in the process of converting our existing CI/CD infrastructure into code so it would be easy to replicate them on other AWS regions.

Currently, we've done all our CodePipeline/CodeBuild projects through the AWS console in the browser and obviously, this is tedious and error-prone.

Noel Llevares
  • 11,823
  • 2
  • 41
  • 72

3 Answers3

19

If you have some codepipeline already created use the cli to extract them as cloudformation.

The command is roughly this

aws codepipeline get-pipeline --name

It will get you back a codepipeline resource for cloudformation. You will be able to see where you need to enter in other resources like codebuild projects and s3 buckets or codecommit repos.

Be aware though that you will need to work through the template to ensure it will be taken as valid by cloudformation. The lines of the resource from the cli need to start with capital letters so it can be tedious to change that.

Another thing to know is a pipeline needed I think two roles which you can have in your template also and it's own bucket. All of these things cab be put in a single template making reproducible pipelines possible. Good luck!

hynespm
  • 612
  • 4
  • 13
  • Not really the answer I hoped but it's the closest thing I can do. Thanks. – Noel Llevares Jan 04 '18 at 03:58
  • Yep its certainly not ideal. But once you do it once, you may be able to create your other pipelines in the future more easily. – hynespm Jan 04 '18 at 20:37
  • it's returned as JSON. anyone got a quick hack to extract cloudformation YAML? – John Vandivier Feb 13 '19 at 22:36
  • I don't believe it comes back that way, although you should check if it comes back as JSON simply because JSON is set as the output format of the cli profile you are using. Text is another format possible for output from CLI commands. – hynespm Feb 15 '19 at 07:42
  • A: I had no idea this existed. I might love you a little. B: @JohnVandivier and anyone else coming here wondering, I just use [CloudFormation designer](https://console.aws.amazon.com/cloudformation/designer). Set the template format to JSON, paste in the CLI results, then set it to YAML and it will convert it for you. – Steve Apr 25 '19 at 22:04
  • this is beautiful. so simple and to the point. you saved me hourss! thanks! – Sebastian Serrano Jun 03 '19 at 23:38
7

And try this for the codebuild-project:

aws codebuild batch-get-projects --name --output json

Then you the CodeBuild Template. Hint:

aws help

or

aws codebuild help

will help you very well.

Thomas Michael
  • 129
  • 2
  • 4
5

I would normally suggest CloudFormer for generating CloudFormation templates from existing resources, but it does not appear that CodePipeline/CodeBuild are supported.

Your next best bet is to start with existing templates, and modify them according to your needs.

Here are some examples:

  1. https://github.com/awslabs/codepipeline-nested-cfn
  2. https://github.com/stelligent/dromedary/blob/master/pipeline/cfn/codepipeline-cfn.json
Rodrigo M
  • 11,060
  • 2
  • 21
  • 43