Skip to content

Commit 90a86c5

Browse files
authored
feat: Add AppConfig provider to parameters module (#1104)
1 parent 0de3034 commit 90a86c5

File tree

15 files changed

+801
-2
lines changed

15 files changed

+801
-2
lines changed

docs/utilities/parameters.md

+43
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@ a `DynamoDbProvider` providing a client if you need to configure it yourself.
214214
}
215215
```
216216

217+
## AppConfig
218+
To get parameters stored in AppConfig, use `getAppConfigProvider`, providing the application and environment
219+
name to retrieve configuration from. As with the other providers, an overloaded method allows you to retrieve
220+
an `AppConfigProvider` providing a client if you need to configure it yourself.
221+
222+
=== "AppConfigProvider"
223+
224+
```java hl_lines="6 9"
225+
import software.amazon.lambda.powertools.parameters.AppConfigProvider;
226+
import software.amazon.lambda.powertools.parameters.ParamManager;
227+
228+
public class AppWitAppConfigParameters implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
229+
// Get an instance of the AppConfigProvider
230+
AppConfigProvider appConfigProvider = ParamManager.getAppConfigProvider("my-environment", "my-app");
231+
232+
// Retrieve a single parameter
233+
String value = appConfigProvider.get("my-key");
234+
}
235+
```
236+
237+
=== "AppConfigProvider with a custom client"
238+
239+
```java hl_lines="9 10 11 12 15 18"
240+
import software.amazon.lambda.powertools.parameters.AppConfigProvider;
241+
import software.amazon.lambda.powertools.parameters.ParamManager;
242+
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
243+
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
244+
import software.amazon.awssdk.regions.Region;
245+
246+
public class AppWithDynamoDbParameters implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
247+
// Get an AppConfig Client with an explicit region
248+
AppConfigDataClient appConfigDataClient = AppConfigDataClient.builder()
249+
.httpClientBuilder(UrlConnectionHttpClient.builder())
250+
.region(Region.EU_CENTRAL_2)
251+
.build();
252+
253+
// Get an instance of the DynamoDbProvider
254+
AppConfigProvider appConfigProvider = ParamManager.getAppConfigProvider(appConfigDataClient, "my-environment", "my-app");
255+
256+
// Retrieve a single parameter
257+
String value = appConfigProvider.get("my-key");
258+
}
259+
```
217260

218261

219262
## Advanced configuration

powertools-e2e-tests/handlers/metrics/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<artifactId>e2e-test-handler-metrics</artifactId>
1212
<packaging>jar</packaging>
13-
<name>A Lambda function using powertools metrics</name>
13+
<name>A Lambda function using Powertools for AWS Lambda (Java) Parameters</name>
1414

1515
<dependencies>
1616
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-parameters</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using powertools logging</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-logging</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>software.amazon.lambda</groupId>
22+
<artifactId>powertools-parameters</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.amazonaws</groupId>
26+
<artifactId>aws-lambda-java-events</artifactId>
27+
</dependency>
28+
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>aspectj-maven-plugin</artifactId>
36+
<configuration>
37+
<source>${maven.compiler.source}</source>
38+
<target>${maven.compiler.target}</target>
39+
<complianceLevel>${maven.compiler.target}</complianceLevel>
40+
<aspectLibraries>
41+
<aspectLibrary>
42+
<groupId>software.amazon.lambda</groupId>
43+
<artifactId>powertools-logging</artifactId>
44+
</aspectLibrary>
45+
</aspectLibraries>
46+
</configuration>
47+
<executions>
48+
<execution>
49+
<goals>
50+
<goal>compile</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-shade-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import software.amazon.lambda.powertools.logging.Logging;
8+
import software.amazon.lambda.powertools.parameters.ParamManager;
9+
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
10+
import software.amazon.lambda.powertools.parameters.AppConfigProvider;
11+
12+
public class Function implements RequestHandler<Input, String> {
13+
14+
private static final Logger LOG = LogManager.getLogger(Function.class);
15+
16+
@Logging
17+
public String handleRequest(Input input, Context context) {
18+
AppConfigProvider provider = ParamManager.getAppConfigProvider(input.getEnvironment(), input.getApp());
19+
return provider.get(input.getKey());
20+
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import java.util.Map;
4+
5+
public class Input {
6+
7+
private String app;
8+
private String environment;
9+
private String key;
10+
public void setApp(String app) {
11+
this.app = app;
12+
}
13+
14+
public void setEnvironment(String environment) {
15+
this.environment = environment;
16+
}
17+
18+
public void setKey(String key) {
19+
this.key = key;
20+
}
21+
22+
public String getApp() {
23+
return app;
24+
}
25+
26+
public String getEnvironment() {
27+
return environment;
28+
}
29+
30+
public String getKey() {
31+
return key;
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Root>
12+
<Logger name="JsonLogger" level="INFO" additivity="false">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Logger>
15+
</Loggers>
16+
</Configuration>

powertools-e2e-tests/handlers/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<module>tracing</module>
2828
<module>metrics</module>
2929
<module>idempotency</module>
30+
<module>parameters</module>
3031
</modules>
3132

3233
<dependencyManagement>
@@ -51,6 +52,11 @@
5152
<artifactId>powertools-idempotency</artifactId>
5253
<version>${lambda.powertools.version}</version>
5354
</dependency>
55+
<dependency>
56+
<groupId>software.amazon.lambda</groupId>
57+
<artifactId>powertools-parameters</artifactId>
58+
<version>${lambda.powertools.version}</version>
59+
</dependency>
5460
<dependency>
5561
<groupId>com.amazonaws</groupId>
5662
<artifactId>aws-lambda-java-core</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package software.amazon.lambda.powertools;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.jupiter.api.*;
5+
import software.amazon.lambda.powertools.testutils.AppConfig;
6+
import software.amazon.lambda.powertools.testutils.Infrastructure;
7+
import software.amazon.lambda.powertools.testutils.lambda.InvocationResult;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static software.amazon.lambda.powertools.testutils.lambda.LambdaInvoker.invokeFunction;
17+
18+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
19+
public class ParametersE2ET {
20+
21+
22+
private final ObjectMapper objectMapper = new ObjectMapper();
23+
24+
private Infrastructure infrastructure;
25+
private String functionName;
26+
private final AppConfig appConfig;
27+
28+
public ParametersE2ET() {
29+
Map<String,String> params = new HashMap<>();
30+
params.put("key1", "value1");
31+
params.put("key2", "value2");
32+
appConfig = new AppConfig("e2eApp", "e2etest", params);
33+
}
34+
@BeforeAll
35+
@Timeout(value = 5, unit = TimeUnit.MINUTES)
36+
public void setup() {
37+
infrastructure = Infrastructure.builder()
38+
.testName(ParametersE2ET.class.getSimpleName())
39+
.pathToFunction("parameters")
40+
.appConfig(appConfig)
41+
.environmentVariables(
42+
Stream.of(new String[][]{
43+
{"POWERTOOLS_LOG_LEVEL", "INFO"},
44+
{"POWERTOOLS_SERVICE_NAME", ParametersE2ET.class.getSimpleName()}
45+
})
46+
.collect(Collectors.toMap(data -> data[0], data -> data[1])))
47+
.build();
48+
functionName = infrastructure.deploy();
49+
}
50+
51+
@AfterAll
52+
public void tearDown() {
53+
if (infrastructure != null)
54+
infrastructure.destroy();
55+
}
56+
57+
@Test
58+
public void test_getAppConfigValue() {
59+
for (Map.Entry<String, String >configKey: appConfig.getConfigurationValues().entrySet()) {
60+
61+
// Arrange
62+
String event1 = "{" +
63+
"\"app\": \"" + appConfig.getApplication() + "\", " +
64+
"\"environment\": \"" + appConfig.getEnvironment() + "\", " +
65+
"\"key\": \"" + configKey.getKey() + "\"" +
66+
"}";
67+
68+
// Act
69+
InvocationResult invocationResult = invokeFunction(functionName, event1);
70+
71+
// Assert
72+
assertThat(invocationResult.getResult()).isEqualTo("\"" + configKey.getValue() + "\"");
73+
}
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package software.amazon.lambda.powertools.testutils;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Defines configuration used to setup an AppConfig
8+
* deployment when the infrastructure is rolled out.
9+
*
10+
* All fields are non-nullable.
11+
*/
12+
public class AppConfig {
13+
private String application;
14+
private String environment;
15+
private Map<String, String> configurationValues;
16+
17+
public AppConfig(String application, String environment, Map<String, String> configurationValues) {
18+
this.application = application;
19+
this.environment = environment;
20+
this.configurationValues = configurationValues;
21+
}
22+
23+
public String getApplication() {
24+
return application;
25+
}
26+
27+
public String getEnvironment() {
28+
return environment;
29+
}
30+
31+
public Map<String, String> getConfigurationValues() {
32+
return configurationValues;
33+
}
34+
}

0 commit comments

Comments
 (0)