|
| 1 | +/** |
| 2 | + * LambdaCreate is an example that handles Lambda functions on AWS. |
| 3 | + * Create a Lambda function. |
| 4 | + * You must provide 1 parameter: |
| 5 | + * FUNCTION_NAME = Lambda function name |
| 6 | + * FUNCTION_FILE = The path to the JAR or ZIP file where the code of the Lambda function is located |
| 7 | + * FUNCTION_ROLE = The role ARN that has Lambda permissions |
| 8 | + * FUNCTION_HANDLER = The fully qualified method name (Ex: example.Handler::handleRequest) |
| 9 | + */ |
| 10 | + |
| 11 | +package example; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import com.amazonaws.AmazonClientException; |
| 18 | +import com.amazonaws.AmazonServiceException; |
| 19 | +import com.amazonaws.auth.profile.ProfileCredentialsProvider; |
| 20 | +import com.amazonaws.services.lambda.AWSLambda; |
| 21 | +import com.amazonaws.services.lambda.AWSLambdaClientBuilder; |
| 22 | +import com.amazonaws.services.lambda.model.ServiceException; |
| 23 | +import com.amazonaws.services.lambda.model.CreateFunctionRequest; |
| 24 | +import com.amazonaws.services.lambda.model.CreateFunctionResult; |
| 25 | +import com.amazonaws.services.lambda.model.FunctionCode; |
| 26 | +import com.amazonaws.services.lambda.model.VpcConfig; |
| 27 | + |
| 28 | + |
| 29 | +public class LambdaCreate { |
| 30 | + private static final String REGION = "eu-west-1"; // Region name |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + |
| 34 | + if (args.length < 4) { |
| 35 | + System.out.println("Not enough parameters.\n"+ |
| 36 | + "Proper Usage is: java -jar lambdacreate.jar " + |
| 37 | + "<FUNCTION_NAME> <FUNCTION_FILE> <FUNCTION_ROLE> <FUNCTION_HANDLER>"); |
| 38 | + System.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + String functionName = args[0]; |
| 42 | + String functionFile = args[1]; |
| 43 | + String functionRole = args[2]; |
| 44 | + String functionHandler = args[3]; |
| 45 | + |
| 46 | + System.out.println("Lambda function name: " + functionName); |
| 47 | + System.out.println("Lambda function file: " + functionFile); |
| 48 | + System.out.println("Lambda function role: " + functionRole); |
| 49 | + System.out.println("Lambda function handler: " + functionHandler); |
| 50 | + |
| 51 | + AWSLambda awsLambda = AWSLambdaClientBuilder.standard() |
| 52 | + .withCredentials(new ProfileCredentialsProvider()) |
| 53 | + .withRegion(REGION).build(); |
| 54 | + |
| 55 | + try { |
| 56 | + System.out.println("Creating Lambda function ..."); |
| 57 | + |
| 58 | + byte[] byteArray = Files.readAllBytes(Paths.get(functionFile)); |
| 59 | + ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); |
| 60 | + |
| 61 | + FunctionCode code = new FunctionCode().withZipFile(byteBuffer); |
| 62 | + |
| 63 | + // Create Lambda function |
| 64 | + CreateFunctionRequest functionRequest = new CreateFunctionRequest() |
| 65 | + .withFunctionName(functionName) |
| 66 | + .withRuntime("java8") |
| 67 | + .withRole(functionRole) |
| 68 | + .withHandler(functionHandler) |
| 69 | + .withCode(code) |
| 70 | + .withDescription("Created by the Lambda Java API") |
| 71 | + .withTimeout(15) |
| 72 | + .withMemorySize(128) |
| 73 | + .withPublish(true) |
| 74 | + .withVpcConfig(new VpcConfig()); |
| 75 | + CreateFunctionResult functionResponse = awsLambda.createFunction(functionRequest); |
| 76 | + |
| 77 | + System.out.println("Created"); |
| 78 | + System.out.println("The function ARN is " + functionResponse.getFunctionArn()); |
| 79 | + |
| 80 | + } catch (IOException ioe) { |
| 81 | + System.err.println(ioe.getMessage()); |
| 82 | + } catch (ServiceException e) { |
| 83 | + System.out.println("ServiceException: " + e); |
| 84 | + } catch (AmazonServiceException ase) { |
| 85 | + System.out.println("Caught an AmazonServiceException, " + |
| 86 | + "which means your request made it " + |
| 87 | + "to AWS Lambda, but was rejected with an error " + |
| 88 | + "response for some reason."); |
| 89 | + System.out.println("Error Message: " + ase.getMessage()); |
| 90 | + System.out.println("HTTP Status Code: " + ase.getStatusCode()); |
| 91 | + System.out.println("AWS Error Code: " + ase.getErrorCode()); |
| 92 | + System.out.println("Error Type: " + ase.getErrorType()); |
| 93 | + System.out.println("Request ID: " + ase.getRequestId()); |
| 94 | + } catch (AmazonClientException ace) { |
| 95 | + System.out.println("Caught an AmazonClientException, " + |
| 96 | + "which means the client encountered " + |
| 97 | + "an internal error while trying to " + |
| 98 | + " communicate with AWS Lambda, " + |
| 99 | + "such as not being able to access the network."); |
| 100 | + System.out.println("Error Message: " + ace.getMessage()); |
| 101 | + } |
| 102 | + awsLambda.shutdown(); |
| 103 | + } |
| 104 | +} |
0 commit comments