Cloning Amazon Elastic beanstalk environment

October 1, 2014 | | Tags : AWS Elastic Beanstalk Powershell


In order to deploy new versions of software using Elastic Beanstalk, Amazon proposed to deploy it first to the new environment of the application, then swap environment CNAME records as it is described here.

In order to simplify creation of the new environment I created a simple powershell script that saves the current configuration of production environment and creates a new environment based on that configuration, removing the saved configuration after the new environment has been initiated.

Here it is. Save it to the file with .ps1 extension and run. You may need to download and install AWS tools for Elastic Beanstalk from here.

#############################################
 # declare parameters  
 [CmdletBinding()]  
 Param(  
   [Parameter(Mandatory=$True)]  
   [string]$ApplicationName,  
   [Parameter(Mandatory=$True)]  
   [string]$SourceEnvironmentName,  
   [Parameter(Mandatory=$True)]  
   [string]$NewEnvironmentName,  
   [Parameter(Mandatory=$True)]  
   [string]$NewEnvironmentCname  
 )  
 # this creates a unique name that we will use later  
 $templateName = [Guid]::NewGuid().ToString();   
 $environment = Get-EBEnvironment -ApplicationName $ApplicationName -EnvironmentNames $SourceEnvironmentName;  
 $environmentId = $environment.EnvironmentId;  
 # because it does find the application case insensitive, but does not want to use it  
 $ApplicationName = $environment.ApplicationName;  
 #echo $templateName;  
 #create new template  
 $template = New-EBConfigurationTemplate -ApplicationName $ApplicationName -TemplateName $templateName -EnvironmentId $environmentId;  
 while (-not (Get-EBApplication -ApplicationNames $ApplicationName)[0].ConfigurationTemplates.Contains($templateName)) {  
   echo ([string]::Format("can't find template '{0}'", $templateName));  
   Start-Sleep -s 5;  
 }  
 (Get-EBApplication -ApplicationNames $ApplicationName)[0].ConfigurationTemplates.Contains($templateName);  
 Get-EBEnvironment -ApplicationName $ApplicationName -EnvironmentNames $SourceEnvironmentName  
 # create new environment from that template  
 New-EBEnvironment -ApplicationName $ApplicationName -EnvironmentName $NewEnvironmentName -CNAMEPrefix $NewEnvironmentCname -TemplateName $template.TemplateName;  
 Remove-EBConfigurationTemplate -ApplicationName $ApplicationName -TemplateName $templateName -Force;  

Comments