Search code examples
node.jsamazon-web-servicesdevopsbitbucket-pipelinesaws-code-deploy

AWS CD/CI pipeline with ASG and ELB (bitbucket)


I must first preface by saying I am relatively new to AWS but finding it immensely useful. Let me describe my scenario...

What I have currently

  • An Auto scaling group (ASG)
  • An Elastic Load Balancer (ELB)
  • A CD/CI pipeline using CodeDeploy and Bitbucket
  • Node/Express app serving a custom API on EC2 instances
  • VPC and subnets are working well
  • An AMI with my app code

My question

When the ASG decides to scale in a new EC2 instance using my launch template and AMI, it will use the application code from the AMI. But if I deploy to master at some point, my AMI will not be updated, but instances within the ASG will be updated. What is the best method for ensuring the new instances spawned by the ASG are running the latest code version (master)?

My initial thoughts

I am thinking to include a bash script in the launch config that will pull the latest code from Bitbucket and run any following steps to get my application running (such as "npm install", "npm run start" etc etc). I'm sure someone has a more elegant solution, and I'd love to hear some suggestions.


Solution

  • To anyone coming to this, I did solve my problem. I was correct initially. The "user data" field within the Launch Template was a good place to bootstrap my app once the instance is up and running. It basically clones from the remote repo and performs any necessary steps to launch the app after that.

    For example, in the launch configuration for EC2

    #cloud-boothook
    #!/bin/bash
    git clone myremoterepo.git
    cd myremoterepo
    npm install
    npm run start
    

    Also, if you're running a classic load balancer, CodeDeploy will attempt to start a deployment, based on your latest code repo in S3, when your ASG scales up the EC2 instances. So the above solution will be redundant.