AWS CDK:如何将指定版本的 Lambda 与别名相关联?
AWS CDK : How do I associate a specified version of Lambda with an alias?
我使用 AWS CDK 来管理 Lambda。
我为 Lambda 函数创建了两个别名,development
和 production
。
但我不知道如何将版本与每个别名相关联。
export class CdkLambdaStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const fnDemo = new NodejsFunction(this, 'demo', {
entry: 'lib/lambda-handler/index.ts',
currentVersionOptions: {
removalPolicy: RemovalPolicy.RETAIN,
retryAttempts: 1
}
});
// In this case, production would be the most recent version
// I want to specify the previous stable version
fnDemo.currentVersion.addAlias('production');
new lambda.Alias(this, 'demo-development-alias', {
aliasName: 'development',
version: fnDemo.latestVersion
});
}
}
我查看了 AWS CDK 文档,但找不到获取以前版本的方法。你还有什么好的想法吗?
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Version.html
已解决
import cdk = require('@aws-cdk/core');
import * as lambda from '@aws-cdk/aws-lambda';
import {NodejsFunction} from '@aws-cdk/aws-lambda-nodejs';
import {RemovalPolicy} from "@aws-cdk/core";
export class CdkLambdaStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const fnDemo = new NodejsFunction(this, 'demo', {
entry: 'lib/lambda-handler/index.ts',
currentVersionOptions: {
removalPolicy: RemovalPolicy.RETAIN,
}
});
const prodVersion = lambda.Version.fromVersionArn(this, 'prodVersion', `${fnDemo.functionArn}:1`);
prodVersion.addAlias('production');
const stgVersion = lambda.Version.fromVersionArn(this, 'stgVersion', `${fnDemo.functionArn}:2`);
stgVersion.addAlias('staging');
const currentVersion = fnDemo.currentVersion;
const development = new lambda.Alias(this, 'demo-development', {
aliasName: 'development',
version: currentVersion
});
}
}
我使用 AWS CDK 来管理 Lambda。
我为 Lambda 函数创建了两个别名,development
和 production
。
但我不知道如何将版本与每个别名相关联。
export class CdkLambdaStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const fnDemo = new NodejsFunction(this, 'demo', {
entry: 'lib/lambda-handler/index.ts',
currentVersionOptions: {
removalPolicy: RemovalPolicy.RETAIN,
retryAttempts: 1
}
});
// In this case, production would be the most recent version
// I want to specify the previous stable version
fnDemo.currentVersion.addAlias('production');
new lambda.Alias(this, 'demo-development-alias', {
aliasName: 'development',
version: fnDemo.latestVersion
});
}
}
我查看了 AWS CDK 文档,但找不到获取以前版本的方法。你还有什么好的想法吗?
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Version.html
已解决
import cdk = require('@aws-cdk/core');
import * as lambda from '@aws-cdk/aws-lambda';
import {NodejsFunction} from '@aws-cdk/aws-lambda-nodejs';
import {RemovalPolicy} from "@aws-cdk/core";
export class CdkLambdaStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const fnDemo = new NodejsFunction(this, 'demo', {
entry: 'lib/lambda-handler/index.ts',
currentVersionOptions: {
removalPolicy: RemovalPolicy.RETAIN,
}
});
const prodVersion = lambda.Version.fromVersionArn(this, 'prodVersion', `${fnDemo.functionArn}:1`);
prodVersion.addAlias('production');
const stgVersion = lambda.Version.fromVersionArn(this, 'stgVersion', `${fnDemo.functionArn}:2`);
stgVersion.addAlias('staging');
const currentVersion = fnDemo.currentVersion;
const development = new lambda.Alias(this, 'demo-development', {
aliasName: 'development',
version: currentVersion
});
}
}