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!
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!
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 use some different kinds of concepts:
1
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.
Upload your code to an already existing s3 bucket:
1
aws s3 cp lazy_app.zip s3://${alreadyExistingBucket}/path/to/lazy_app.zip
1
aws deploy create-application --application-name LazyApp
1
2
3
4
5
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.
1
2
3
4
5
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
1
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
You are too lazy to run six commands? Ok, just run one!
1
lazydeploy ec2 <EC2_INSTANCE_NAME> <S3_BUCKET> <S3_PATH> <CODEDEPLOYROLE> [--exclude ]
For example:
1
lazydeploy ec2 hakuna-503 pipeline app/ CodeDeployRole "--exclude=*git* --exclude=.env --exclude=*node_modules* --exclude=*dist*"
You can find it on GitHub
Giorgio Catenacci
CTO @ HakunaCloud
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."