AWS Elastic Beanstalk Swap Environment Urls
byAWS provides a feature to exchange or swap the CNAME between environments. Documentation says -
CNAME swap
Because AWS Elastic Beanstalk performs an in-place update when you update your application versions, your application can become unavailable to users for a short period of time. You can avoid this downtime by performing a blue/green deployment, where you deploy the new version to a separate environment, and then swap CNAMEs of the two environments to redirect traffic to the new version instantly. A blue/green deployment is also required when you want to update an environment to an incompatible platform version.If I want to upgrade my elastic beanstalk platform from say - for example php 5.6 to php 7.1 - then unfortunately I cannot do it. It allows me to upgrade within 5.6 platform but not switch between major platforms. Only recommended way is to create new environment with major upgraded platform and then swap the environment urls. This way original environment url will still be pointed to my domain running new platform.
In case if custom domain is pointing to current environment url in Route 53, then to point this same url to new environment this feature comes in handy allowing not to do anything on Route 53. All is taken care of in background. Elastic Beanstalk swaps the CNAME records of the old and new environments, redirecting traffic from the old version to the new version and vice versa.
Now, there is an official warning:
Warning
Blue/green deployments require that your environment runs independently of your production database, if your application uses one. If your environment has an Amazon RDS DB instance attached to it, the data will not transfer over to your second environment, and will be lost if you terminate the original environment.In that case I have to make sure that my new environment is all well-configured with RDS DB - the one original environment is using.
But I am here to discuss the common mistake error happening during this process. That is -
Error
You need at least two web tier environments in the Ready state to complete this operation.So far I have found its cause for two reasons:
- If I do not have new environment able to swap url with.
- If I do not have new environment created under same application with original environment in it.
The most common cause is the second one. I have to make sure that my two environments that I am gonna swap URLs between are under same application. I had created new environment under separate application and that caused this error because elastic beanstalk allows swapping url between environments under single application. I rectified that and made sure my environments are under same application. I was able to swap CNAME this time.
How long does it takes to propagate?
After Elastic Beanstalk completes the swap operation, verify that the new environment responds when you try to connect to the old environment URL. However, do not terminate your old environment until the DNS changes are propagated and your old DNS records expire. DNS servers don't necessarily clear old records from their cache based on the time to live (TTL) you set on your DNS records.Answer is: Assuming our domain is pointing to beanstalk environment url then this should takes within 60 seconds. That is what I have noticed so far. Remember swap url feature is to avoid downtime not to expect instant switch. Our domain will still be pointing to old environment well-working in case SWAP hasn't propgated yet so don't delete old environment until it propagates. Goal here is to avoid downtime. Now internal CNAME changes within 60 seconds by Route 53 according to offcicial doc. For me it did take less than 60 seconds.Conclusion: We cannot swap with an environment that was created under separate application - for now.
You might also like: HTTPS not working on AWS Elastic Beanstalk
You might also like: How to redirect HTTP to HTTPS in Elastic Beanstalk