There are more AWS SDK examples available in the AWS Doc SDK Examples
API Gateway examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with API Gateway.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use create-api-key.
- AWS CLI
-
To create an API key that is enabled for an existing API and Stage
Command:
aws apigateway create-api-key --name 'Dev API Key' --description 'Used for development' --enabled --stage-keys restApiId='a1b2c3d4e5',stageName='dev'-
For API details, see CreateApiKey
in AWS CLI Command Reference.
-
The following code example shows how to use create-authorizer.
- AWS CLI
-
Example 1: To create a token-based API Gateway Custom Authorizer for the API
The following
create-authorizerexample creates a token-based authorizer.aws apigateway create-authorizer \ --rest-api-id1234123412\ --name 'First_Token_Custom_Authorizer' \ --typeTOKEN\ --authorizer-uri 'arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:customAuthFunction/invocations' \ --identity-source 'method.request.header.Authorization' \ --authorizer-result-ttl-in-seconds300Output:
{ "authType": "custom", "name": "First_Token_Custom_Authorizer", "authorizerUri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:customAuthFunction/invocations", "authorizerResultTtlInSeconds": 300, "identitySource": "method.request.header.Authorization", "type": "TOKEN", "id": "z40xj0" }Example 2: To create a Cognito User Pools based API Gateway Custom Authorizer for the API
The following
create-authorizerexample creates a Cognito User Pools based API Gateway Custom Authorizer.aws apigateway create-authorizer \ --rest-api-id1234123412\ --name 'First_Cognito_Custom_Authorizer' \ --typeCOGNITO_USER_POOLS\ --provider-arns 'arn:aws:cognito-idp:us-east-1:123412341234:userpool/us-east-1_aWcZeQbuD' \ --identity-source 'method.request.header.Authorization'Output:
{ "authType": "cognito_user_pools", "identitySource": "method.request.header.Authorization", "name": "First_Cognito_Custom_Authorizer", "providerARNs": [ "arn:aws:cognito-idp:us-east-1:342398297714:userpool/us-east-1_qWbZzQhzE" ], "type": "COGNITO_USER_POOLS", "id": "5yid1t" }Example 3: To create a request-based API Gateway Custom Authorizer for the API
The following
create-authorizerexample creates a request-based authorizer.aws apigateway create-authorizer \ --rest-api-id1234123412\ --name 'First_Request_Custom_Authorizer' \ --typeREQUEST\ --authorizer-uri 'arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:customAuthFunction/invocations' \ --identity-source 'method.request.header.Authorization,context.accountId' \ --authorizer-result-ttl-in-seconds300Output:
{ "id": "z40xj0", "name": "First_Request_Custom_Authorizer", "type": "REQUEST", "authType": "custom", "authorizerUri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:customAuthFunction/invocations", "identitySource": "method.request.header.Authorization,context.accountId", "authorizerResultTtlInSeconds": 300 }-
For API details, see CreateAuthorizer
in AWS CLI Command Reference.
-
The following code example shows how to use create-base-path-mapping.
- AWS CLI
-
To create the base path mapping for a custom domain name
Command:
aws apigateway create-base-path-mapping --domain-namesubdomain.domain.tld--rest-api-id1234123412--stageprod--base-pathv1-
For API details, see CreateBasePathMapping
in AWS CLI Command Reference.
-
The following code example shows how to use create-deployment.
- AWS CLI
-
To deploy the configured resources for an API to a new Stage
Command:
aws apigateway create-deployment --rest-api-id1234123412--stage-namedev--stage-description 'Development Stage' --description 'First deployment to the dev stage'To deploy the configured resources for an API to an existing stage
Command:
aws apigateway create-deployment --rest-api-id1234123412--stage-namedev--description 'Second deployment to the dev stage'To deploy the configured resources for an API to an existing stage with Stage Variables
aws apigateway create-deployment --rest-api-id 1234123412 --stage-name dev --description 'Third deployment to the dev stage' --variables key='value',otherKey='otherValue'
-
For API details, see CreateDeployment
in AWS CLI Command Reference.
-
The following code example shows how to use create-domain-name-access-association.
- AWS CLI
-
To create a domain name access association
The following
create-domain-name-access-associationexample creates a domain name access association between a private custom domain name and VPC endpoint.aws apigateway create-domain-name-access-association \ --domain-name-arnarn:aws:apigateway:us-west-2:111122223333:/domainnames/my.private.domain.tld+abcd1234\ --access-association-sourcevpce-abcd1234efg\ --access-association-source-typeVPCEOutput:
{ "domainNameAccessAssociationArn": "arn:aws:apigateway:us-west-2:012345678910:/domainnameaccessassociations/domainname/my.private.domain.tld/vpcesource/vpce-abcd1234efg "accessAssociationSource": "vpce-abcd1234efg", "accessAssociationSourceType": "VPCE", "domainNameArn" : "arn:aws:apigateway:us-west-2:111122223333:/domainnames/private.example.com+abcd1234" }For more information, see Custom domain names for private APIs in API Gateway in the Amazon API Gateway Developer Guide.
-
For API details, see CreateDomainNameAccessAssociation
in AWS CLI Command Reference.
-
The following code example shows how to use create-domain-name.
- AWS CLI
-
Example 1: To create a public custom domain name
The following
create-domain-nameexample creates a public custom domain name.aws apigateway create-domain-name \ --domain-name 'my.domain.tld' \ --certificate-name 'my.domain.tld cert'\ --certificate-arn 'arn:aws:acm:us-east-1:012345678910:certificate/fb1b9770-a305-495d-aefb-27e5e101ff3'Output:
{ "domainName": "my.domain.tld", "certificateName": "my.domain.tld cert", "certificateArn": "arn:aws:acm:us-east-1:012345678910:certificate/fb1b9770-a305-495d-aefb-27e5e101ff3", "certificateUploadDate": "2024-10-08T11:29:49-07:00", "distributionDomainName": "abcd1234.cloudfront.net", "distributionHostedZoneId": "Z2FDTNDATAQYW2", "endpointConfiguration": { "types": [ "EDGE" ] }, "domainNameStatus": "AVAILABLE", "securityPolicy": "TLS_1_2" }For more information, see Custom domain name for public REST APIs in API Gateway in the Amazon API Gateway Developer Guide.
Example 2: To create a private custom domain name
The following
create-domain-nameexample creates a private custom domain name.aws apigateway create-domain-name \ --domain-name 'my.private.domain.tld' \ --certificate-name 'my.domain.tld cert' \ --certificate-arn 'arn:aws:acm:us-east-1:012345678910:certificate/fb1b9770-a305-495d-aefb-27e5e101ff3' \ --endpoint-configuration '{"types": ["PRIVATE"]}' \ --security-policy 'TLS_1_2' \ --policyfile://policy.jsonContents of
policy.json:{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": "*", "Action": "execute-api:Invoke", "Resource": [ "execute-api:/*" ] }, { "Effect": "Deny", "Principal": "*", "Action": "execute-api:Invoke", "Resource": [ "execute-api:/*" ], "Condition" : { "StringNotEquals": { "aws:SourceVpce": "vpce-abcd1234efg" } } } ] }Output: