Skip to content

Commit

Permalink
feat: Allow riff-raff.yaml auto-generation to work with managed lambd…
Browse files Browse the repository at this point in the history
…a deploys (#1838)

* use bucketNamePath

* managed deployment doesn't need upload

* update generated rr lambda deploys to pay attention to location prefix

* move null coalesce into default params

Co-authored-by: Akash Askoolum <[email protected]>

* pull s3 location props out into discrete interface

Co-authored-by: Akash Askoolum <[email protected]>

* describe managedDeployment parameter

Co-authored-by: Akash Askoolum <[email protected]>

* adds test for managedDeploy rr yaml generation

* Rename managedDeployment -> withoutArtifactUpload

Co-authored-by: Akash Askoolum <[email protected]>

* fix unexpected assignment syntx

---------

Co-authored-by: Akash Askoolum <[email protected]>
  • Loading branch information
kenoir and akash1810 authored May 3, 2023
1 parent 34fc8bf commit e2a2a69
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/constructs/lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export interface GuFunctionProps extends GuDistributable, Omit<FunctionProps, "c
* accounts.
*/
withoutFilePrefix?: boolean;
/**
* Set to `true` this informs consumers of this function that upload is
* managed elsewhere by DevX.
*
* This is used by RiffRaffYamlFileExperimental to skip generating
* an uploadLambda step.
*/
withoutArtifactUpload?: boolean;
}

function defaultMemorySize(runtime: Runtime, memorySize?: number): number {
Expand Down Expand Up @@ -92,9 +100,21 @@ function defaultMemorySize(runtime: Runtime, memorySize?: number): number {
export class GuLambdaFunction extends Function {
public readonly app: string;
public readonly fileName: string;
public readonly bucketNamePath: string | undefined;
public readonly withoutArtifactUpload: boolean;
public readonly withoutFilePrefix: boolean;

constructor(scope: GuStack, id: string, props: GuFunctionProps) {
const { app, fileName, runtime, memorySize, timeout, bucketNamePath, withoutFilePrefix } = props;
const {
app,
fileName,
runtime,
memorySize,
timeout,
bucketNamePath,
withoutFilePrefix = false,
withoutArtifactUpload = false,
} = props;

const bucketName = bucketNamePath
? StringParameter.fromStringParameterName(scope, "bucketoverride", bucketNamePath).stringValue
Expand Down Expand Up @@ -122,6 +142,9 @@ export class GuLambdaFunction extends Function {

this.app = app;
this.fileName = fileName;
this.bucketNamePath = bucketNamePath;
this.withoutArtifactUpload = withoutArtifactUpload;
this.withoutFilePrefix = withoutFilePrefix;

if (props.errorPercentageMonitoring) {
new GuLambdaErrorPercentageAlarm(scope, `${id}-ErrorPercentageAlarmForLambda`, {
Expand Down
30 changes: 28 additions & 2 deletions src/experimental/riff-raff-yaml-file/deployments/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import type { GuStack } from "../../../constructs/core";
import type { GuLambdaFunction } from "../../../constructs/lambda";
import type { RiffRaffDeployment } from "../types";

interface S3LocationProps {
bucketSsmLookup?: boolean;
bucketSsmKey?: string;
prefixStackToKey?: boolean;
prefixAppToKey?: boolean;
prefixStageToKey?: boolean;
}

const locationProps = (lambda: GuLambdaFunction): S3LocationProps => {
const bucketProp =
lambda.bucketNamePath === undefined
? {
bucketSsmLookup: true,
}
: {
bucketSsmKey: lambda.bucketNamePath,
};

return {
...bucketProp,
prefixStackToKey: !lambda.withoutFilePrefix,
prefixAppToKey: !lambda.withoutFilePrefix,
prefixStageToKey: !lambda.withoutFilePrefix,
};
};

export function uploadLambdaArtifact(lambda: GuLambdaFunction): RiffRaffDeployment {
const { app, fileName } = lambda;
const { stack, region } = lambda.stack as GuStack;
Expand All @@ -15,7 +41,7 @@ export function uploadLambdaArtifact(lambda: GuLambdaFunction): RiffRaffDeployme
app,
contentDirectory: app,
parameters: {
bucketSsmLookup: true,
...locationProps(lambda),
lookupByTags: true,
fileName,
},
Expand All @@ -40,7 +66,7 @@ export function updateLambdaDeployment(
app,
contentDirectory: app,
parameters: {
bucketSsmLookup: true,
...locationProps(lambda),
lookupByTags: true,
fileName,
},
Expand Down
80 changes: 80 additions & 0 deletions src/experimental/riff-raff-yaml-file/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Runtime } from "aws-cdk-lib/aws-lambda";
import { AccessScope } from "../../constants";
import type { GuStackProps } from "../../constructs/core";
import { GuStack } from "../../constructs/core";
import { GuLambdaFunction } from "../../constructs/lambda";
import { GuEc2App, GuNodeApp, GuPlayApp, GuScheduledLambda } from "../../patterns";
import { RiffRaffYamlFileExperimental } from "./index";

Expand Down Expand Up @@ -343,6 +344,9 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-artifact.zip
actions:
Expand Down Expand Up @@ -370,6 +374,70 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-artifact.zip
actions:
- updateLambda
dependencies:
- cfn-eu-west-1-test-my-application-stack
"
`);
});

it("Should not create an uploadLambda step when withoutArtifactUpload is true", () => {
const app = new App({ outdir: "/tmp/cdk.out" });

class MyApplicationStack extends GuStack {
// eslint-disable-next-line custom-rules/valid-constructors -- unit testing
constructor(app: App, id: string, props: GuStackProps) {
super(app, id, props);

new GuLambdaFunction(this, "test", {
app: "my-lambda",
withoutArtifactUpload: true,
runtime: Runtime.NODEJS_16_X,
fileName: "my-lambda-artifact.zip",
handler: "handler.main",
timeout: Duration.minutes(1),
});
}
}

new MyApplicationStack(app, "test-stack", { stack: "test", stage: "TEST", env: { region: "eu-west-1" } });

const actual = new RiffRaffYamlFileExperimental(app).toYAML();

expect(actual).toMatchInlineSnapshot(`
"allowedStages:
- TEST
deployments:
cfn-eu-west-1-test-my-application-stack:
type: cloud-formation
regions:
- eu-west-1
stacks:
- test
app: my-application-stack
contentDirectory: /tmp/cdk.out
parameters:
templateStagePaths:
TEST: test-stack.template.json
lambda-update-eu-west-1-test-my-lambda:
type: aws-lambda
stacks:
- test
regions:
- eu-west-1
app: my-lambda
contentDirectory: my-lambda
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-artifact.zip
actions:
Expand Down Expand Up @@ -535,6 +603,9 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda-app
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-app.zip
actions:
Expand Down Expand Up @@ -583,6 +654,9 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda-app
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-app.zip
actions:
Expand Down Expand Up @@ -614,6 +688,9 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda-app
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-app.zip
actions:
Expand Down Expand Up @@ -662,6 +739,9 @@ describe("The RiffRaffYamlFileExperimental class", () => {
contentDirectory: my-lambda-app
parameters:
bucketSsmLookup: true
prefixStackToKey: true
prefixAppToKey: true
prefixStageToKey: true
lookupByTags: true
fileName: my-lambda-app.zip
actions:
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/riff-raff-yaml-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class RiffRaffYamlFileExperimental {
const autoscalingGroups = this.getAutoScalingGroups(stack);

const artifactUploads: RiffRaffDeployment[] = [
lambdas.map(uploadLambdaArtifact),
lambdas.filter((_) => !_.withoutArtifactUpload).map(uploadLambdaArtifact),
autoscalingGroups.map(uploadAutoscalingArtifact),
].flat();
artifactUploads.forEach(({ name, props }) => deployments.set(name, props));
Expand Down
1 change: 1 addition & 0 deletions src/patterns/ec2-app/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ export class GuEc2App extends Construct {
runtime: Runtime.GO_1_X,
fileName: "deploy/INFRA/cognito-lambda/devx-cognito-lambda-amd64-v1.zip",
withoutFilePrefix: true,
withoutArtifactUpload: true,
bucketNamePath: NAMED_SSM_PARAMETER_PATHS.OrganisationDistributionBucket.path,
architecture: Architecture.X86_64,
environment: {
Expand Down

0 comments on commit e2a2a69

Please sign in to comment.