imTachu
imTachu

Reputation: 3809

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

Upvotes: 2

Views: 1162

Answers (2)

Andrés Esguerra
Andrés Esguerra

Reputation: 861

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.

Upvotes: 2

Frederic Henri
Frederic Henri

Reputation: 53793

There has been an issue opened for this as for now the clone operation is not possible from the sdk. If you're interested you might want to reopen the ticket and provide your reason.

The other possibility is to call the eb cli from your Java to run the clone of your environment.

Upvotes: 2

Related Questions