Skip to main content

apigateway

create rest-api

$ aws apigateway get-rest-apis
$ aws apigateway create-rest-api --name <API Name>
$ aws apigateway get-rest-api --rest-api-id <API ID>

create resource

$ aws apigateway get-resources --rest-api-id <API ID>
$ aws apigateway create-resource --rest-api-id <API ID> --parent-id <Parent Resource ID> --path-part subpath
$ aws apigateway get-resource --rest-api-id <API ID> --resource-id <Resource ID>

put method

$ aws apigateway put-method --rest-api-id <API ID> --resource-id <Resource ID> --authorization-type none --http-method get 
$ aws apigateway get-method --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET

put integration response

$ aws apigateway put-integration-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200 --selection-pattern "" --response-templates '{"application/json":"{\"json\":\"template\"}"}'
$ aws apigateway get-integration-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200

put method response

$ aws apigateway put-method-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200 --response-models '{"text/plain":"Empty"}'
$ aws apigateway get-method-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200

update integration

$ jq '.' test.json
[
  {
    "op": "add",
    "path": "/requestTemplates/application~1json",
    "value": "{\"example\":\"json\"}"
  }
]
$ aws apigateway update-integration --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --patch-operations file://test.json

create deployment

$ aws apigateway get-deployments --rest-api-id <API ID>
$ aws apigateway create-deployment --rest-api-id <API ID> --stage-name <Stage Name>
$ aws apigateway get-deployment --rest-api-id <API ID> --deployment-id <Deployment ID>

create stage

$ aws apigateway get-stages --rest-api-id <API ID>
$ aws apigateway create-stage --rest-api-id <API ID> --stage-name <Stage Name> --deployment-id <Deployment ID>
$ aws apigateway get-stage --rest-api-id <API ID> --stage-name <Stage Name>

test invoke method

$ aws apigateway test-invoke-method --rest-api-id <API ID> --resource-id <Resource ID>
  --http-method [GET|POST|DELETE|...]
  --path-with-query-string "/"
  --body "bodystring"

create api key

$ aws apigateway get-api-keys
$ aws apigateway create-api-key --name testkey
$ aws apigateway get-api-key --api-key <Key ID>
$ aws apigateway get-api-key --api-key <Key ID> --include-value

update api key

$ aws apigateway update-api-key --api-key <Key ID> --patch-operations '{ "op":"replace", "path":"/enabled", "value":"true" }'

create usage plan

$ aws apigateway get-usage-plans
$ aws apigateway create-usage-plan --name myusageplan --api-stages '{"apiId":"<API ID>","stage":"<Stage Name>"}' --throttle '{ "burstLimit": 2, "rateLimit": 1.0 }' --quota '{ "limit": 320, "offset": 0, "period": "DAY" }'
$ aws apigateway get-usage-plan --usage-plan-id <Usage Plan ID>

associate usage-plan and key

$ aws apigateway get-usage-plan-keys --usage-plan-id <Usage Plan ID>
$ aws apigateway create-usage-plan-key --usage-plan-id <Usage Plan ID> --key-id <Key ID> --key-type API_KEY
$ aws apigateway get-usage-plan-key --usage-plan-id <Usage Plan ID> --key-id <Key ID>

update method

$ aws apigateway update-method --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --patch-operations op='replace',path='/apiKeyRequired',value='true'
$ aws apigateway get-method --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET

test run with api-key

$ curl https://<API ID>.execute-api.<Region Name>.amazonaws.com/<Stage Name> --header "x-api-key:<API Key Value>"

diassociate usage-plan and key

$ aws apigateway get-usage-plan-key --usage-plan-id <Usage Plan ID> --key-id <Key ID>
$ aws apigateway delete-usage-plan-key --usage-plan-id <Usage Plan ID> --key-id <Key ID>

delete stage

$ aws apigateway get-stage --rest-api-id <API ID> --stage-name <Stage Name>
$ aws apigateway delete-stage --rest-api-id <API ID> --stage-name <Stage Name>

delete deployment

$ aws apigateway get-deployment --rest-api-id <API ID> --deployment-id <Deployment ID>
$ aws apigateway delete-deployment --rest-api-id <API ID> --deployment-id <Deployment ID>

delete usage-plan

$ aws apigateway get-usage-plan --usage-plan-id <Usage Plan ID>
$ aws apigateway delete-usage-plan --usage-plan-id <Usage Plan ID>

delete api key

$ aws apigateway get-api-key --api-key <Key ID>
$ aws apigateway delete-api-key --api-key <Key ID>

delete integration response

$ aws apigateway get-integration-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200
$ aws apigateway delete-integration-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200

delete method response

$ aws apigateway get-method-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200
$ aws apigateway delete-method-response --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET --status-code 200

delete integration

$ aws apigateway get-integration --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET
$ aws apigateway delete-integration --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET

delete method

$ aws apigateway get-method --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET
$ aws apigateway delete-method --rest-api-id <API ID> --resource-id <Resource ID> --http-method GET

delete resource

$ aws apigateway get-resource --rest-api-id <API ID> --resource-id <Resource ID>
$ aws apigateway delete-resource --rest-api-id <API ID> --resource-id <Resource ID>

delete rest-api

$ aws apigateway get-rest-api --rest-api-id <API ID>
$ aws apigateway delete-rest-api --rest-api-id <API ID>

cloudformation sample template

    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: "ApiGatewayRestApi"
        Tags:
          - "Key": "Name"
            "Value": "test"
    ApiGatewayResourceSubject:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
        PathPart: "{subject}"
        RestApiId: !Ref ApiGatewayRestApi
    TestLambdaPermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName: !GetAtt TestLambda.Arn
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
    ApiGatewayGET:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: "GET"
        RestApiId: !Ref ApiGatewayRestApi
        ResourceId: !Ref ApiGatewayResourceSubject
        RequestParameters:
          "method.request.path.string": true
          "method.request.path.year": true
        AuthorizationType: "NONE"
        MethodResponses:
            - StatusCode: 200
              ResponseModels:
                application/json;charset=UTF-8: Empty
        Integration:
          Type: "AWS_PROXY"
          IntegrationHttpMethod: "POST"
          RequestParameters:
            "integration.request.path.string": "method.request.path.string"
            "integration.request.path.year": "method.request.path.year"
          Uri: !Sub >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${TestLambda.Arn}/invocations
          RequestTemplates:
            application/json: "{ \"statusCode\" : 200 }"
    DeployDevel:
      DependsOn: ApiGatewayGET
      Type: 'AWS::ApiGateway::Deployment'
      Properties:
        RestApiId: !Ref ApiGatewayRestApi
        StageName: "devel"