-
Notifications
You must be signed in to change notification settings - Fork 156
chore(ci): add canary to layer deployment #1593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { CustomResource, Duration, Stack, StackProps } from 'aws-cdk-lib'; | ||
import { Construct } from 'constructs'; | ||
import { LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda'; | ||
import { RetentionDays } from 'aws-cdk-lib/aws-logs'; | ||
import { v4 } from 'uuid'; | ||
import { | ||
Effect, | ||
ManagedPolicy, | ||
PolicyStatement, | ||
Role, | ||
ServicePrincipal, | ||
} from 'aws-cdk-lib/aws-iam'; | ||
import { Provider } from 'aws-cdk-lib/custom-resources'; | ||
import { StringParameter } from 'aws-cdk-lib/aws-ssm'; | ||
import path from 'path'; | ||
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; | ||
|
||
export interface CanaryStackProps extends StackProps { | ||
readonly layerName: string; | ||
readonly powertoolsPackageVersion: string; | ||
readonly ssmParameterLayerArn: string; | ||
} | ||
|
||
export class CanaryStack extends Stack { | ||
public constructor(scope: Construct, id: string, props: CanaryStackProps) { | ||
super(scope, id, props); | ||
const { layerName, powertoolsPackageVersion } = props; | ||
|
||
const suffix = v4().substring(0, 5); | ||
|
||
const layerArn = StringParameter.fromStringParameterAttributes( | ||
this, | ||
'LayerArn', | ||
{ | ||
parameterName: props.ssmParameterLayerArn, | ||
} | ||
).stringValue; | ||
|
||
// lambda function | ||
const layer = [ | ||
LayerVersion.fromLayerVersionArn(this, 'powertools-layer', layerArn), | ||
]; | ||
|
||
const executionRole = new Role(this, 'LambdaExecutionRole', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: if we are defining the canaryFunction.role?.attachInlinePolicy(
new iam.Policy(this, 'list-buckets-policy', {
statements: [
new PolicyStatement({
actions: ['lambda:GetFunction'],
resources: ['*'],
effect: Effect.ALLOW,
})
],
}),
); This way we let CDK manage the role now & in future versions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This made me realise that the role is not required any more, so removing. |
||
assumedBy: new ServicePrincipal('lambda.amazonaws.com'), | ||
managedPolicies: [ | ||
ManagedPolicy.fromAwsManagedPolicyName( | ||
'service-role/AWSLambdaBasicExecutionRole' | ||
), | ||
], | ||
}); | ||
|
||
executionRole.addToPolicy( | ||
new PolicyStatement({ | ||
actions: ['lambda:GetFunction'], | ||
resources: ['*'], | ||
effect: Effect.ALLOW, | ||
}) | ||
); | ||
|
||
const canaryFunction = new NodejsFunction(this, 'CanaryFunction', { | ||
entry: path.join(__dirname, './canary/app.ts'), | ||
handler: 'handler', | ||
runtime: Runtime.NODEJS_18_X, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we / is it worth it to use different runtimes? Is it overkill to do so here? If we decide to go with only one, should we use the oldest runtime instead? The reasoning behind using oldest is that we already develop using the latest, so it's more likely we'll have caught issues with that. On the other hand, that might be a concern for e2e tests and not for this stage. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am indifferent, the e2e tests for layer should ensure it works with all supported runtimes. Using oldest is a better choice as you mentioned. |
||
functionName: `canary-${suffix}`, | ||
timeout: Duration.seconds(30), | ||
bundling: { | ||
externalModules: [ | ||
// don't package these modules, we want to pull them from the layer | ||
'aws-sdk', | ||
'@aws-lambda-powertools/logger', | ||
'@aws-lambda-powertools/metrics', | ||
'@aws-lambda-powertools/tracer', | ||
'@aws-lambda-powertools/parameters', | ||
'@aws-lambda-powertools/commons', | ||
], | ||
}, | ||
role: executionRole, | ||
environment: { | ||
POWERTOOLS_SERVICE_NAME: 'canary', | ||
POWERTOOLS_VERSION: powertoolsPackageVersion, | ||
POWERTOOLS_LAYER_NAME: layerName, | ||
}, | ||
layers: layer, | ||
logRetention: RetentionDays.ONE_DAY, | ||
}); | ||
|
||
// use custom resource to trigger the lambda function during the CFN deployment | ||
const provider = new Provider(this, 'CanaryCustomResourceProvider', { | ||
onEventHandler: canaryFunction, | ||
logRetention: RetentionDays.ONE_DAY, | ||
}); | ||
|
||
// random suffix forces recreation of the custom resource otherwise the custom resource will be reused from prevous deployment | ||
new CustomResource(this, `CanaryCustomResource${suffix}`, { | ||
serviceToken: provider.serviceToken, | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
import { Context } from 'aws-lambda'; | ||||||
import { Logger } from '@aws-lambda-powertools/logger'; | ||||||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||||||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||||||
import { SSMProvider } from '@aws-lambda-powertools/parameters/ssm'; | ||||||
|
||||||
const logger = new Logger(); | ||||||
const tracer = new Tracer(); | ||||||
const metrics = new Metrics({}); | ||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||||
const ssmProvider = new SSMProvider(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we can remove the Eslint ignore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, let's actually retrieve a parameter - I see that the stack has one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can fetch the layer ARN parameter, good point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed my mind, I only create the provider. It'd add otherwise unnecessary complexity to change the tests with parameters. |
||||||
|
||||||
export const handler = async ( | ||||||
_event: unknown, | ||||||
_context: Context | ||||||
): Promise<void> => { | ||||||
logger.info('Hello, world!'); | ||||||
metrics.addMetric('MyMetric', MetricUnits.Count, 1); | ||||||
tracer.annotateColdStart(); | ||||||
}; |
Uh oh!
There was an error while loading. Please reload this page.