如何使用java sdk创建AWS Elastic Beanstalk环境?

时间:2014-11-25 03:37:11

标签: amazon-web-services

任何人都可以帮助我或使用java程序提供任何创建Aws Elastic beanstalk环境的资源并在其中解密我们的应用程序吗?

提前谢谢。

3 个答案:

答案 0 :(得分:2)

您可以下载AWS Java SDK here。它也在maven存储库中:

<强>的Maven:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.9.7</version>
</dependency>

<强>摇篮:

'com.amazonaws:aws-java-sdk:1.9.7'

现在,使用sdk。您可能需要阅读getting started with the aws sdk.

这是一些非常淡化的代码,可以帮助您入门:

import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalkClient;
import com.amazonaws.services.elasticbeanstalk.model.*;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.File;

public class AwsTest {

    public static void main(String[] args) {

        AWSElasticBeanstalkClient eb = new AWSElasticBeanstalkClient();

        // Create Application
        CreateApplicationRequest request = new CreateApplicationRequest("myAppName");
        eb.createApplication(request);

        // Create Environment
        CreateEnvironmentRequest envRequest = new CreateEnvironmentRequest("myAppName", "env-name");
        envRequest.setSolutionStackName("64bit Amazon Linux 2014.09 v1.0.9 running Tomcat 7 Java 7");
        envRequest.setVersionLabel("application Version");
        eb.createEnvironment(envRequest);

        // Deploy code
        CreateStorageLocationResult location = eb.createStorageLocation();
        String bucket = location.getS3Bucket();
        File file = new File("myapp.zip");
        PutObjectRequest object = new PutObjectRequest(bucket, "myapp.zip", file);
        new AmazonS3Client().putObject(object);

        CreateApplicationVersionRequest versionRequest = new CreateApplicationVersionRequest();
        versionRequest.setVersionLabel("myversion");
        versionRequest.setApplicationName("myAppName");
        S3Location s3 = new S3Location(bucket, "myapp.zip");
        versionRequest.setSourceBundle(s3);

        UpdateEnvironmentRequest updateRequest = new UpdateEnvironmentRequest();
        updateRequest.setVersionLabel("myversion");
        eb.updateEnvironment(updateRequest);

    }
}

答案 1 :(得分:0)

部署AWS Elastic Beanstalk环境的便捷方法是使用AWS Toolkit for Eclipse

它允许您在本地编写和测试代码,然后创建Elastic Beanstalk环境并将代码部署到环境中。

Elastic Beanstalk管理控制台还可用于使用示例应用程序部署Java环境,然后您可以使用自己的代码覆盖它。

另见:

答案 2 :(得分:0)

在本节的上面给出的代码中缺少一小段代码,

CreateApplicationVersionRequest versionRequest = new CreateApplicationVersionRequest();
        versionRequest.setVersionLabel("myversion");
        versionRequest.setApplicationName("myAppName");
        S3Location s3 = new S3Location(bucket, "myapp.zip");
        versionRequest.setSourceBundle(s3);

您需要添加eb.createApplicationVersion(versionRequest);才能使用自己的源文件创建新版本。只有这样,您才能将新版本部署到正在运行的环境实例中。