20

How do I find the URL address of the API Gateway after deployment from Command line ?

I use a script similar to below to deploy my API Gateway and Authorizer, and it deploys fine.

https://github.com/floodfx/aws-lambda-proxy-using-sam-local/blob/master/deploy.sh

I'm trying to figure out how to get the address of the API Gateway after Deployment from the command line

The API Gateway gets created, I can see the stack:

aws cloudformation describe-stacks
"Stacks": [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:761861444952:stack/mygateway912/72100720-6e67-11e8-93e9-500c28604c4a", 
            "Description": "An example serverless \"Hello World2 \" application with a custom authorizer.", 
            "Tags": [], 
            "CreationTime": "2018-06-12T17:38:40.946Z", 
            "Capabilities": [
                "CAPABILITY_IAM"
            ], 
            "StackName": "mygateway912", 
            "NotificationARNs": [], 
            "StackStatus": "CREATE_COMPLETE", 
            "DisableRollback": false, 
            "ChangeSetId": "arn:aws:cloudformation:us-east-1:76161444952:changeSet/awscli-cloudformation-package-deploy-1528825120/352f7c7a-2870-44ea-9e7f-40d16c0015df", 
            "LastUpdatedTime": "2018-06-12T17:38:46.411Z"
        }

There must be a simple command I'm missing to get this.

3
  • 1
    you can use output parameter of the cloudformation template to export resource references Commented Jun 12, 2018 at 17:59
  • I just saw that.. Maybe it will help someone else I'll add an example Commented Jun 12, 2018 at 18:06
  • 1
    Outputs: ExampleAPIUrl: Value: !Sub "https://${ExampleAPI}.execute-api.${AWS::Region}.amazonaws.com/${StageName}/" Commented Jun 12, 2018 at 18:07

3 Answers 3

31

I just had time to answer properly. Having API Gateway definition:

Resources:
  ...
  ServerlessRestApi:
    Type: AWS::Serverless::Api
    DeletionPolicy: "Retain"
    Properties:
      StageName: Prod
  ...

you can output

Outputs:
  ProdDataEndpoint:
    Description: "API Prod stage endpoint"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
Sign up to request clarification or add additional context in comments.

2 Comments

I put some example on my github, if anyone is still struggling with this: github.com/pmcdowell-okta/my-notes/tree/master/awsSam
How can I get the value of that output at runtime?
11

I have separate AWS::ApiGateway::RestApi and AWS::ApiGateway::Stage resources, so my Output looked a bit different, since I didn't/couldn't hard code the stage name:

Outputs:
  ProdEndpoint:
    Value: !Sub "https://${ApiGw}.execute-api.${AWS::Region}.amazonaws.com/${ApiGwStage}/"

Resources:
  ApiGw:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: 'Serverless Ipsum #noServerNovember challenge'
      FailOnWarnings: true

  ApiGwDeployment:
    Type: AWS::ApiGateway::Deployment
    # Required -- see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html
    DependsOn: ApiGwMethod
    Properties:
      RestApiId: !Ref ApiGw

  ApiGwStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      DeploymentId: !Ref ApiGwDeployment
      MethodSettings:
        - DataTraceEnabled: true
          HttpMethod: '*'
          LoggingLevel: INFO
          ResourcePath: '/*'
      RestApiId: !Ref ApiGw
      StageName: prod

  ApiGwResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref ApiGw
      ParentId: !GetAtt ["ApiGw", "RootResourceId"]
      PathPart: "{proxy+}"

  ApiGwMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref ApiGw
      ResourceId: !Ref ApiGwResource
      HttpMethod: ANY
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ServerlessIpsumFunction.Arn}/invocations"

1 Comment

Op specified SAM Deploy (not explicitly creating resources).
0

I am using a parameter AppEnv

....

Parameters:
  ...

  AppEnv:
    Type: String
    Default: stage
    Description: Application environment

Resources:
  GateWayAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref AppEnv
      ....
  UploadFileFunction:
    ....
      Events:
        UploadFile:
          ...
          Properties:
            Path: /upload-file
            Method: post
            RestApiId: !Ref GateWayAPI
....

Outputs:
 ....
  UploadFileApi:
    Description: "API Gateway endpoint URL for UploadFileFunction"
    Value: !Sub "https://${GateWayAPI}.execute-api.${AWS::Region}.amazonaws.com/${AppEnv}/upload-file/"
  ...

Full configuration file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.