如何从CloudFormation AWS :: Lambda :: Alias获取函数名称和别名?

问题描述:

我需要为API网关阶段设置阶段变量。这个阶段变量必须是lambda函数和别名(Foo:dev)。它不能是完整的ARN。然后,这个变量用在swagger中以将API网关与具有特定别名的lambda函数集成。如何从CloudFormation AWS :: Lambda :: Alias获取函数名称和别名?

看起来我唯一能够脱离AWS :: Lambda :: Alias资源的就是ARN。我如何获取名称和别名?

这是舞台资源。 “lamdaAlias”被设置为别名的完整ARN。

"ApiGatewayStageDev": { 
     "Type": "AWS::ApiGateway::Stage", 
     "Properties": { 
      "StageName": "dev", 
      "Description": "Dev Stage", 
      "RestApiId": { 
       "Ref": "ApiGatewayApi" 
      }, 
      "DeploymentId": { 
       "Ref": "ApiGatewayDeployment" 
      }, 
      "Variables": { 
       "lambdaAlias": { 
        "Ref": "LambdaAliasDev" 
       } 
      } 
     } 
    } 

只是重用用于指定在AWS::Lambda::Alias资源FunctionNameName性质相同的值。例如,假设您的资源这样的规定在您的模板:

"LambdaAliasDev" : { 
    "Type" : "AWS::Lambda::Alias", 
    "Properties" : { 
    "FunctionName" : { "Ref" : "MyFunction" }, 
    "FunctionVersion" : { "Fn::GetAtt" : [ "TestingNewFeature", "Version" ] }, 
    "Name" : { "Ref" : "MyFunctionAlias" } 
    } 
} 

你会使用Fn::Join内在函数的函数和别名合并成一个字符串,像这样:

"ApiGatewayStageDev": { 
    "Type": "AWS::ApiGateway::Stage", 
    "Properties": { 
     "StageName": "dev", 
     "Description": "Dev Stage", 
     "RestApiId": { 
      "Ref": "ApiGatewayApi" 
     }, 
     "DeploymentId": { 
      "Ref": "ApiGatewayDeployment" 
     }, 
     "Variables": { 
      "lambdaAlias": { 
       "Fn::Join": {[ ":", [ 
        { "Ref": "MyFunction" }, 
        { "Ref": "MyFunctionAlias" } 
       ]} 
      } 
     } 
    } 
} 

假设MyFunctionFooMyFunctionAliasdev,这将根据需要设置lambdaAliasFoo:dev

+0

谢谢!你能告诉你如何定义MyFunctionAlias(假设它不是一个参数)? – boris

+1

@boris'MyFunctionAlias'是本示例中的一个参数 - 请参阅[Parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)文档以了解如何定义堆栈模板中的参数。如果您想将该值指定为固定常数,则可以使用默认值创建一个参数,或者在两个参数中都使用常量字符串(例如,“dev”)替换“{”Ref“:”MyFunctionAlias“}。放置在模板中。 – wjordan