简体   繁体   English

如何使用 java lambda 函数创建 ec2 实例?

[英]How to create ec2 instance using java lambda function?

I am able to create an EC2 instance using Java from my local machine in Eclipse.我可以在 Eclipse 中使用本地机器上的 Java 创建 EC2 实例。 The issue that I am facing is I cannot find a way to create an EC2 instance using Java Lambda Function.我面临的问题是我找不到使用 Java Lambda 函数创建 EC2 实例的方法。 Can someone please help me?有人可以帮帮我吗?

The lambda function I am using:-我正在使用的 lambda 函数:-

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";
    }

}

This is the code I have used:- This is the error I have got:-这是我使用的代码:- 这是我得到的错误:-

[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] ------------------------------------------------------------------------

You can wrap a Java API call that invokes a given service in a Lambda function.您可以将调用给定服务的 Java API 调用封装在 Lambda 函数中。 We have written an article that shows you how to create a Lambda function that invokes an AWS Service operation by using a given service client.我们写了一篇文章,向您展示如何创建一个 Lambda 函数,该函数使用给定的服务客户端调用 AWS 服务操作。

Then the Lambda function is used in AWS Steps to create a workflow.然后在 AWS Steps 中使用 Lambda 函数来创建工作流。 If you follow this document, you will see the pattern to successfully invoke AWS Service operation from code that can be wrapped in a Lambda function.如果您遵循本文档,您将看到从可以包装在 Lambda 函数中的代码成功调用 AWS 服务操作的模式。 See:看:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_serverless%20workflows https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_serverless%20workflows

In your use case, you can use the EC2 Service Client within a Lambda function.在您的使用案例中,您可以在 Lambda 函数中使用 EC2 服务客户端。

I don't think you've included the aws-lambda-java-core dependency我认为您没有包含 aws-lambda-java-core 依赖项

The Lambda developer guide goes into the process in detail Lambda 开发人员指南详细介绍了该过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM