public TestResult run() { final LambdaSeleniumService lambdaService = LambdaInvokerFactory.builder() .lambdaClient(AWSLambdaClientBuilder.defaultClient()) .build(LambdaSeleniumService.class); return lambdaService.runTest(request); }
void deploy(AwsKeyPair keyPair, String region, String zipFilename, String functionName, Proxy proxy) { long t = System.currentTimeMillis(); final AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider( new BasicAWSCredentials(keyPair.key, keyPair.secret)); AWSLambda lambda = AWSLambdaClientBuilder.standard().withCredentials(credentials) .withClientConfiguration(Util.createConfiguration(proxy)).withRegion(region).build(); byte[] bytes; try { bytes = IOUtils.toByteArray(new FileInputStream(zipFilename)); } catch (IOException e) { throw new RuntimeException(e); } DecimalFormat df = new DecimalFormat("0.000"); log.info("deploying " + zipFilename + ", length=" + df.format(bytes.length / 1024.0 / 1024.0) + "MB, to functionName=" + functionName); lambda.updateFunctionCode( // new UpdateFunctionCodeRequest() // .withFunctionName(functionName) // .withPublish(true) // .withZipFile(ByteBuffer.wrap(bytes))); log.info("deployed in " + (System.currentTimeMillis() - t) + "ms"); }
@Test public void testLambdaInvokeSubsegmentContainsFunctionName() { // Setup test AWSLambda lambda = AWSLambdaClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build(); AmazonHttpClient amazonHttpClient = new AmazonHttpClient(new ClientConfiguration()); ConnectionManagerAwareHttpClient apacheHttpClient = Mockito.mock(ConnectionManagerAwareHttpClient.class); HttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); BasicHttpEntity responseBody = new BasicHttpEntity(); responseBody.setContent(new ByteArrayInputStream("null".getBytes(StandardCharsets.UTF_8))); // Lambda returns "null" on successful fn. with no return value httpResponse.setEntity(responseBody); try { Mockito.doReturn(httpResponse).when(apacheHttpClient).execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class)); } catch (IOException e) { } Whitebox.setInternalState(amazonHttpClient, "httpClient", apacheHttpClient); Whitebox.setInternalState(lambda, "client", amazonHttpClient); // Test logic Segment segment = AWSXRay.beginSegment("test"); InvokeRequest request = new InvokeRequest(); request.setFunctionName("testFunctionName"); InvokeResult r = lambda.invoke(request); System.out.println(r.getStatusCode()); System.out.println(r); Assert.assertEquals(1, segment.getSubsegments().size()); Assert.assertEquals("Invoke", segment.getSubsegments().get(0).getAws().get("operation")); System.out.println(segment.getSubsegments().get(0).getAws()); Assert.assertEquals("testFunctionName", segment.getSubsegments().get(0).getAws().get("function_name")); }
public ConnectorModel(Regions region, String profileName) { ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(profileName); awsLambdaClient = AWSLambdaClientBuilder.standard() .withRegion(region) .withCredentials(profileCredentialsProvider) .build(); }
protected AWSLambda resolveAwsLambdaClient() { AWSLambda resolvedClient = awsLambdaClient; if (resolvedClient == null && region != null) { resolvedClient = AWSLambdaClientBuilder.standard().withRegion(region).build(); } return requireToBuild(resolvedClient, "an awsLambdaClient or a region is required"); }
/** * Executes a lambda function and returns the result of the execution. */ @Override public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { AmazonKey amazonKey = getAmazonKey( _session, argStruct ); // Arguments to extract String payload = getNamedStringParam( argStruct, "payload", null ); String functionName = getNamedStringParam( argStruct, "function", null ); String qualifier = getNamedStringParam( argStruct, "qualifier", null ); try { // Construct the Lambda Client InvokeRequest invokeRequest = new InvokeRequest(); invokeRequest.setInvocationType( InvocationType.Event ); invokeRequest.setLogType( LogType.Tail ); invokeRequest.setFunctionName( functionName ); invokeRequest.setPayload( payload ); if ( qualifier != null ) { invokeRequest.setQualifier( qualifier ); } // Lambda client must be created with credentials BasicAWSCredentials awsCreds = new BasicAWSCredentials( amazonKey.getKey(), amazonKey.getSecret() ); AWSLambda awsLambda = AWSLambdaClientBuilder.standard() .withRegion( amazonKey.getAmazonRegion().toAWSRegion().getName() ) .withCredentials( new AWSStaticCredentialsProvider( awsCreds ) ).build(); // Execute awsLambda.invoke( invokeRequest ); } catch ( Exception e ) { throwException( _session, "AmazonLambdaAsyncExecute: " + e.getMessage() ); return cfBooleanData.FALSE; } return cfBooleanData.TRUE; }
/** * Creates a proxy instance of a supplied interface that contains methods annotated with * {@link LambdaFunction}. Provides automatic credential support to authenticate with an IAM * access keys using {@link BasicSessionCredentials} auto-configured from Spring Boot * configuration properties in {@link AmazonProperties}. * * @param type * @param <T> * @return */ public <T> T getFunctionInstance(Class<T> type) { return LambdaInvokerFactory.builder() .lambdaClient(AWSLambdaClientBuilder.standard() .withRegion(Regions.US_EAST_1) .withCredentials(new LambdaCredentialsProvider(amazonProperties)) .build()) .build(type); }
public AWSLambda getLambdaClient() { return AWSLambdaClientBuilder.standard() .withRegion(Regions.US_EAST_1) .withCredentials(new LambdaCredentialsProvider(amazonProperties)) .build(); }
/** * Executes a lambda function and returns the result of the execution. */ @Override public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { AmazonKey amazonKey = getAmazonKey( _session, argStruct ); // Arguments to extract String payload = getNamedStringParam( argStruct, "payload", null ); String functionName = getNamedStringParam( argStruct, "function", null ); String qualifier = getNamedStringParam( argStruct, "qualifier", null ); try { // Construct the Lambda Client InvokeRequest invokeRequest = new InvokeRequest(); invokeRequest.setInvocationType( InvocationType.RequestResponse ); invokeRequest.setLogType( LogType.Tail ); invokeRequest.setFunctionName( functionName ); invokeRequest.setPayload( payload ); if ( qualifier != null ) { invokeRequest.setQualifier( qualifier ); } // Lambda client must be created with credentials BasicAWSCredentials awsCreds = new BasicAWSCredentials( amazonKey.getKey(), amazonKey.getSecret() ); AWSLambda awsLambda = AWSLambdaClientBuilder.standard() .withRegion( amazonKey.getAmazonRegion().toAWSRegion().getName() ) .withCredentials( new AWSStaticCredentialsProvider( awsCreds ) ).build(); // Execute and process the results InvokeResult result = awsLambda.invoke( invokeRequest ); // Convert the returned result ByteBuffer resultPayload = result.getPayload(); String resultJson = new String( resultPayload.array(), "UTF-8" ); Map<String, Object> resultMap = Jackson.fromJsonString( resultJson, Map.class ); return tagUtils.convertToCfData( resultMap ); } catch ( Exception e ) { throwException( _session, "AmazonLambdaExecute: " + e.getMessage() ); return cfBooleanData.FALSE; } }