Tachú Salamanca Tachú Salamanca - 1 month ago 18
Java Question

How can I clone an Elastic Beanstalk environment from the AWS SDK for Java?

I need to clone an Elastic Beanstalk environment from the AWS SDK for Java.

I found this answer explaining how to create an environment but I can't find any example or documentation showing how to clone an environment.

I found a

clone
method for
CreateEnvironmentRequest
but according to the documentation it clones the
CreateEnvironmentRequest
object, not the environment.

enter image description here

Answer

Well, you can't exactly clone an environment with the Java SDK for AWS, my approach is to simply create a new environment every time, you can do something like this:

  1. Upload your deployment file (war, zip whatever) to S3

  2. Create a version for your deployment linking to the previously uploaded file

    private CreateApplicationVersionRequest createApplicationVersion() {
        return new CreateApplicationVersionRequest()
                .withApplicationName("The app name")
                .withAutoCreateApplication(true)
                .withSourceBundle(new S3Location("bucket_name", deployedArtifactId))
                .withVersionLabel("a number for the version");
    }
    
  3. You go and look for the latest version of your stack

    private void getLatestStackSolutionVersion() {
        ListAvailableSolutionStacksResult response = beanstalkClient.listAvailableSolutionStacks();
        for (int i = 0; i < response.getSolutionStacks().size(); i++) {
            if (response.getSolutionStacks().get(i).contains("Java 8") {
                stackName = response.getSolutionStacks().get(i);
                break;
            }
        }    
    }
    
  4. Configure the properties of your environment, system properties and everything.

    private Collection<ConfigurationOptionSetting> setEnvironmentProperties() {
        Collection<ConfigurationOptionSetting> configurationOptionSettings = new HashSet<>();
        configurationOptionSettings.add(new ConfigurationOptionSetting("aws:autoscaling:launchconfiguration", "InstanceType", "t2.medium"));
        configurationOptionSettings.add(new ConfigurationOptionSetting("aws:ec2:vpc", "VPCId","thevpcid");
        configurationOptionSettings.add(new ConfigurationOptionSetting("aws:ec2:vpc", "Subnets", "thesubnets");
        configurationOptionSettings.add(new ConfigurationOptionSetting("aws:ec2:vpc", "ELBSubnets", "theelbsubnets");
        configurationOptionSettings.add(new ConfigurationOptionSetting("aws:elasticbeanstalk:application:environment", "ANOTHER_PROPERTY", "a value"));
        return configurationOptionSettings;
    }
    
  5. Create the environment with everything ready.

    public void deployCreatingEnvironment() {
        getLatestStackSolutionVersion();
        beanstalkClient.createApplicationVersion(createApplicationVersion());
        beanstalkClient.createEnvironment(setupNewEnvironment());
    }
    

VoilĂ ! It is like cloning everytime, hope this works for you.