Skip to content

Instantly share code, notes, and snippets.

@cmsd2
Created November 11, 2019 16:25
Show Gist options
  • Save cmsd2/cc41b8c1f8c8162c813cb576fed31b6e to your computer and use it in GitHub Desktop.
Save cmsd2/cc41b8c1f8c8162c813cb576fed31b6e to your computer and use it in GitHub Desktop.
lambda CDK stack with canary rollout using CodeDeploy
package com.example;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Duration;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.iam.User;
import software.amazon.awscdk.services.iam.Role;
import software.amazon.awscdk.services.iam.RoleProps;
import software.amazon.awscdk.services.iam.ServicePrincipal;
import software.amazon.awscdk.services.lambda.Alias;
import software.amazon.awscdk.services.lambda.AliasProps;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.FunctionProps;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awscdk.services.lambda.Version;
import software.amazon.awscdk.services.lambda.VersionProps;
import software.amazon.awscdk.services.codedeploy.LambdaApplication;
import software.amazon.awscdk.services.codedeploy.LambdaDeploymentConfig;
import software.amazon.awscdk.services.codedeploy.LambdaDeploymentGroup;
import software.amazon.awscdk.services.codedeploy.LambdaDeploymentGroupProps;
public class ExampleLambdaStack extends Stack {
public ExampleLambdaStack(final Construct parent, final String id, String lambdaJarChecksum) {
this(parent, id, lambdaJarChecksum, null);
}
public ExampleLambdaStack(final Construct parent, final String id, String lambdaJarChecksum, final StackProps props) {
super(parent, id, props);
String path = "target/debug/HelloLambda_deploy.jar";
String klass = "com.example.helloLambda.Handler";
int memory = 128;
Runtime runtime = Runtime.JAVA_8;
String aliasName = "prod";
RoleProps roleProps = new RoleProps.Builder()
.assumedBy(new ServicePrincipal("lambda.amazonaws.com"))
.roleName(id)
.build();
Role role = new Role(this, id + "-role", roleProps);
FunctionProps functionProps = new FunctionProps.Builder()
.code(Code.fromAsset(path))
.functionName(id)
.runtime(runtime)
.handler(klass)
.memorySize(memory)
.role(role)
.build();
Function function = new Function(this, id + "-function", functionProps);
Version version = function.addVersion(lambdaJarChecksum); // not using checksum for validation, just stack id
AliasProps aliasProps = new AliasProps.Builder()
.aliasName(aliasName)
.version(version)
.build();
Alias alias = new Alias(this, id + "-function-alias", aliasProps);
LambdaApplication app = new LambdaApplication(this, id + "-codedeploy-application");
LambdaDeploymentGroupProps groupProps = new LambdaDeploymentGroupProps.Builder()
.alias(alias)
.application(app)
.deploymentConfig(LambdaDeploymentConfig.CANARY_10_PERCENT_5_MINUTES)
.deploymentGroupName(id)
.build();
LambdaDeploymentGroup group = new LambdaDeploymentGroup(this, id + "-codedeploy-deployment-group", groupProps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment