Skip to content

Commit

Permalink
Add Support for AWS Lambda Function SDK V2 (#2130)
Browse files Browse the repository at this point in the history
  • Loading branch information
driverpt committed Aug 20, 2024
1 parent d1df556 commit b1389bb
Show file tree
Hide file tree
Showing 55 changed files with 1,340 additions and 105 deletions.
2 changes: 2 additions & 0 deletions aws-sdk-v2/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
compileOnly(libs.awssdk.secretsmanager)
compileOnly(libs.awssdk.servicediscovery)
compileOnly(libs.awssdk.cloudwatchlogs)
compileOnly(libs.awssdk.lambda)

// Tests
testAnnotationProcessor(mn.micronaut.inject.java)
Expand All @@ -41,6 +42,7 @@ dependencies {
testImplementation(libs.awssdk.sqs)
testImplementation(libs.awssdk.ssm)
testImplementation(libs.awssdk.rekognition)
testImplementation(libs.awssdk.lambda)
testRuntimeOnly(libs.jcl.over.slf4j)

testRuntimeOnly(mn.snakeyaml)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.aws.sdk.v2.service.lambda;

import io.micronaut.aws.sdk.v2.service.AWSServiceConfiguration;
import io.micronaut.aws.sdk.v2.service.AwsClientFactory;
import io.micronaut.aws.ua.UserAgentProvider;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Nullable;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.services.lambda.LambdaAsyncClient;
import software.amazon.awssdk.services.lambda.LambdaAsyncClientBuilder;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.LambdaClientBuilder;

/**
* Factory that creates {@link LambdaClient} and {@link LambdaAsyncClient}.
* @since 4.7.0
*/
@Factory
class LambdaClientFactory extends AwsClientFactory<LambdaClientBuilder, LambdaAsyncClientBuilder, LambdaClient, LambdaAsyncClient> {
/**
* Constructor.
*
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
* @param userAgentProvider User-Agent Provider
* @param awsServiceConfiguration AWS Service Configuration
*/
protected LambdaClientFactory(AwsCredentialsProviderChain credentialsProvider,
AwsRegionProviderChain regionProvider,
@Nullable UserAgentProvider userAgentProvider,
@Nullable @Named(LambdaClient.SERVICE_NAME) AWSServiceConfiguration awsServiceConfiguration) {
super(credentialsProvider, regionProvider, userAgentProvider, awsServiceConfiguration);
}

@Override
protected LambdaClientBuilder createSyncBuilder() {
return LambdaClient.builder();
}

@Override
protected LambdaAsyncClientBuilder createAsyncBuilder() {
return LambdaAsyncClient.builder();
}

@Override
@Singleton
public LambdaClientBuilder syncBuilder(SdkHttpClient httpClient) {
return super.syncBuilder(httpClient);
}

@Override
@Bean(preDestroy = "close")
@Singleton
public LambdaClient syncClient(LambdaClientBuilder builder) {
return super.syncClient(builder);
}

@Override
@Singleton
@Requires(beans = SdkAsyncHttpClient.class)
public LambdaAsyncClientBuilder asyncBuilder(SdkAsyncHttpClient httpClient) {
return super.asyncBuilder(httpClient);
}

@Override
@Bean(preDestroy = "close")
@Singleton
@Requires(beans = SdkAsyncHttpClient.class)
public LambdaAsyncClient asyncClient(LambdaAsyncClientBuilder builder) {
return super.asyncClient(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Lambda client factory.
* @author Luis Duarte
* @since 4.7.0
*/
@Requires(classes = {LambdaClient.class, LambdaAsyncClient.class})
@Configuration
package io.micronaut.aws.sdk.v2.service.lambda;

import io.micronaut.context.annotation.Configuration;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.services.lambda.LambdaAsyncClient;
import software.amazon.awssdk.services.lambda.LambdaClient;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.micronaut.aws.sdk.v2.service

import software.amazon.awssdk.services.lambda.LambdaAsyncClient
import software.amazon.awssdk.services.lambda.LambdaClient

class LambdaClientSpec extends ServiceClientSpec<LambdaClient, LambdaAsyncClient> {
@Override
protected String serviceName() {
return LambdaClient.SERVICE_NAME
}

@Override
protected LambdaClient getClient() {
applicationContext.getBean(LambdaClient)
}

protected LambdaAsyncClient getAsyncClient() {
applicationContext.getBean(LambdaAsyncClient)
}
}
28 changes: 28 additions & 0 deletions function-client-aws-v2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("io.micronaut.build.internal.aws-module")
}

dependencies {
api(projects.micronautAwsSdkV2)
implementation(libs.awssdk.lambda)
implementation(mn.reactor)
api(mn.micronaut.function.client)
testAnnotationProcessor(mn.micronaut.inject.java)
testImplementation(mn.micronaut.inject.java)
testImplementation(mnSerde.micronaut.serde.api)
testImplementation(mn.micronaut.http.server.netty)
testImplementation(mn.micronaut.function.web)
testImplementation(mnGroovy.micronaut.function.groovy)
testImplementation(mnGroovy.micronaut.runtime.groovy)
testImplementation(platform(mnTestResources.boms.testcontainers))
testImplementation(libs.testcontainers)
testImplementation(libs.testcontainers.localstack)
testImplementation(libs.testcontainers.spock)
testImplementation(libs.awssdk.iam)
}
micronautBuild {
// new module, so no binary check
binaryCompatibility {
enabled.set(false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.function.client.aws.v2;

import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.function.client.FunctionDefinition;

/**
* Builds an {@link AwsInvokeRequestDefinition} for each definition under {@code aws.lambda.functions}.
*
* @since 4.7.0
*/
@EachProperty(AwsInvokeRequestDefinition.AWS_LAMBDA_FUNCTIONS)
public class AwsInvokeRequestDefinition implements FunctionDefinition {
/**
* Configuration prefix.
*/
public static final String AWS_LAMBDA_FUNCTIONS = "aws.lambda.functions";

private final String name;

private String functionName;

private String qualifier;

private String clientContext;

/**
* Constructor.
*
* @param name configured name from a property
*/
public AwsInvokeRequestDefinition(@Parameter String name) {
this.name = name;
}

@Override
public String getName() {
return this.name;
}

/**
*
* @return The name or ARN of the Lambda function, version, or alias.
*/
public String getFunctionName() {
return functionName;
}

/**
*
* @param functionName The name or ARN of the Lambda function, version, or alias.
*/
public void setFunctionName(String functionName) {
this.functionName = functionName;
}

/**
*
* @return Specify a version or alias to invoke a published version of the function.
*/
public String getQualifier() {
return qualifier;
}

/**
* {@see software.amazon.awssdk.services.lambda.model.InvokeRequest#clientContext}.
* @return Up to 3,583 bytes of base64-encoded data about the invoking client to pass to the function in the context object.
*/
public String getClientContext() {
return clientContext;
}

/**
* {@see software.amazon.awssdk.services.lambda.model.InvokeRequest#qualifier}.
* @param qualifier Specify a version or alias to invoke a published version of the function.
*/
public void setQualifier(String qualifier) {
this.qualifier = qualifier;
}

/**
*
* @param clientContext Up to 3,583 bytes of base64-encoded data about the invoking client to pass to the function in the context object.
*/
public void setClientContext(String clientContext) {
this.clientContext = clientContext;
}
}
Loading

0 comments on commit b1389bb

Please sign in to comment.