Skip to content

feature: Idempotency module #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4440dce
Move JSON config to a new utility module
jeromevdl Jan 24, 2022
08c575b
Use new utility module for JSON config
jeromevdl Jan 24, 2022
03999db
Idempotency module
jeromevdl Jan 24, 2022
b1d0262
Idempotency module example
jeromevdl Jan 24, 2022
2ac9f65
Idempotency module added to lambda-powertools
jeromevdl Jan 24, 2022
4ed575d
solve JDK 16 build
jeromevdl Jan 24, 2022
a8e0ff5
refactoring to simplify 'around' method
jeromevdl Jan 24, 2022
8ec3b90
solve javadoc warnings
jeromevdl Jan 24, 2022
a4db313
PR #717: document exceptions
jeromevdl Feb 2, 2022
9d29528
PR #717: move cache to utilities
jeromevdl Feb 2, 2022
2aa583e
PR #717: native libs for dynamo & sqlite
jeromevdl Feb 2, 2022
92fafe6
PR #717: changes post review
jeromevdl Feb 2, 2022
c284584
PR #717: add modules to GitHub workflows
jeromevdl Feb 2, 2022
b2ec97d
PR #717: fix utilities dependency
jeromevdl Feb 2, 2022
32fd1e5
PR #717: fix utilities dependency
jeromevdl Feb 2, 2022
a48a5d3
PR #717: fix utilities dependency
jeromevdl Feb 2, 2022
1803bb9
PR #717: upgrade utilities and idempotency version to 1.10.3
jeromevdl Feb 2, 2022
cd71b5f
PR #717: end to end test with a handler
jeromevdl Feb 2, 2022
3245ba6
PR #717: upgrade version in example
jeromevdl Feb 3, 2022
69521ec
PR #717: expiration in seconds instead of ms
jeromevdl Feb 3, 2022
96eadc0
PR #717: new test
jeromevdl Feb 3, 2022
d302f7a
adding documentation
jeromevdl Feb 7, 2022
f713c3c
removing unused dependencies
jeromevdl Feb 7, 2022
38522bb
sqlite dependency for Mac M1
jeromevdl Feb 14, 2022
7401f22
update doc for sqlite dependency on M1
jeromevdl Feb 14, 2022
c39d3a9
change expiration to duration
jeromevdl Feb 14, 2022
4855a20
update doc for utilities
jeromevdl Feb 14, 2022
81a7052
PR #717: rename utilities into serialization
jeromevdl Feb 16, 2022
cefd1f7
PR #717: move back LRU into idempotency module
jeromevdl Feb 16, 2022
1550cc4
PR #717: local cache disabled by default
jeromevdl Feb 16, 2022
221855e
PR #717: update documentation
jeromevdl Feb 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ on:
paths:
- 'powertools-cloudformation/**'
- 'powertools-core/**'
- 'powertools-utilities/**'
- 'powertools-logging/**'
- 'powertools-sqs/**'
- 'powertools-tracing/**'
- 'powertools-validation/**'
- 'powertools-idempotency/**'
- 'powertools-parameters/**'
- 'powertools-metrics/**'
- 'powertools-test-suite/**'
Expand All @@ -22,10 +24,12 @@ on:
paths:
- 'powertools-cloudformation/**'
- 'powertools-core/**'
- 'powertools-utilities/**'
- 'powertools-logging/**'
- 'powertools-sqs/**'
- 'powertools-tracing/**'
- 'powertools-validation/**'
- 'powertools-idempotency/**'
- 'powertools-parameters/**'
- 'powertools-metrics/**'
- 'powertools-test-suite/**'
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/spotbugs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ on:
paths:
- 'powertools-cloudformation/**'
- 'powertools-core/**'
- 'powertools-utilities/**'
- 'powertools-logging/**'
- 'powertools-sqs/**'
- 'powertools-tracing/**'
- 'powertools-validation/**'
- 'powertools-parameters/**'
- 'powertools-idempotency/**'
- 'powertools-metrics/**'
- 'powertools-test-suite/**'
- 'pom.xml'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hs_err_pid*

# Maven build
target/
native-libs/

######################
# IntelliJ
Expand Down
6 changes: 5 additions & 1 deletion example/HelloWorldFunction/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins{
}

repositories {
mavenLocal()
mavenCentral()
}

Expand All @@ -15,8 +16,11 @@ dependencies {
aspect 'software.amazon.lambda:powertools-parameters:1.10.3'
aspect 'software.amazon.lambda:powertools-validation:1.10.3'

implementation 'software.amazon.lambda:powertools-idempotency:1.10.2'
aspectpath 'software.amazon.lambda:powertools-idempotency:1.10.2'

implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
implementation 'com.amazonaws:aws-lambda-java-events:3.1.0'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'

implementation 'org.apache.logging.log4j:log4j-api:2.16.0'
implementation 'org.apache.logging.log4j:log4j-core:2.16.0'
Expand Down
7 changes: 6 additions & 1 deletion example/HelloWorldFunction/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>powertools-sqs</artifactId>
<version>1.10.3</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-idempotency</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
Expand All @@ -51,7 +56,7 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.1.0</version>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package helloworld;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.powertools.idempotency.Idempotency;
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig;
import software.amazon.lambda.powertools.idempotency.Idempotent;
import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore;
import software.amazon.lambda.powertools.utilities.JsonConfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class AppIdempotency implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
Copy link
Contributor

@pankajagrawal16 pankajagrawal16 Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid anything new to the examples project. see #726.

it will be great to have a working app with demo showcased here instead https://github.com/aws-samples/aws-lambda-powertools-examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can i just let it here and also add it to the other repo, now that it's done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if you want to leave it here for now thats fine, as long as its added in the example repo too. Eventually examples folder will be gone from the project. We are tracking this against this issue #732

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private final static Logger LOG = LogManager.getLogger();

public AppIdempotency() {
// we need to initialize idempotency configuration before the handleRequest method is called
Idempotency.config().withConfig(
IdempotencyConfig.builder()
.withEventKeyJMESPath("powertools_json(body).address")
.build())
.withPersistenceStore(
DynamoDBPersistenceStore.builder()
.withTableName("idempotency_table")
.build()
).configure();
}


/**
* Try with:
* <pre>
* curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}'
* </pre>
* @param input
* @param context
* @return
*/
@Idempotent
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
Map<String, String> headers = new HashMap<>();

headers.put("Content-Type", "application/json");
headers.put("Access-Control-Allow-Origin", "*");
headers.put("Access-Control-Allow-Methods", "GET, OPTIONS");
headers.put("Access-Control-Allow-Headers", "*");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
String address = JsonConfig.get().getObjectMapper().readTree(input.getBody()).get("address").asText();
final String pageContents = this.getPageContents(address);
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

LOG.debug("ip is {}", pageContents);
return response
.withStatusCode(200)
.withBody(output);

} catch (IOException e) {
return response
.withBody("{}")
.withStatusCode(500);
}
}

// we could actually also put the @Idempotent annotation here
private String getPageContents(String address) throws IOException {
URL url = new URL(address);
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}
35 changes: 35 additions & 0 deletions example/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ Resources:
Path: /helloparams
Method: get

IdempotencyTable:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: idempotency_table
PrimaryKey:
Name: id
Type: String

HelloWorldIdempotentFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.AppIdempotency::handleRequest
MemorySize: 512
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
POWERTOOLS_LOG_LEVEL: INFO
AWS_ENDPOINT_DISCOVERY_ENABLED: false
Tracing: Active
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref IdempotencyTable
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /helloidem
Method: post

UserPwd:
Type: AWS::SecretsManager::Secret
Properties:
Expand Down Expand Up @@ -171,3 +200,9 @@ Outputs:
Description: "Hello World Params Lambda Function ARN"
Value: !GetAtt HelloWorldParamsFunction.Arn

HelloWorldIdempotencyApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World idempotency function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/helloidem/"
HelloWorldIdempotencyFunction:
Description: "Hello World Idempotency Lambda Function ARN"
Value: !GetAtt HelloWorldIdempotentFunction.Arn
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<modules>
<module>powertools-core</module>
<module>powertools-utilities</module>
<module>powertools-logging</module>
<module>powertools-tracing</module>
<module>powertools-sqs</module>
Expand All @@ -36,6 +37,7 @@
<module>powertools-validation</module>
<module>powertools-test-suite</module>
<module>powertools-cloudformation</module>
<module>powertools-idempotency</module>
</modules>

<scm>
Expand Down Expand Up @@ -90,6 +92,11 @@
<artifactId>powertools-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-utilities</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
Expand Down
Loading