-
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 all commits
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,87 @@ | ||
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, PolicyStatement } 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 canaryFunction = new NodejsFunction(this, 'CanaryFunction', { | ||
entry: path.join( | ||
__dirname, | ||
'../tests/e2e/layerPublisher.class.test.functionCode.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', | ||
], | ||
}, | ||
environment: { | ||
POWERTOOLS_SERVICE_NAME: 'canary', | ||
POWERTOOLS_PACKAGE_VERSION: powertoolsPackageVersion, | ||
POWERTOOLS_LAYER_NAME: layerName, | ||
SSM_PARAMETER_LAYER_ARN: props.ssmParameterLayerArn, | ||
}, | ||
layers: layer, | ||
logRetention: RetentionDays.ONE_DAY, | ||
}); | ||
|
||
canaryFunction.addToRolePolicy( | ||
new PolicyStatement({ | ||
actions: ['ssm:GetParameter'], | ||
resources: ['*'], | ||
effect: Effect.ALLOW, | ||
}) | ||
); | ||
|
||
// 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, | ||
}); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.