This repository shows an example program to upgrade EC2 instances behind an ELB. What it does:
- Detect all EC2 instances with the given AMI
- Find the ELB for the instances
- Deploy new EC2 instances with the same configuration, except with a new AMI
- Attach the new EC2 instances to the ELB
- Wait till the new EC2s are serving traffic
- Terminate the old EC2s
- AWS CLI v2
- Golang compiler 1.16
The program to deploy the new EC2 instances is written in Go, thus it requires to be compiled.
To compile the program, run:
make build
You should now have the deploy
binary in the current directory.
To run the tests:
make test
-
Deploy the environment using CloudFormation:
make bootstrap-env
The script will print the DNS of the LoadBalancer at the end.
-
Verify the environment is working. This can take a few minutes, as the domain name needs to propagate. Access the LoadBalancer DNS to check, if the webpage is working:
curl http://<your-loadbalancer-dns>
You should see the deployment date and the AMI ID of the instance on the webpage.
-
Execute the
deploy
binary to perform a deployment of new EC2 instances../deploy <old-ami-id> <new-ami-id>
It will create new EC2 instances, attach them to the load balancer, then detach and remove the old EC2 instances.
-
Verify again, if the webpage is working. You should the new AMI ID on the webpage.
To remove all resources you have to:
- Remove the EC2 instances manually. They cannot be removed using CloudFormation, because they were created using the
deploy
program. - Delete the CloudFormation stack:
make destroy-env