in code/java/tests-workshop/src/main/java/com/myorg/HitCounter.java [22:58]
public HitCounter(final Construct scope, final String id, final HitCounterProps props) throws RuntimeException {
super(scope, id);
if (props.getReadCapacity() != null) {
if (props.getReadCapacity().intValue() < 5 || props.getReadCapacity().intValue() > 20) {
throw new RuntimeException("readCapacity must be greater than 5 or less than 20");
}
}
Number readCapacity = (props.getReadCapacity() == null) ? 5 : props.getReadCapacity();
this.table = Table.Builder.create(this, "Hits")
.partitionKey(Attribute.builder()
.name("path")
.type(AttributeType.STRING)
.build())
.encryption(TableEncryption.AWS_MANAGED)
.readCapacity(readCapacity)
.build();
final Map<String, String> environment = new HashMap<>();
environment.put("DOWNSTREAM_FUNCTION_NAME", props.getDownstream().getFunctionName());
environment.put("HITS_TABLE_NAME", this.table.getTableName());
this.handler = Function.Builder.create(this, "HitCounterHandler")
.runtime(Runtime.NODEJS_14_X)
.handler("hitcounter.handler")
.code(Code.fromAsset("lambda"))
.environment(environment)
.build();
// Grants the lambda function read/write permissions to our table
this.table.grantReadWriteData(this.handler);
// Grants the lambda function invoke permissions to the downstream function
props.getDownstream().grantInvoke(this.handler);
}