Skip to content

Commit 497aec4

Browse files
authored
Expose LambdaDeployment.alias (#28)
1 parent 7bee461 commit 497aec4

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

packages/infra/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ In a CDK stack:
3434
+ import { LambdaDeployment } from '@seek/aws-codedeploy-infra';
3535
import { aws_lambda_nodejs } from 'aws-cdk-lib';
3636

37-
const lambdaFunction = new aws_lambda_nodejs.NodejsFunction(
38-
this,
39-
'MyLambdaFunction',
40-
{
41-
// ...
42-
},
43-
);
44-
45-
+ new LambdaDeployment(this, 'MyLambdaDeployment', { lambdaFunction });
37+
const lambdaFunction = new aws_lambda_nodejs.NodejsFunction(this, 'Function', {
38+
// ...
39+
});
40+
41+
+ const lambdaDeployment = new LambdaDeployment(this, 'Deployment', {
42+
+ lambdaFunction,
43+
+ });
44+
45+
- lambdaFunction.addEventSource(source);
46+
+ lambdaDeployment.alias.addEventSource(source);
4647
```
4748

4849
The `LambdaDeployment` construct creates a CodeDeploy application and deployment group for your Lambda function,

packages/infra/src/constructs/__snapshots__/lambdaDeployment.test.ts.snap

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,36 @@ exports[`returns expected CloudFormation stack 1`] = `
401401
},
402402
},
403403
},
404+
"LambdaFunctionAliasLiveSqsEventSourceQueue453DF2EA": {
405+
"Properties": {
406+
"EventSourceArn": "arn:aws:sqs:us-east-2:123456789012:queue",
407+
"FunctionName": {
408+
"Fn::Join": [
409+
"",
410+
[
411+
{
412+
"Fn::Select": [
413+
6,
414+
{
415+
"Fn::Split": [
416+
":",
417+
{
418+
"Ref": "LambdaFunctionAliasLive400E15E9",
419+
},
420+
],
421+
},
422+
],
423+
},
424+
":Live",
425+
],
426+
],
427+
},
428+
},
429+
"Type": "AWS::Lambda::EventSourceMapping",
430+
},
404431
"LambdaFunctionBF21E41F": {
405432
"DependsOn": [
433+
"LambdaFunctionServiceRoleDefaultPolicy32EEEE35",
406434
"LambdaFunctionServiceRoleC555A460",
407435
],
408436
"Properties": {
@@ -471,6 +499,33 @@ exports[`returns expected CloudFormation stack 1`] = `
471499
},
472500
"Type": "AWS::IAM::Role",
473501
},
502+
"LambdaFunctionServiceRoleDefaultPolicy32EEEE35": {
503+
"Properties": {
504+
"PolicyDocument": {
505+
"Statement": [
506+
{
507+
"Action": [
508+
"sqs:ReceiveMessage",
509+
"sqs:ChangeMessageVisibility",
510+
"sqs:GetQueueUrl",
511+
"sqs:DeleteMessage",
512+
"sqs:GetQueueAttributes",
513+
],
514+
"Effect": "Allow",
515+
"Resource": "arn:aws:sqs:us-east-2:123456789012:queue",
516+
},
517+
],
518+
"Version": "2012-10-17",
519+
},
520+
"PolicyName": "LambdaFunctionServiceRoleDefaultPolicy32EEEE35",
521+
"Roles": [
522+
{
523+
"Ref": "LambdaFunctionServiceRoleC555A460",
524+
},
525+
],
526+
},
527+
"Type": "AWS::IAM::Policy",
528+
},
474529
},
475530
"Rules": {
476531
"CheckBootstrapVersion": {

packages/infra/src/constructs/lambdaDeployment.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { App, Stack, assertions, aws_lambda } from 'aws-cdk-lib';
1+
import {
2+
App,
3+
Stack,
4+
assertions,
5+
aws_lambda,
6+
aws_lambda_event_sources,
7+
aws_sqs,
8+
} from 'aws-cdk-lib';
29

310
import { LambdaDeployment } from './lambdaDeployment';
411

@@ -20,8 +27,17 @@ it('returns expected CloudFormation stack', () => {
2027
runtime: aws_lambda.Runtime.NODEJS_20_X,
2128
});
2229

23-
// eslint-disable-next-line no-new
24-
new LambdaDeployment(stack, null, { lambdaFunction });
30+
const deployment = new LambdaDeployment(stack, null, { lambdaFunction });
31+
32+
const queue = aws_sqs.Queue.fromQueueArn(
33+
stack,
34+
'Queue',
35+
'arn:aws:sqs:us-east-2:123456789012:queue',
36+
);
37+
38+
const eventSource = new aws_lambda_event_sources.SqsEventSource(queue);
39+
40+
deployment.alias.addEventSource(eventSource);
2541

2642
const template = assertions.Template.fromStack(stack);
2743

packages/infra/src/constructs/lambdaDeployment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type LambdaDeploymentProps = {
1010
};
1111

1212
export class LambdaDeployment extends Construct {
13+
alias: Readonly<aws_lambda.Alias>;
14+
1315
constructor(
1416
scope: Construct,
1517
id: string | null,
@@ -23,6 +25,8 @@ export class LambdaDeployment extends Construct {
2325
description: 'The Lambda version currently receiving traffic',
2426
});
2527

28+
this.alias = alias;
29+
2630
const application = new aws_codedeploy.LambdaApplication(
2731
this,
2832
'CodeDeployLambdaApplication',

0 commit comments

Comments
 (0)