Skip to content

Commit

Permalink
fix: deprecation warnings (#41)
Browse files Browse the repository at this point in the history
* replace ec2.SubnetType.PRIVATE with ec2.SubnetType.PRIVATE_WITH_NAT

* changed securityGroup to securityGroups

* change how we set metric statistics and period

* yummy feed the linter
  • Loading branch information
maddocash authored Feb 10, 2022
1 parent 6699bb5 commit 7386401
Show file tree
Hide file tree
Showing 9 changed files with 714 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ export class SimpleAuthenticatedApiStack extends cdk.Stack {
// one, the default group created will add significant time to deploy and destroy
// steps in the build. This is not a problem IRL where the group will only be created
// once instead of being created and destroyed on every build.
const lambdaSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this,
"a-talis-cdk-constructs-build",
"sg-0ac646f0077b5ce03",
{
mutable: false,
}
);
const lambdaSecurityGroups = [
ec2.SecurityGroup.fromSecurityGroupId(
this,
"a-talis-cdk-constructs-build",
"sg-0ac646f0077b5ce03",
{
mutable: false,
}
),
];

/* const api = */ new AuthenticatedApi(this, "simple-authenticated-api", {
prefix,
Expand All @@ -53,7 +55,7 @@ export class SimpleAuthenticatedApiStack extends cdk.Stack {
alarmTopic,
vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_NAT },
securityGroup: lambdaSecurityGroup,
securityGroups: lambdaSecurityGroups,

persona: {
host: "staging-users.talis.com",
Expand Down
20 changes: 11 additions & 9 deletions examples/simple-lambda-worker/lib/simple-lambda-worker-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ export class SimpleLambdaWorkerStack extends cdk.Stack {
// one, the default group created will add significant time to deploy and destroy
// steps in the build. This is not a problem IRL where the group will only be created
// once instead of being created and destroyed on every build.
const lambdaSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this,
"a-talis-cdk-constructs-build",
"sg-0ac646f0077b5ce03",
{
mutable: false,
}
);
const lambdaSecurityGroups = [
ec2.SecurityGroup.fromSecurityGroupId(
this,
"a-talis-cdk-constructs-build",
"sg-0ac646f0077b5ce03",
{
mutable: false,
}
),
];

// In this example, and to aid integration tests, after successfully processing
// a message the lambda worker will send a new messages to an SQS queue.
Expand All @@ -79,7 +81,7 @@ export class SimpleLambdaWorkerStack extends cdk.Stack {
entry: "src/lambda/simple-worker.js",
handler: "simpleLambdaWorker",
memorySize: 1024,
securityGroup: lambdaSecurityGroup,
securityGroups: lambdaSecurityGroups,
timeout: cdk.Duration.seconds(30),
vpc: vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_NAT },
Expand Down
2 changes: 1 addition & 1 deletion lib/authenticated-api/authenticated-api-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface AuthenticatedApiProps {
description: string;
stageName: string;
routes: Array<RouteLambdaProps>;
securityGroup?: ec2.ISecurityGroup;
securityGroups?: Array<ec2.ISecurityGroup>;
vpc: ec2.IVpc;
vpcSubnets: ec2.SubnetSelection;

Expand Down
16 changes: 8 additions & 8 deletions lib/authenticated-api/authenticated-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class AuthenticatedApi extends cdk.Construct {
awsSdkConnectionReuse: true,
runtime: lambda.Runtime.NODEJS_14_X,
timeout: cdk.Duration.minutes(2),
securityGroup: props.securityGroup,
securityGroups: props.securityGroups,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
}
Expand Down Expand Up @@ -108,7 +108,7 @@ export class AuthenticatedApi extends cdk.Construct {
awsSdkConnectionReuse: true,
runtime: lambda.Runtime.NODEJS_14_X,
timeout: routeProps.lambdaProps.timeout,
securityGroup: props.securityGroup,
securityGroups: props.securityGroups,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
}
Expand Down Expand Up @@ -148,7 +148,9 @@ export class AuthenticatedApi extends cdk.Construct {
const durationThreshold = routeProps.lamdaDurationAlarmThreshold
? routeProps.lamdaDurationAlarmThreshold
: DEFAULT_LAMBDA_DURATION_THRESHOLD;
const durationMetric = routeLambda.metric("Duration");
const durationMetric = routeLambda
.metric("Duration")
.with({ period: cdk.Duration.minutes(1), statistic: "sum" });
const durationAlarm = new cloudwatch.Alarm(
this,
`${props.prefix}${props.name}-${routeProps.name}-duration-alarm`,
Expand All @@ -161,8 +163,6 @@ export class AuthenticatedApi extends cdk.Construct {
} exceeds duration ${durationThreshold.toMilliseconds()} milliseconds`,
actionsEnabled: true,
metric: durationMetric,
statistic: "sum",
period: cdk.Duration.minutes(1),
evaluationPeriods: 1,
threshold: durationThreshold.toMilliseconds(),
comparisonOperator:
Expand All @@ -180,7 +180,9 @@ export class AuthenticatedApi extends cdk.Construct {
const latencyThreshold = props.apiLatencyAlarmThreshold
? props.apiLatencyAlarmThreshold
: DEFAULT_API_LATENCY_THRESHOLD;
const metricLatency = httpApi.metricLatency(); //{
const metricLatency = httpApi
.metricLatency()
.with({ statistic: "sum", period: cdk.Duration.minutes(1) });

const routeLatencyAlarm = new cloudwatch.Alarm(
this,
Expand All @@ -192,8 +194,6 @@ export class AuthenticatedApi extends cdk.Construct {
} exceeds ${latencyThreshold.toMilliseconds()} milliseconds`,
actionsEnabled: true,
metric: metricLatency,
statistic: "sum",
period: cdk.Duration.minutes(1),
evaluationPeriods: 1,
threshold: latencyThreshold.toMilliseconds(),
comparisonOperator:
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda-worker/lambda-worker-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface LambdaWorkerProps {
policyStatements?: iam.PolicyStatement[];
reservedConcurrentExecutions?: number;
retryAttempts?: number;
securityGroup?: ec2.ISecurityGroup;
securityGroups?: Array<ec2.ISecurityGroup>;
timeout: cdk.Duration;
vpc: ec2.IVpc;
vpcSubnets: ec2.SubnetSelection;
Expand Down
25 changes: 11 additions & 14 deletions lib/lambda-worker/lambda-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ export class LambdaWorker extends cdk.Construct {
const alarmAction = new cloudwatchActions.SnsAction(props.alarmTopic);

// Add an alarm on any messages appearing on the DLQ
const approximateNumberOfMessagesVisibleMetric = lambdaDLQ.metric(
"ApproximateNumberOfMessagesVisible"
);
const approximateNumberOfMessagesVisibleMetric = lambdaDLQ
.metric("ApproximateNumberOfMessagesVisible")
.with({
statistic: "sum",
period: cdk.Duration.minutes(1),
});
const dlqMessagesVisable = new cloudwatch.Alarm(
this,
`${props.name}-dlq-messages-visible-alarm`,
Expand All @@ -143,8 +146,6 @@ export class LambdaWorker extends cdk.Construct {
alarmDescription: `Alarm when the lambda worker fails to process a message and the message appears on the DLQ`,
actionsEnabled: true,
metric: approximateNumberOfMessagesVisibleMetric,
statistic: "sum",
period: cdk.Duration.minutes(1),
evaluationPeriods: 1,
threshold: 1,
comparisonOperator:
Expand All @@ -158,9 +159,9 @@ export class LambdaWorker extends cdk.Construct {
dlqMessagesVisable.addOkAction(alarmAction);

// Add an alarm for the age of the oldest message on the LambdaWorkers main trigger queue
const approximateAgeOfOldestMessageMetric = lambdaDLQ.metric(
"ApproximateAgeOfOldestMessage"
);
const approximateAgeOfOldestMessageMetric = lambdaDLQ
.metric("ApproximateAgeOfOldestMessage")
.with({ statistic: "average", period: cdk.Duration.minutes(1) });
const queueMessagesAge = new cloudwatch.Alarm(
this,
`${props.name}-queue-message-age-alarm`,
Expand All @@ -169,8 +170,6 @@ export class LambdaWorker extends cdk.Construct {
alarmDescription: `Alarm when the lambda workers main trigger queue has messages older than ${approximateAgeOfOldestMessageThreshold.toSeconds()} seconds`,
actionsEnabled: true,
metric: approximateAgeOfOldestMessageMetric,
statistic: "average",
period: cdk.Duration.minutes(1),
evaluationPeriods: 1,
threshold: approximateAgeOfOldestMessageThreshold.toSeconds(),
comparisonOperator:
Expand All @@ -192,8 +191,6 @@ export class LambdaWorker extends cdk.Construct {
alarmDescription: `Alarm when the lambda workers main trigger queue has more than ${approximateNumberOfMessagesVisibleThreshold} messages on the queue`,
actionsEnabled: true,
metric: approximateNumberOfMessagesVisibleMetric,
statistic: "sum",
period: cdk.Duration.minutes(1),
evaluationPeriods: 1,
threshold: approximateNumberOfMessagesVisibleThreshold,
comparisonOperator:
Expand Down Expand Up @@ -259,7 +256,7 @@ export class LambdaWorker extends cdk.Construct {
reservedConcurrentExecutions:
props.lambdaProps.reservedConcurrentExecutions,
retryAttempts: props.lambdaProps.retryAttempts,
securityGroup: props.lambdaProps.securityGroup,
securityGroups: props.lambdaProps.securityGroups,
timeout: props.lambdaProps.timeout,
vpc: props.lambdaProps.vpc,
vpcSubnets: props.lambdaProps.vpcSubnets,
Expand Down Expand Up @@ -304,7 +301,7 @@ export class LambdaWorker extends cdk.Construct {
reservedConcurrentExecutions:
props.lambdaProps.reservedConcurrentExecutions,
retryAttempts: props.lambdaProps.retryAttempts,
securityGroup: props.lambdaProps.securityGroup,
securityGroups: props.lambdaProps.securityGroups,
timeout: props.lambdaProps.timeout,
filesystem: props.lambdaProps.filesystem,
vpc: props.lambdaProps.vpc,
Expand Down
Loading

0 comments on commit 7386401

Please sign in to comment.