Can you rename an Elastic Beanstalk environment?

Viewed 4444

I started to use the Swap Environment URLs feature in Elastic Beanstalk (CNAME swap). What is the best approach, if I want to stick to the same environment name?

Currently, I swap twice. First I bring up my changed my-env-new environment. I perform the first cname swap from my-env to my-env-new. Then I delete the previous my-env. I clone the new my-env-new into my-env and swap again. Is there a better way?

I run an AWS external CI/CD pipeline, which uses the environment name to update the environment, hence I need to retain that name. Maybe it's not a best practice to hardcode the environment name in such a pipeline.

2 Answers

Yes, this is pretty much how you do blue/green deployment using EB. The cloning is an integral part of it:

Lambda functions to clone the blue environment as the green environment, to re-swap the URLs, and to terminate the green environment when deployment and testing for the blue environment are complete.

The exact details how to do it in an automated manner are described in AWS whitepaper:

Regarding the hard-coding of the env name, I'm not sure from your question where is it being hard-coded? If you use CloudFormatin for your pipeline, you can pass it as a default parameter to it. This way it is easy to change and re-use your pipeline. If you created pipeline in AWS console, then selection of existing EB application and EB environment are necessary in a deploy stage.

What is the best approach, if I want to stick to the same environment name?

Unfortunately it's not possible to preserve the Environment name after the CNAME Swap. Elastic Beanstalk doesn't support the ability to rename an Environment.

You are correct that you can terminate the Environment with the desired name, then recreate the Environment with the same name. If you use this approach, as you mentioned, you'll need to wait for the old Environment to be cleaned up and removed from the list of Environments. Just like when EC2 instances terminate, there can be a delay (even up to an hour) where items are displayed even after termination. The Elastic Beanstalk Environment name cannot be reused until the the terminated Environment has been fully removed.

Maybe it's not a best practice to hardcode the environment name in such a pipeline.

Your options here will depend on what your specific use case is. For example, we commonly have users implementing CI/CD pipelines with Elastic Beanstalk, and they want to ensure the pipeline is configured to point to the correct Environment. Depending on the pipeline configuration, the name might need to be applied manually, or point the pipeline to the Environment CNAME instead.

If you needed to automate this so that the Environment name can be fetched for the swapped Environment, you might consider running the DescribeEnvironments() API call to scan all your Environments, and filtering based on the CNAME field. This would allow you to discover the Environment name by using the CNAME as the key. If you know the CNAME (which you're preserving across Environments during a CNAME Swap), then using that as a key for the filtering would offer an automated way of fetching an Environment name.

Related