fbpixel

Lazy DevOps: AWS CodeDeploy

Giorgio Catenacci}
Giorgio Catenacci
Share:

In the real world you just need to easy deploy your code on an EC2 instance!

If you are too lazy to read, just go to the bottom of this post and run the scripts!

Lazy DevOps: CodeDeploy

Devops is the new Blockchain, except the fact it can be useful. All kidding aside, DevOps set of practices are making the lives of engineers much better and in this post we will use AWS CodeDeploy, a great tool that AWS offer to deploy your application (from GitHub or S3 or similiar) to lambdas or EC2 instances. You can use CodeDeploy with a bunch of other tools from AWS (CodePipeline!) or not (I use it often with Bitbucket Pipelines or CircleCI), to build your wonderfull full CI/CD devops pipeline

Anyway, down-to-earth, sometimes a lazy developer just need to deploy his code to his EC2 instance. Just as simple as it sounds, without committing the code to any repository, without running any CI/CD tool or something like that. I know, I know, you are already blaming me about that last sentence, but no one is looking at you at the moment, this voice is only inside your mind, you can admit it: IT’S HANDY!

So, how can you do that without broke up anything and just running a command? How can you upload your code to an EC2 instance?

In this post I will show you how to deploy a nodejs application, from your laptop to an EC2 instance, using aws cli and some other stuff.

If you are lazy enough to be interested, take a deep breath and go ahead with this post. Be patient, your little commitment will be paid back!

If you are too much lazy to read, just go to the bottom of this post and run the scripts!

Lazy Sloth
Being lazy is not always a bad thing. It help you to decide what matters and what does not.

Requirements

We are going to use the aws cli and zip, make sure you got it both installed.

You are going to need a CodeDeploRole, already cofnigured in your AWS Account. Follow this instructions if you do not already have it.

You need an s3 bucket as well. I will call it alreadyExistingBucket. Remember to replace it!

CodeDeploy concepts

CodeDeploy use some different kinds of concepts:

  • Application: is what your are going to deploy, just to give it a name. Let’s say “LazyApp”;
  • Deployment group: is where you are going to deploy. We will use EC2 tag filter to choose to which instance deploy, specifing the name of the instance: Name=LazyInstance;
  • Deployment: well, it is what you want to do!
  • Appsec and scripts: this is how you want to deploy the application. You can specify what to do after deploying code, how to start and stop the application and so on. This is beyond the purpose of this post.

Babysteps

  1. Open a terminal in your project folder and zip up your code runing:
     zip -r lazy_app.zip ./ --exclude=*.git*
    

    You can add some other stuff to exclude, depending on how it works and what you do in your appsec scripts. For example if our nodejs application is using npm and in the afterinstall.sh script we laung the npm install command, we can add --exclide=*node_modules* option to this command.  

  2. Upload your code to an already existing s3 bucket:

     aws s3 cp lazy_app.zip s3://${alreadyExistingBucket}/path/to/lazy_app.zip
    
  3. Create an applcation using the aws cli:
     aws deploy create-application --application-name LazyApp
    
  4. Create a deployment group running:
     aws deploy create-deployment-group --application-name CodeDeployTestDev \
     --ec2-tag-filters Key=Name,Type=KEY_AND_VALUE,Value=LazyInstance \
     --deployment-group-name LazyGroup \
     --service-role-arn \ 
     $(aws iam get-role --role-name ${CodeDeployRole} --query "Role.Arn" --output text)
    

    remember to insert yout CodeDeploy Role replacing the placeholder.

  5. Finally, create a deployment to deploy it:
     aws deploy create-deployment --application-name LazyApp \
     --deployment-config-name CodeDeployDefault.OneAtATime  \ 
     --deployment-group-name LazyGroup \
      --s3-location ${alereadyExistingBucket} \
      bundleType=zip,key=path/to/lazy_app.zip
    
  6. Verify the deployment:
     aws deploy get-deployment --deployment-id $(aws deploy list-deployments --application-name LazyApp --deployment-group-name LazyGroup --query "deployments" --output text) --query "deploymentInfo.[status, creator]" --output text
    
  7. The magic trick just happen! Enjoy your deployed code!

Only for laziest

You are too lazy to run six commands? Ok, just run one!

lazydeploy ec2 <EC2_INSTANCE_NAME> <S3_BUCKET> <S3_PATH> <CODEDEPLOYROLE> [--exclude ]

For example:

lazydeploy ec2 hakuna-503 pipeline app/ CodeDeployRole "--exclude=*git* --exclude=.env --exclude=*node_modules* --exclude=*dist*"

You can find it on GitHub

CTO @ BeeKube

Developer Team Leader, later Senior Cloud Consultant for NTT Data, Cloud Solution Architect on projects in the US, Europe, Japan.

"I'm obsessed with the idea of using any design techniques to improve the experience of people in any scope."