title: How to specify a Stage variable in AWS API Gateway integration using AWS CloudFormation?
body: <p>I am trying to build an AWS CloudFormation template to create an API Gateway.<br>
  When I manually created the API Gateway, I use stage variables to use different AWS Functions for different stages.<br></p>
  <p>eg. I have a Stage Variables called <code>adminLogin</code>,<br>
  The values of adminLogin will be -<br>
  <code>dev_adminLogin</code> when the API Gateway's stage is <code>dev</code><br>
  <code>stage_adminLogin</code> when the API Gateway's stage is <code>stage</code></p>
  <p><strong>API Gateway's Resource integration request -</strong><br>
  <a href="https://i.stack.imgur.com/g6Imm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g6Imm.png" alt="enter image description here" /></a></p>
  <p><strong>Stage Variable mapping -</strong><br>
  <a href="https://i.stack.imgur.com/7BdjE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7BdjE.png" alt="enter image description here" /></a></p>
  <p><strong>CloudFormation template snippet -</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>test:
      Type: 'AWS::ApiGateway::RestApi'
      Properties:
        Name: 'test'
        Body:
          swagger: &quot;2.0&quot;
          info:
            version: &quot;2019-04-11T02:29:18Z&quot;
            title: &quot;Test&quot;
          basePath: !Ref &quot;testEnv&quot;
          schemes:
            - &quot;https&quot;
          paths:
            /admin/login:
              post:
                consumes:
                  - &quot;application/json&quot;
                produces:
                  - &quot;application/json&quot;
                responses:
                  '200':
                    description: &quot;200 response&quot;
                    schema:
                      $ref: &quot;#/definitions/Empty&quot;
                x-amazon-apigateway-integration:
                  #uri: !Sub &quot;arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${stageVariables.adminLogin}/invocations&quot;
                  uri: !Join [
                      '', [
                        'arn:',
                        'aws:',
                        'apigateway:',
                        !Ref &quot;AWS::Region&quot;,
                        ':lambda:',
                        'path/2015-03-31/functions/',
                        '${stageVariables.adminLogin}',
                        '/invocations'
                      ]
                    ]
                  responses:
                    default:
                      statusCode: &quot;200&quot;
                  passthroughBehavior: &quot;when_no_templates&quot;
                  httpMethod: &quot;POST&quot;
                  contentHandling: &quot;CONVERT_TO_TEXT&quot;
                  type: &quot;aws_proxy&quot;
  </code></pre>
  <p><strong>I am getting the following error when I run the cloudformation template -</strong><br></p>
  <pre><code>Errors found during import: Unable to put integration on 'POST' for resource at path '/admin/login': Invalid lambda function 
  (Service: AmazonApiGateway; 
  Status Code: 400; 
  Error Code: BadRequestException; 
  </code></pre>
  <p><strong>The issue is definately with the <code>uri</code> property,<br>
  I tried both -</strong><br></p>
  <pre class="lang-yaml prettyprint-override"><code>uri: !Sub &quot;arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${stageVariables.adminLogin}/invocations&quot;
  </code></pre>
  <p>and</p>
  <pre class="lang-yaml prettyprint-override"><code>uri: !Join ['', ['arn:','aws:','apigateway:',!Ref &quot;AWS::Region&quot;,':lambda:','path/2015-03-31/functions/','${!stageVariables.adminLogin}','/invocations']]
  </code></pre>
  <p><strong>Reference -</strong><br></p>
  <ol>
  <li><a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html#cfn-apigateway-method-integration-uri" rel="nofollow noreferrer">https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html#cfn-apigateway-method-integration-uri</a><br></li>
  <li><a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html</a></li>
  </ol>