My First Hand-built AWS CodePipeline

I’ve just completed my first, successful, AWS CodePipeline. I’m sorry to say it took me weeks to figure out just how simple this was. Frankly, it’s embarrassing. I already posted how I used the example code supplied by AWS to build a full blown CI/CD process. However, I went back into all the tools and tore it all apart so that I could build it, myself, by hand, in order to better understand it.

Code All The Things

Let’s first talk about the tools I used. I have my code sourced in CodeCommit. It’s Git. I’m using AWS CodePipeline to manage the flow control. It’s triggered by a commit to the main branch. It sets up an Ubuntu instance that I can use to do things with (you can use Windows and other flavors of Linux too). Then, CodePipeline uses CodeBuild to run commands on the instance.

All that I knew from my previous experience using their example to build a deployment pipeline. What I struggled with for weeks, yes weeks, was how to get commands into the CodeBuild steps that I wanted to run. Anyone who already knows the answer has their face in their palm right now.

It’s easy, you just need a buildspec.yml file in your code repository. It might look something like this:

buildspec.yml

If you just follow through the GUI to set up everything else, you’re most of where you need to be. Then you have to create this buildspec.yml file. I spent weeks struggling to figure out how to run commands and other stuff through all sorts of means. Finally, it occurred to me to start searching through the code I had in the example. I discovered two simple bits of text that lead me to my solution: build: phases:

I found these because I searched for what had to be in the code somewhere, the commands for Flyway. Discovering those and understanding them already, I could back into the incredibly simple solution. It’s sad. However, so you don’t struggle, let me show my first buildspec.yml file:

version: 0.2

phases:
    build:
        commands:
            - wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/6.5.3/flyway-commandline-6.5.3-linux-x64.tar.gz | tar xvz && ln -s `pwd`/flyway-6.5.3/flyway /usr/local/bin 
            - flyway -enterprise migrate

It looks like a lot, but it isn’t. Two things are happening. I’ve issued a wget command to retrieve the Flyway command line and unpack it. Then, I run Flyway through the migrate command. I’ve got my database configured in the flyway.conf file above.

That’s it. It’s that simple.

There’s a lot more capability in there. I’m going to expand out into using parameters and variables for a more sophisticated build. You can run installations, pre-build steps, post-build steps, generate artifacts and control the security. Read all about it here.

Oh, and, if, like me, you’re just getting started in yaml, use Visual Studio Code. It’s a great yaml editor.

Conclusion

Weeks?

Yeah. Sorry. I’m not that bright.

However, now that I’ve cracked the code, as it were, I’m going to be able to do a lot more. The thing is, through AWS CodePipeline you can build a series of steps, consisting of CodeBuild sets of commands. This enables you to then create a process and deploy to multiple environments, across multiple pipelines, all sorts of stuff. Whatever you need to configure your systems.

Now, I actually already have a single line of code that will do the same thing I did above. Once you figure out what’s possible within the environments generated, lots of commands open up. For example: docker run…

But that’s for another blog post.

Please let me know what you think about this article or any questions:

This site uses Akismet to reduce spam. Learn how your comment data is processed.