Как создать экземпляр ec2 с помощью лямбда-функции Java? - PullRequest
0 голосов
/ 24 января 2019

Я могу создать экземпляр EC2, используя Java, с моей локальной машины в Eclipse. Проблема, с которой я сталкиваюсь, заключается в том, что я не могу найти способ создания экземпляра EC2 с использованием лямбда-функции Java. Может ли кто-нибудь помочь мне?

Я использую лямбда-функцию: -

package com.ec2application.ec2application;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.CreateTagsRequest;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class EC2UsingLambda implements RequestHandler<Object, String> 
{
    private static final AWSCredentials AWS_CREDENTIALS;

    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "myid",
                "key"
        );
    }


    public String handleRequest(Object input, Context context) 
    {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        // Set up the amazon ec2 client
        AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
                .withRegion(Regions.US_EAST_1)
                .build();

        // Launch an Amazon EC2 Instance
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-0080e4c5bc078760e")
                .withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
                .withMinCount(1)
                .withMaxCount(1)
                .withKeyName("priyajdm")
                .withNetworkInterfaces(new InstanceNetworkInterfaceSpecification()
                        .withAssociatePublicIpAddress(true)
                        .withDeviceIndex(0)
                        .withSubnetId("subnet-02ffd56c277")
                        .withGroups("sg-0e93be4d"));

        RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest);

        Instance instance = runInstancesResult.getReservation().getInstances().get(0);
        String instanceId = instance.getInstanceId();
        System.out.println("EC2 Instance Id: " + instanceId);

        // Setting up the tags for the instance
        CreateTagsRequest createTagsRequest = new CreateTagsRequest()
                .withResources(instance.getInstanceId())
                .withTags(new Tag("Name", "demo"));
        ec2Client.createTags(createTagsRequest);

        // Starting the Instance
        StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(instanceId);

        ec2Client.startInstances(startInstancesRequest);
        return "EC2 Instance created";
    }

}

Это код, который я использовал: - Это ошибка, которую я получил: -

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/priyj_kumar/workspace/EC2Application/src/main/java/com/ec2application/ec2application/EC2UsingLambda.java:[16,45] package com.amazonaws.services.lambda.runtime does not exist
[ERROR] /C:/Users/priyj_kumar/workspace/EC2Application/src/main/java/com/ec2application/ec2application/EC2UsingLambda.java:[17,45] package com.amazonaws.services.lambda.runtime does not exist
[ERROR] /C:/Users/priyj_kumar/workspace/EC2Application/src/main/java/com/ec2application/ec2application/EC2UsingLambda.java:[19,40] cannot find symbol
  symbol: class RequestHandler
[ERROR] /C:/Users/priyj_kumar/workspace/EC2Application/src/main/java/com/ec2application/ec2application/EC2UsingLambda.java:[32,47] cannot find symbol
  symbol:   class Context
  location: class com.ec2application.ec2application.EC2UsingLambda
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...