Test the microservice

Please note the Cognito URL will change based on the AWS region in which the cognito user pool was created

You can test using cURL or Postman. Follow the section below Testing microservice APIs using Postman if you prefer testing with Postman

Testing microservice APIs using cURL

  • Sign-Up in Amazon Cognito for accessing the APIs

Execute below curl request to sign-up in Cognito

curl -X POST https://cognito-idp.us-west-2.amazonaws.com/ -H 'Content-Type: application/x-amz-json-1.1' -H 'cache-control: no-cache' -H 'x-amz-target: AWSCognitoIdentityProviderService.SignUp' -d '{"ClientId":"<YOUR APP CLIENT ID>","Username":"<YOUR EMAIL ID>","Password":"<YOUR PASSWORD>","UserAttributes":[{"Name":"email","Value":"<YOUR EMAIL ID>"}],"ValidationData":null}'
  • Check your email and copy the Verification code

  • Confirm the sign-up request

Replace the Verification Code and email-id in the below request and then execute below curl request to verify.

curl -X POST https://cognito-idp.us-west-2.amazonaws.com/ -H 'Content-Type: application/x-amz-json-1.1' -H 'cache-control: no-cache' -H 'x-amz-target: AWSCognitoIdentityProviderService.ConfirmSignUp' -d '{"ClientId":"<YOUR APP CLIENT ID>","ConfirmationCode":"<YOUR VERIFICATION CODE>","Username":"<YOUR EMAIL ID>","ForceAliasCreation":true}'
  • Get the ID Token to access the APIs

Execute the below Curl request to get the ID-Token from Cognito

curl -X POST https://cognito-idp.us-west-2.amazonaws.com/ -H 'Content-Type: application/x-amz-json-1.1' -H 'cache-control: no-cache' -H 'x-amz-target: AWSCognitoIdentityProviderService.InitiateAuth' -d '{"AuthFlow":"USER_PASSWORD_AUTH","ClientId":"<YOUR APP CLIENT ID>","AuthParameters":{"USERNAME":"<YOUR EMAIL ID>","PASSWORD":"<YOUR PASSWORD>"},"ClientMetadata":{}}' | jq '.AuthenticationResult.IdToken'
  • Copy the value of “IdToken” from the Response. You will use this token to make calls to the microservice APIs

  • Create an Application Resource

Please change the URL of the API with the URL of the API Gateway resources that you created. You can get the URL of the API from the API Gateway by visiting the API Gateway Console -> Stages -> resources -> method. You will find the Invoke URL on the right panel

Optionally change the JSON body in the below request and execute below HTTP POST request invoking Application microservice

curl -X POST <URL of your API> -H 'Authorization: <Your IdToken>' -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d '{"applicationId":"microservice-test-app","author":"team","description":"Reinvent microservice demo","homePageUrl":"https://www.amazon.com"}'
  • Get all the application resources

Replace the URL with your API URL under /applications resource in Amazon API Gateway console. To get all the application resources created so far, execute below GET method to get a list of all application resources.

curl -X GET <URL of your API> -H 'Authorization: <Your IdToken>' -H 'cache-control: no-cache' -H 'content-type: application/json'
  • Review the response.

The API should return all the application resources created via POST requests.

{
"applications": [
    {
        "applicationId": " microservice-test-app ",
        "description": " Reinvent microservice demo ",
        "creationTime": ""
    }
],
"nextToken": null
}
  • Optionally invoke DELETE, PATCH and GET Method of Application Microservice

Congratulations! You have successfully developed, secured, tested and deployed the microservice APIs on AWS!

Testing microservice APIs using Postman

This section requires Postman to be installed on your PC/Laptop. Follow the steps in the below section to test the APIs using Postman collection

  • Open the imported Collection

Replace the values with placeholders like <YOUR COGNITO APP CLIENT ID>, <YOUR EMAIL ID>, <YOUR PASSWORD>,<YOUR API Invoke URL>, <YOUR VERIFICATION CODE>

  • Prepare your API requests to be sent by replacing the values sequentially.
  • After replacing the values, start sending the requests in sequence till get-access-token-request request. This is the request that will give you the IdToken JWT Token that will be used to invoke your microservice APIs.
  • Copy the value of IdToken from the Response body of Request get-access-token-request as shown below

  • Replace the values of <YOUR IdToken> and <YOUR API Invoke URL> in the subsequent microservice API Invoke requests
  • The Create-Application-Request Request should return with HTTP Response code-200 OK as shown below to mark the successfull response of POST request

  • You can optionally get the Application resource you created above by sending a get-Application-request in postman collection. This will send a GET /applications microservice endpoint to get all the Application resources

Congratulations! You have successfully developed, secured, tested and deployed the microservice APIs on AWS!