如何从AWS SDK for Java克隆Elastic Beanstalk环境?

时间:2016-10-06 22:26:12

标签: java elastic-beanstalk aws-sdk

我需要从AWS SDK for Java克隆Elastic Beanstalk环境。

我找到this解答如何创建环境,但我找不到任何示例或文档来说明如何克隆环境。

我为clone找到了CreateEnvironmentRequest方法,但根据documentation,它克隆了CreateEnvironmentRequest对象,而不是环境。

enter image description here

2 个答案:

答案 0 :(得分:2)

已经为此打开了issue,因为现在无法从sdk进行克隆操作。如果您有兴趣,可能需要重新打开故障单并提供理由。

另一种可能性是从Java调用eb cli来运行环境的克隆。

答案 1 :(得分:2)

好吧,你无法使用Java SDK for AWS完全克隆环境,我的方法是每次只创建一个新环境,你可以这样做:

  1. 将您的部署文件(war,zip)上传到S3

  2. 为您的部署创建一个链接到以前上传的文件的版本

    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. 您去寻找最新版本的堆栈

    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. 配置环境,系统属性和所有内容的属性。

    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. 创建一切准备就绪的环境。

    public void deployCreatingEnvironment() {
        getLatestStackSolutionVersion();
        beanstalkClient.createApplicationVersion(createApplicationVersion());
        beanstalkClient.createEnvironment(setupNewEnvironment());
    }
    
  6. 瞧!这就像每次克隆一样,希望这对你有用。