Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-40195 implemented dead letter queue functionality
  • Loading branch information
cast committed Jan 2, 2017
1 parent 243a5f3 commit 5836396
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -45,7 +45,7 @@
</developers>

<properties>
<aws.version>1.11.58</aws.version>
<aws.version>1.11.64</aws.version>
<jsonpath.version>2.0.0</jsonpath.version>
</properties>

Expand Down
Expand Up @@ -161,13 +161,17 @@ private String createLambdaFunction(DeployConfig config, FunctionCode functionCo
.withEnvironment(new Environment().withVariables(config.getEnvironmentVariables()))
.withKMSKeyArn(config.getKmsArn());

if(config.getSubnets().size() > 0 && config.getSecurityGroups().size() > 0){
if(config.getSubnets() != null && config.getSubnets().size() > 0 && config.getSecurityGroups() != null && config.getSecurityGroups().size() > 0){
VpcConfig vpcConfig = new VpcConfig()
.withSubnetIds(config.getSubnets())
.withSecurityGroupIds(config.getSecurityGroups());
createFunctionRequest.withVpcConfig(vpcConfig);
}

if(config.getDeadLetterQueueArn() != null && config.getDeadLetterQueueArn().length() > 0){
createFunctionRequest.setDeadLetterConfig(new DeadLetterConfig().withTargetArn(config.getDeadLetterQueueArn()));
}

logger.log("Lambda create function request:%n%s%n", createFunctionRequest.toString());

CreateFunctionResult uploadFunctionResult = client.createFunction(createFunctionRequest);
Expand Down Expand Up @@ -264,13 +268,17 @@ private String updateConfigurationOnly(DeployConfig config){
.withEnvironment(new Environment().withVariables(config.getEnvironmentVariables()))
.withKMSKeyArn(config.getKmsArn());

if(config.getSubnets().size() > 0 && config.getSecurityGroups().size() > 0){
if(config.getSubnets() != null && config.getSubnets().size() > 0 && config.getSecurityGroups() != null && config.getSecurityGroups().size() > 0){
VpcConfig vpcConfig = new VpcConfig()
.withSubnetIds(config.getSubnets())
.withSecurityGroupIds(config.getSecurityGroups());
updateFunctionConfigurationRequest.withVpcConfig(vpcConfig);
}

if(config.getDeadLetterQueueArn() != null && config.getDeadLetterQueueArn().length() > 0){
updateFunctionConfigurationRequest.setDeadLetterConfig(new DeadLetterConfig().withTargetArn(config.getDeadLetterQueueArn()));
}

logger.log("Lambda update configuration request:%n%s%n", updateFunctionConfigurationRequest.toString());

UpdateFunctionConfigurationResult updateFunctionConfigurationResult = client.updateFunctionConfiguration(updateFunctionConfigurationRequest);
Expand Down
Expand Up @@ -21,6 +21,7 @@ public class DeployConfig implements Serializable {
private List<String> securityGroups;
private Map<String, String> environmentVariables;
private String kmsArn;
private String deadLetterQueueArn;

public DeployConfig(String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, String updateMode, boolean publish, String alias, boolean createAlias, List<String> subnets, List<String> securityGroups) {
this.artifactLocation = artifactLocation;
Expand Down Expand Up @@ -162,4 +163,12 @@ public String getKmsArn() {
public void setKmsArn(String kmsArn) {
this.kmsArn = kmsArn;
}

public String getDeadLetterQueueArn() {
return deadLetterQueueArn;
}

public void setDeadLetterQueueArn(String deadLetterQueueArn) {
this.deadLetterQueueArn = deadLetterQueueArn;
}
}
Expand Up @@ -67,6 +67,8 @@ public class LambdaUploadBuildStepVariables extends AbstractDescribableImpl<Lamb
private String subnets;
private String securityGroups;
private EnvironmentConfiguration environmentConfiguration;
private boolean enableDeadLetterQueue;
private String deadLetterQueueArn;

@DataBoundConstructor
public LambdaUploadBuildStepVariables(String awsRegion, String functionName, String updateMode){
Expand Down Expand Up @@ -253,6 +255,25 @@ public void setEnvironmentConfiguration(EnvironmentConfiguration environmentConf
this.environmentConfiguration = environmentConfiguration;
}

public boolean getEnableDeadLetterQueue() {
return enableDeadLetterQueue;
}

@DataBoundSetter
public void setEnableDeadLetterQueue(boolean enableDeadLetterQueue) {
this.enableDeadLetterQueue = enableDeadLetterQueue;
}

public String getDeadLetterQueueArn() {
return deadLetterQueueArn;
}

@DataBoundSetter
public void setDeadLetterQueueArn(String deadLetterQueueArn) {
this.deadLetterQueueArn = deadLetterQueueArn;
}


public void expandVariables(EnvVars env) {
awsAccessKeyId = ExpansionUtils.expand(awsAccessKeyId, env);
clearTextAwsSecretKey = ExpansionUtils.expand(Secret.toString(Secret.fromString(awsSecretKey)), env);
Expand All @@ -271,6 +292,7 @@ public void expandVariables(EnvVars env) {
if(environmentConfiguration != null){
environmentConfiguration.expandVariables(env);
}
deadLetterQueueArn = ExpansionUtils.expand(deadLetterQueueArn, env);
}

public LambdaUploadBuildStepVariables getClone(){
Expand All @@ -293,6 +315,8 @@ public LambdaUploadBuildStepVariables getClone(){
if(environmentConfiguration != null){
lambdaUploadBuildStepVariables.setEnvironmentConfiguration(environmentConfiguration.getClone());
}
lambdaUploadBuildStepVariables.setEnableDeadLetterQueue(enableDeadLetterQueue);
lambdaUploadBuildStepVariables.setDeadLetterQueueArn(deadLetterQueueArn);
return lambdaUploadBuildStepVariables;
}

Expand All @@ -306,6 +330,9 @@ public DeployConfig getUploadConfig(){
} else {
deployConfig.setEnvironmentVariables(new HashMap<String, String>());
}
if(enableDeadLetterQueue){
deployConfig.setDeadLetterQueueArn(deadLetterQueueArn);
}
return deployConfig;
}

Expand Down
Expand Up @@ -69,6 +69,8 @@ public class LambdaUploadVariables extends AbstractDescribableImpl<LambdaUploadV
private String subnets;
private String securityGroups;
private EnvironmentConfiguration environmentConfiguration;
private boolean enableDeadLetterQueue;
private String deadLetterQueueArn;

@DataBoundConstructor
public LambdaUploadVariables(String awsRegion, String functionName, String updateMode){
Expand Down Expand Up @@ -265,6 +267,24 @@ public void setEnvironmentConfiguration(EnvironmentConfiguration environmentConf
this.environmentConfiguration = environmentConfiguration;
}

public boolean getEnableDeadLetterQueue() {
return enableDeadLetterQueue;
}

@DataBoundSetter
public void setEnableDeadLetterQueue(boolean enableDeadLetterQueue) {
this.enableDeadLetterQueue = enableDeadLetterQueue;
}

public String getDeadLetterQueueArn() {
return deadLetterQueueArn;
}

@DataBoundSetter
public void setDeadLetterQueueArn(String deadLetterQueueArn) {
this.deadLetterQueueArn = deadLetterQueueArn;
}

public void expandVariables(EnvVars env) {
awsAccessKeyId = ExpansionUtils.expand(awsAccessKeyId, env);
clearTextAwsSecretKey = ExpansionUtils.expand(Secret.toString(Secret.fromString(awsSecretKey)), env);
Expand All @@ -283,6 +303,7 @@ public void expandVariables(EnvVars env) {
if(environmentConfiguration != null){
environmentConfiguration.expandVariables(env);
}
deadLetterQueueArn = ExpansionUtils.expand(deadLetterQueueArn, env);
}

public LambdaUploadVariables getClone(){
Expand All @@ -306,6 +327,8 @@ public LambdaUploadVariables getClone(){
if(environmentConfiguration != null){
lambdaUploadVariables.setEnvironmentConfiguration(environmentConfiguration.getClone());
}
lambdaUploadVariables.setEnableDeadLetterQueue(enableDeadLetterQueue);
lambdaUploadVariables.setDeadLetterQueueArn(deadLetterQueueArn);
return lambdaUploadVariables;
}

Expand All @@ -319,6 +342,9 @@ public DeployConfig getUploadConfig(){
} else {
deployConfig.setEnvironmentVariables(new HashMap<String, String>());
}
if(enableDeadLetterQueue){
deployConfig.setDeadLetterQueueArn(deadLetterQueueArn);
}
return deployConfig;
}

Expand Down
Expand Up @@ -60,6 +60,11 @@
<f:entry title="Security Groups" help="/plugin/aws-lambda/help-securityGroups.html">
<f:textbox name="securityGroups" field="securityGroups" />
</f:entry>
<f:optionalBlock title="Use dead letter queue" field="enableDeadLetterQueue" checked="${instance.enableDeadLetterQueue}" inline="true">
<f:entry title="target ARN" help="/plugin/aws-lambda/help-deadletterqueue.html">
<f:textbox name="deadLetterQueueArn" field="deadLetterQueueArn" />
</f:entry>
</f:optionalBlock>
</f:advanced>
</table>
</div>
Expand Down
Expand Up @@ -63,6 +63,11 @@
<f:entry title="Security Groups" help="/plugin/aws-lambda/help-securityGroups.html">
<f:textbox name="securityGroups" field="securityGroups" />
</f:entry>
<f:optionalBlock title="Use dead letter queue" field="enableDeadLetterQueue" checked="${instance.enableDeadLetterQueue}" inline="true">
<f:entry title="target ARN" help="/plugin/aws-lambda/help-deadletterqueue.html">
<f:textbox name="deadLetterQueueArn" field="deadLetterQueueArn" />
</f:entry>
</f:optionalBlock>
</f:advanced>

<f:entry title="">
Expand Down
Expand Up @@ -57,6 +57,11 @@
<f:entry title="Security Groups" help="/plugin/aws-lambda/help-securityGroups.html">
<f:textbox name="securityGroups" field="securityGroups" />
</f:entry>
<f:optionalBlock title="Use dead letter queue" field="enableDeadLetterQueue" checked="${instance.enableDeadLetterQueue}" inline="true">
<f:entry title="target ARN" help="/plugin/aws-lambda/help-deadletterqueue.html">
<f:textbox name="deadLetterQueueArn" field="deadLetterQueueArn" />
</f:entry>
</f:optionalBlock>
</f:advanced>
</table>
</div>
Expand Down
28 changes: 28 additions & 0 deletions src/main/webapp/help-deadletterqueue.html
@@ -0,0 +1,28 @@
<!--
#%L
AWS Lambda Upload Plugin
%%
Copyright (C) 2015 XT-i
%%
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#L%
-->
<div>
Target ARN of Amazon SQS queue or Amazon SNS topic. Add respectively sqs:SendMessage or sns:Publish as an allowed action on the target ARN in the role IAM policy.
</div>
Expand Up @@ -5,6 +5,7 @@
import com.amazonaws.services.lambda.model.CreateAliasResult;
import com.amazonaws.services.lambda.model.CreateFunctionRequest;
import com.amazonaws.services.lambda.model.CreateFunctionResult;
import com.amazonaws.services.lambda.model.DeadLetterConfig;
import com.amazonaws.services.lambda.model.Environment;
import com.amazonaws.services.lambda.model.FunctionCode;
import com.amazonaws.services.lambda.model.GetAliasRequest;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class LambdaDeployServiceTest {
private List<String> securityGroups;
private String kmsArn;
private Map<String, String> environment;
private String deadLetterQueueArn;

@Mock
private AWSLambdaClient awsLambdaClient;
Expand Down Expand Up @@ -100,6 +102,7 @@ public void setUp() throws Exception {
kmsArn = "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012";
environment = new HashMap<>();
environment.put("key", "value");
deadLetterQueueArn = "arn:aws:sqs:us-east-1:123456789012:queueName";
lambdaDeployService = new LambdaDeployService(awsLambdaClient, jenkinsLogger);
when(awsLambdaClient.updateFunctionConfiguration(any(UpdateFunctionConfigurationRequest.class)))
.thenReturn(new UpdateFunctionConfigurationResult());
Expand Down Expand Up @@ -484,7 +487,8 @@ private void calledUpdateConfiguration(Boolean called){
.withVpcConfig(subnets.size() > 0 || securityGroups.size() > 0 ? new VpcConfig().withSubnetIds(subnets).withSecurityGroupIds(securityGroups) : null)
.withTimeout(timeout)
.withKMSKeyArn(kmsArn)
.withEnvironment(new Environment().withVariables(environment));
.withEnvironment(new Environment().withVariables(environment))
.withDeadLetterConfig(new DeadLetterConfig().withTargetArn(deadLetterQueueArn));

assertEquals(expected, args.getValue());

Expand All @@ -510,7 +514,9 @@ private void calledCreateFunction(Boolean called) {
.withVpcConfig(subnets.size() > 0 || securityGroups.size() > 0 ? new VpcConfig().withSubnetIds(subnets).withSecurityGroupIds(securityGroups) : null)
.withCode(new FunctionCode().withZipFile(ByteBuffer.wrap(FileUtils.readFileToByteArray(getZipFile()))))
.withKMSKeyArn(kmsArn)
.withEnvironment(new Environment().withVariables(environment));
.withEnvironment(new Environment().withVariables(environment))
.withDeadLetterConfig(new DeadLetterConfig().withTargetArn(deadLetterQueueArn));

assertEquals(expected, args.getValue());

} catch (IOException e) {
Expand Down Expand Up @@ -555,6 +561,7 @@ private DeployConfig getDeployConfig(){
DeployConfig deployConfig = new DeployConfig(null, description, functionName, handler, memory, role, runtime, timeout, null, false, null, false, subnets, securityGroups);
deployConfig.setKmsArn(kmsArn);
deployConfig.setEnvironmentVariables(environment);
deployConfig.setDeadLetterQueueArn(deadLetterQueueArn);
return deployConfig;
}

Expand Down

0 comments on commit 5836396

Please sign in to comment.