首页 文章

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

提问于
浏览
1

我需要为API Gateway阶段设置阶段变量 . 这个阶段变量必须只是lambda函数和别名(Foo:dev) . 它不能是完整的ARN . 然后,该变量用于swagger,以将API网关与具有特定别名的lambda函数集成 .

看起来我唯一能从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"
                }
            }
        }
    }

1 回答

  • 2

    只需重用用于指定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 ,则会根据需要将 lambdaAlias 设置为 Foo:dev .

相关问题