1
votes

I need to set a stage variable for an API Gateway stage. This stage variable has to be just the lambda function and alias (Foo:dev). It cannot be the full ARN. The variable is then used in swagger to integrate API Gateway with a lambda function with a specific alias.

It looks like the only thing I can out of AWS::Lambda::Alias resource is the ARN. How do I just get the name and alias?

This is the stage resource. "lamdaAlias" gets set to the full ARN of the alias.

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

1 Answers

2
votes

Just reuse the same values used to specify the FunctionName and Name properties in the AWS::Lambda::Alias resource. For example, assuming your resource is specified like this in your template:

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

You would combine the function and alias into a single string using the Fn::Join intrinsic function, like this:

"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" }
                ]}
            }
        }
    }
}

Assuming MyFunction is Foo and MyFunctionAlias is dev, this would set lambdaAlias to Foo:dev as desired.