每当代码有更新时,我都会发布一个新版本,并为该版本创建一个别名。但是以前的别名在新版本中丢失了
这是我的 YAML 模板片段
Description: Publish a new version of a Lambda function whenever the code is updated.
Parameters:
MyTestString:
Description: Change this string when code is updated.
Type: String
Default: "Test"
LambdaFuncName:
Type: String
Default: stack_over_flow
LambdaCustomFuncName:
Type: String
Default: custom_stack_over_flow
LambdaVersionNumber:
Type: String
Default: MyAlias-001
Resources:
MyCustomResource:
Type: Custom::Resource
Properties:
ServiceToken: !GetAtt MyFunction.Arn
MyTestString: !Ref MyTestString
MyFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
FunctionName:
Ref: LambdaFuncName
Code:
ZipFile: !Sub |
var response = require('cfn-response');
exports.handler = function(event, context) {
return response.send(event, context, response.SUCCESS, {Result: '${MyTestString}'});
};
Runtime: nodejs12.x
LambdaDeploy:
Type: Custom::LambdaVersion
Properties:
ServiceToken: !GetAtt LambdaDeployFunction.Arn
FunctionName: !Ref MyFunction
MyTestString: !Ref MyTestString
LambdaDeployFunction:
Type: AWS::Lambda::Function
Properties:
Handler: "index.handler"
Role: !GetAtt LambdaExecutionRole.Arn
FunctionName:
Ref: LambdaCustomFuncName
Code:
ZipFile: !Sub |
var AWS = require('aws-sdk');
var response = require('cfn-response');
exports.handler = (event, context) => {
console.log("Request received:\n", JSON.stringify(event));
if (event.RequestType == 'Delete') {
return response.send(event, context, response.SUCCESS);
}
var lambda = new AWS.Lambda();
lambda.publishVersion({FunctionName: event.ResourceProperties.FunctionName}).promise().then((data) => {
return response.send(event, context, response.SUCCESS, {Version: data.Version}, data.FunctionArn);
}).catch((e) => {
return response.send(event, context, response.FAILED, e);
});
};
Runtime: nodejs12.x
LambdaAlias:
Type: 'AWS::Lambda::Alias'
Properties:
FunctionName: !Ref MyFunction
FunctionVersion: !GetAtt
- LambdaDeploy
- Version
Name:
Ref: LambdaVersionNumber
LambdaExecutionRole:
Type: AWS::IAM::Role
DeletionPolicy: Retain
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: {Service: [lambda.amazonaws.com]}
Action: ['sts:AssumeRole']
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: PublishVersion
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: ['lambda:PublishVersion']
Resource: '*'
Outputs:
LambdaVersion:
Value: !GetAtt LambdaDeploy.Version
CustomResourceResult:
Value: !GetAtt MyCustomResource.Result
LambdaVersionNumber是定义的参数字段,以便我每次都可以获得新版本
提前感谢任何线索或帮助