API Gateway
API Gateway CLI Reference
AWS CLI commands for API Gateway
Complete reference for AWS API Gateway CLI commands with examples.
REST API Management
Create REST API
aws apigateway create-rest-api \
--name my-api \
--description "My REST API" \
--endpoint-configuration types=REGIONALGet REST APIs
aws apigateway get-rest-apis
# Get specific API
aws apigateway get-rest-api --rest-api-id abc123Delete REST API
aws apigateway delete-rest-api --rest-api-id abc123Resources
Get Root Resource
aws apigateway get-resources --rest-api-id abc123Create Resource
# Get root resource ID first
ROOT_ID=$(aws apigateway get-resources --rest-api-id abc123 --query 'items[?path==`/`].id' --output text)
# Create /users resource
aws apigateway create-resource \
--rest-api-id abc123 \
--parent-id $ROOT_ID \
--path-part users
# Create /users/{id} resource
aws apigateway create-resource \
--rest-api-id abc123 \
--parent-id users-resource-id \
--path-part "{id}"Delete Resource
aws apigateway delete-resource \
--rest-api-id abc123 \
--resource-id xyz789Methods
Create Method
# Create GET method
aws apigateway put-method \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--authorization-type NONE
# With API key required
aws apigateway put-method \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method POST \
--authorization-type NONE \
--api-key-required
# With IAM authorization
aws apigateway put-method \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--authorization-type AWS_IAMDelete Method
aws apigateway delete-method \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GETIntegrations
Lambda Integration
aws apigateway put-integration \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--type AWS_PROXY \
--integration-http-method POST \
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:my-function/invocationsHTTP Integration
aws apigateway put-integration \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--type HTTP_PROXY \
--integration-http-method GET \
--uri https://api.example.com/endpointMock Integration
aws apigateway put-integration \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--type MOCK \
--request-templates '{"application/json": "{\"statusCode\": 200}"}'Method Responses
Create Method Response
aws apigateway put-method-response \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--status-code 200 \
--response-models '{"application/json": "Empty"}'Integration Responses
Create Integration Response
aws apigateway put-integration-response \
--rest-api-id abc123 \
--resource-id xyz789 \
--http-method GET \
--status-code 200 \
--selection-pattern ""Deployments
Create Deployment
aws apigateway create-deployment \
--rest-api-id abc123 \
--stage-name prod \
--stage-description "Production stage" \
--description "Initial deployment"Get Deployments
aws apigateway get-deployments --rest-api-id abc123Stages
Create Stage
aws apigateway create-stage \
--rest-api-id abc123 \
--stage-name dev \
--deployment-id def456 \
--description "Development stage"Update Stage
# Enable caching
aws apigateway update-stage \
--rest-api-id abc123 \
--stage-name prod \
--patch-operations '[
{"op": "replace", "path": "/cacheClusterEnabled", "value": "true"},
{"op": "replace", "path": "/cacheClusterSize", "value": "0.5"}
]'
# Set throttling
aws apigateway update-stage \
--rest-api-id abc123 \
--stage-name prod \
--patch-operations '[
{"op": "replace", "path": "/*/*/throttling/rateLimit", "value": "1000"},
{"op": "replace", "path": "/*/*/throttling/burstLimit", "value": "2000"}
]'Get Stage
aws apigateway get-stage \
--rest-api-id abc123 \
--stage-name prodDelete Stage
aws apigateway delete-stage \
--rest-api-id abc123 \
--stage-name devAPI Keys
Create API Key
aws apigateway create-api-key \
--name my-api-key \
--description "API key for my app" \
--enabledGet API Keys
aws apigateway get-api-keys --include-valuesDelete API Key
aws apigateway delete-api-key --api-key abc123Usage Plans
Create Usage Plan
aws apigateway create-usage-plan \
--name my-usage-plan \
--description "Standard usage plan" \
--throttle burstLimit=500,rateLimit=1000 \
--quota limit=10000,period=MONTH \
--api-stages apiId=abc123,stage=prodAdd API Key to Usage Plan
aws apigateway create-usage-plan-key \
--usage-plan-id plan123 \
--key-id key456 \
--key-type API_KEYGet Usage
aws apigateway get-usage \
--usage-plan-id plan123 \
--start-date 2024-01-01 \
--end-date 2024-01-31Authorizers
Create Lambda Authorizer
aws apigateway create-authorizer \
--rest-api-id abc123 \
--name my-authorizer \
--type TOKEN \
--authorizer-uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:authorizer/invocations \
--identity-source method.request.header.Authorization \
--authorizer-result-ttl-in-seconds 300Create Cognito Authorizer
aws apigateway create-authorizer \
--rest-api-id abc123 \
--name cognito-authorizer \
--type COGNITO_USER_POOLS \
--provider-arns arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_abc123 \
--identity-source method.request.header.AuthorizationGet Authorizers
aws apigateway get-authorizers --rest-api-id abc123Custom Domain Names
Create Domain Name
aws apigateway create-domain-name \
--domain-name api.example.com \
--regional-certificate-arn arn:aws:acm:us-east-1:123456789012:certificate/abc123 \
--endpoint-configuration types=REGIONALCreate Base Path Mapping
aws apigateway create-base-path-mapping \
--domain-name api.example.com \
--rest-api-id abc123 \
--stage prod \
--base-path v1Get Domain Names
aws apigateway get-domain-namesHTTP API (API Gateway v2)
Create HTTP API
aws apigatewayv2 create-api \
--name my-http-api \
--protocol-type HTTP \
--target arn:aws:lambda:us-east-1:123456789012:function:my-functionGet HTTP APIs
aws apigatewayv2 get-apisCreate Route
aws apigatewayv2 create-route \
--api-id abc123 \
--route-key "GET /users"Create Integration
aws apigatewayv2 create-integration \
--api-id abc123 \
--integration-type AWS_PROXY \
--integration-uri arn:aws:lambda:us-east-1:123456789012:function:my-function \
--payload-format-version 2.0Create Stage
aws apigatewayv2 create-stage \
--api-id abc123 \
--stage-name prod \
--auto-deployDelete HTTP API
aws apigatewayv2 delete-api --api-id abc123Export/Import
Export API
# OpenAPI 3.0
aws apigateway get-export \
--rest-api-id abc123 \
--stage-name prod \
--export-type oas30 \
api-export.jsonImport API
aws apigateway import-rest-api \
--body fileb://api-definition.jsonUpdate API from Import
aws apigateway put-rest-api \
--rest-api-id abc123 \
--mode overwrite \
--body fileb://api-definition.jsonTags
# Add tags
aws apigateway tag-resource \
--resource-arn arn:aws:apigateway:us-east-1::/restapis/abc123 \
--tags Environment=Production
# Get tags
aws apigateway get-tags \
--resource-arn arn:aws:apigateway:us-east-1::/restapis/abc123