Let’s get started with a brief description of API Gateway. Amazon API Gateway is an AWS service that can be used to create, publish, maintain, secure and monitor for REST and WebSocket APIs worrying about scaling and underlying infrastructure.
What can API Gateway access as Back-end ?
API Gateway can be integrated with one of these back-ends.
1. AWS Lambda
2. Any HTTP endpoint
3. Mock Endpoint
4. Access APIs of supported AWS Services like S3, DynamoDB
5. Private Integration using VPCLink to access an endpoint inside AWS VPC
What are different ways of building a REST API as API Gateway resource ?
A REST API can be created using one of the following
1. AWS Console
2. AWS CLI
3. AWS SDK
4. Importing Open API definitions or swagger template
In this article, we will just focus on building a REST API using AWS CLI with Httpbin.org as back-end integration. Httpbin provides multiple endpoints to test various HTTP behavior.
We will use following endpoints from Httpbin to integrate with our API Gateway.
1. https://httpbin.org/ip – Returns the requester’s IP Address.
2. https://httpbin.org/status/{code} – Return status code or random status code if more than one are given
Let’s build and test the API resource /ip
Use create-rest-api CLI command to set up the RestApi in a specific region. I have used us-west-1 region in here but you can choose it to be any region.
Note down the id of the API just created. This would be needed to set up the next parts of your API. ’95rbczxv9j’ is the rest-api-id.
aws apigateway create-rest-api --name 'HTTP BIN (AWS CLI)' --region us-west-1
Use the get-resources CLI command to retrieve the root resource identifier of the RestApi.
aws apigateway get-resources --rest-api-id 95rbczxv9j --region us-west-1
Right now our API just has one resource which we call as root resource /.
Note the id of the root resource, it would be needed to set up other resources and the methods for those resources. ‘zw1g9tpoo9’ is the root resource id.
Use the create-resource command to add a child resource (ip) under the root resource.
aws apigateway create-resource --rest-api-id 95rbczxv9j \ --region us-west-1 \ --parent-id zw1g9tpoo9 \ --path-part ip
Use the put-method CLI command to add the GET HTTP method on the /ip resource. This creates an API Method of GET /ip with no authorization. This resource will have no access restrictions.
aws apigateway put-method --rest-api-id 95rbczxv9j \ --resource-id 5zxtsd \ --http-method GET \ --authorization-type "NONE" \ --region us-west-1
Use the put-method-response command to set up the 200 OK response of the GET /ip method. For this resource we just need a 200 OK response.
aws apigateway put-method-response --rest-api-id 95rbczxv9j \ --resource-id 5zxtsd \ --http-method GET \ --status-code 200 \ --region us-west-1
Use the put-integration command to set up an Integration with a https://httpbin.org/ip HTTP endpoint for the GET /ip method.
aws apigateway put-integration --rest-api-id 95rbczxv9j \ --resource-id 5zxtsd --http-method GET --type HTTP \ --integration-http-method GET \ --uri 'https://httpbin.org/ip' \ --region us-west-1
Use the put-integration-response command to create an IntegrationResponse of the GET /ip method integrated with an HTTP backend.
aws apigateway put-integration-response --rest-api-id 95rbczxv9j \ --resource-id 5zxtsd \ --http-method GET \ --status-code 200 \ --selection-pattern "" \ --region us-west-1
Deploy the API to a stage stage, using create-deployment CLI command.
aws apigateway create-deployment --rest-api-id 95rbczxv9j \
--region us-west-1 \
--stage-name test \
--stage-description 'Test stage' \
--description 'First deployment'
Test the deployed API Endpoint using cURL or Postman or Browser.
curl -v https://95rbczxv9j.execute-api.us-west-1.amazonaws.com/test/ip * Trying 99.86.212.38... * TCP_NODELAY set * Connected to 95rbczxv9j.execute-api.us-west-1.amazonaws.com (99.86.212.38) port 443 (#0) * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: *.execute-api.us-west-1.amazonaws.com * Server certificate: Amazon * Server certificate: Amazon Root CA 1 * Server certificate: Starfield Services Root Certificate Authority - G2 > GET /test/ip HTTP/1.1 > Host: 95rbczxv9j.execute-api.us-west-1.amazonaws.com > User-Agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: application/json < Content-Length: 43 < Connection: keep-alive < Date: Mon, 19 Aug 2019 01:45:23 GMT < x-amzn-RequestId: 35cfc655-19b2-4dd2-8500-4c17bb3f545f < x-amz-apigw-id: epTQDEs2SK4FmJA= < X-Amzn-Trace-Id: Root=1-5d59ff33-8b5c83aa45fe68931c9a3f6b < X-Cache: Miss from cloudfront < Via: 1.1 b044e0b5670e7555b57b4adf16d61701.cloudfront.net (CloudFront) < X-Amz-Cf-Pop: SYD4-C1 < X-Amz-Cf-Id: mQaf8bgkJVTGW_A7JI6CsArN8s_krVqjofCimJ0fUE5FrenwlLnMOA== < { "origin": "13.52.201.6, 13.52.201.6" } * Connection #0 to host 95rbczxv9j.execute-api.us-west-1.amazonaws.com left intact
Let’s build and test the API resource /status/{code}
This I would leave for you to try. Try below CLI commands in sequence !!!
aws apigateway create-resource --rest-api-id 95rbczxv9j --region us-west-1 --parent-id zw1g9tpoo9 --path-part status aws apigateway create-resource --rest-api-id 95rbczxv9j \ --region us-west-1 \ --parent-id u4ioey \ --path-part {statusCodes} aws apigateway put-method --rest-api-id 95rbczxv9j \ --resource-id 2pvhdz --http-method GET \ --authorization-type "NONE" \ --region us-west-1 \ --request-parameters method.request.path.statusCodes=true aws apigateway put-method --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi --http-method GET \ --authorization-type "NONE" \ --region us-west-1 \ --request-parameters method.request.path.statusCodes=true aws apigateway put-method-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 200 \ --region us-west-1 aws apigateway put-method-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 300 \ --region us-west-1 aws apigateway put-method-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 500 \ --region us-west-1 aws apigateway put-integration --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --type HTTP \ > --integration-http-method GET \ --uri "https://httpbin.org/status/{codes}" \ --request-parameters '{"integration.request.path.codes":"method.request.path.statusCodes"}' \ --region us-west-1 aws apigateway put-integration-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 200 \ --selection-pattern "" \ --region us-west-1 aws apigateway put-integration-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 500 \ --selection-pattern "5\d{2}" \ --region us-west-1 aws apigateway put-integration-response --rest-api-id 95rbczxv9j \ --resource-id 5yf2yi \ --http-method GET \ --status-code 300 \ --selection-pattern "3\d{2}" \ --region us-west-1 aws apigateway create-deployment --rest-api-id 95rbczxv9j \ --region us-west-1 \ --stage-name test \ --stage-description 'Test stage' \ --description 'Second deployment' curl -v https://95rbczxv9j.execute-api.us-west-1.amazonaws.com/test/status/200 curl -v https://95rbczxv9j.execute-api.us-west-1.amazonaws.com/test/status/500 curl -v https://95rbczxv9j.execute-api.us-west-1.amazonaws.com/test/status/302
We have reached the end of this article. We now know how to build and deploy and API Gateway using AWS CLI.
Have a look at all the published content on our Library page
I hope that you have enjoyed this article. Please do leave some feedback 🙂