Skip to content

Commit

Permalink
SeanRoy#117 Fixed: ResourceConflictException while deploying lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
gergely.juhasz committed Sep 17, 2021
1 parent 48b9d5b commit 859ac34
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</contributors>

<properties>
<aws.version>1.11.271</aws.version>
<aws.version>1.12.68</aws.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -262,7 +262,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<version>3.6.1</version>
<executions>
<execution>
<id>default-descriptor</id>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/github/seanroy/plugins/AbstractLambdaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.amazonaws.services.lambda.model.GetFunctionRequest;
import com.amazonaws.services.lambda.model.GetFunctionResult;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeResult;
import com.amazonaws.services.s3.model.*;
Expand Down Expand Up @@ -293,6 +295,24 @@ void uploadJarToS3() throws Exception {
.withS3Key(fileName)
.withPublish(lambdaFunction.isPublish());
UpdateFunctionCodeResult updateFunctionCodeResult = lambdaClient.updateFunctionCode(updateFunctionRequest);

// wait until the UpdateFunctionCode finishes processing to avoid com.amazonaws.services.lambda.model.ResourceConflictException. See: https://docs.aws.amazon.com/lambda/latest/dg/functions-states.html
GetFunctionRequest getFunctionRequest = new GetFunctionRequest()
.withFunctionName(lambdaFunction.getFunctionName());
GetFunctionResult getFunctionResult = lambdaClient.getFunction(getFunctionRequest);

while (!getFunctionResult.getConfiguration().getState().equals("Active")
|| !getFunctionResult.getConfiguration().getLastUpdateStatus().equals("Successful")) {
try {
getLog().info(String.format("UpdateFunctionCode for %s is still processing <State: %s, LastUpdateStatus: %s>, waiting... ", lambdaFunction.getFunctionName(), getFunctionResult.getConfiguration().getState(), getFunctionResult.getConfiguration().getLastUpdateStatus()));
Thread.sleep(3000);
getFunctionResult = lambdaClient.getFunction(getFunctionRequest);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getLog().info("UpdateFunctionCode finished successfully for " + lambdaFunction.getFunctionName());

return lambdaFunction
.withVersion(updateFunctionCodeResult.getVersion())
.withFunctionArn(updateFunctionCodeResult.getFunctionArn());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/seanroy/utils/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public static String toJson(Object message) throws JsonProcessingException {
}

public static <T> T fromJson(String body) throws IOException {
return mapper.readValue(body, new TypeReference<List<LambdaFunction>>(){});
return (T) mapper.readValue(body, new TypeReference<List<LambdaFunction>>(){});
}
}

0 comments on commit 859ac34

Please sign in to comment.