Testing the APIs

Go back to your Cloud9 environment and open a terminal tab.

Development Environment Testing

Export the Dev stack output variables

To invoke our API’s, we first need to fetch the ApiUrl output variable that our CloudFormation Development stack gives us. So let us iterate through our stack and export all output variables as environment variables.

export ApiUrl=$(aws cloudformation describe-stacks --stack-name demo-service-Dev --output json | jq '.Stacks[].Outputs[] | select(.OutputKey=="ApiUrl") | .OutputValue' | sed -e 's/^"//'  -e 's/"$//')
echo "export ApiUrl="$ApiUrl

Test the Put Item operation

curl -X POST \
  $ApiUrl/items/ \
  -d '{
        "id":"1",  
        "name": "Development Test Item"
  }'

Test the Get Item by Id operation

curl -X GET $ApiUrl/items/1 | jq

Your expected output should be:

{"id":"1","name":"Development Test Item"}

Production Environment Testing

Export the Prod stack output variables

Let’s override our environment variables with the values from the Production stack.

export ApiUrl=$(aws cloudformation describe-stacks --stack-name demo-service-Prod --output json | jq '.Stacks[].Outputs[] | select(.OutputKey=="ApiUrl") | .OutputValue' | sed -e 's/^"//'  -e 's/"$//')
echo "export ApiUrl="$ApiUrl

Test the Get Item by Id operation

Let’s first make sure that we are calling a different endpoint return different data.

curl -X GET $ApiUrl/items/1 | jq

Your expected output should be:

{}

Test the Put Item operation

curl -X POST \
  $ApiUrl/items/ \
  -d '{
        "id":"1",  
        "name": "Production Test Item"
  }'

Test the Get Item by Id operation again

curl -X GET $ApiUrl/items/1 | jq

Your expected output should be:

{"id":"1","name":"Production Test Item"}