We will tackle this together stay with me :).
By: Jay Rao
Tags: Integration, AWS, AWS Lambda, Aws Cognito, Aws Api Gateway

We will tackle this together stay with me :).

You need to have access to an AWS account. I logged in as root user in AWS. Install Visual Studio 2019/2017. AWS studio toolkit installed on your computer.

Part one: Build sample .net core Rest API

Refer the below link to do that

Create a sample rest API application

Copy the API URL someplace you used in postman from the above tutorial, We will require it during API gateway integration. My URL looked this way you

https://m40spcat1h.execute-api.us-east-2.amazonaws.com/Prod/api/ShoppingList

Part Two: Creating a congnito pool.

In the AWS console select Cognito. Click on Manage User pools. Create user pools. Give a pool name. (All the names must contain only lower case letters and hyphen or so special characters.) Click on Review Defaults. Click on Add App client.

7. Again click on AddAppClient option.

8. Give the app client name of your choice.

9. Enable username password-based authentication (ALLOW_USER_PASSWORD_AUTH).

10. Hit the Create app client option.

11. Click on create pool option.

12. Click on Domain name.

13. Give a valid domain name(name related to your API would be more appropriate) copy the domain name in this (mydomain125) and save changes.

14. Click on Resource Servers that you can see in step 12.

15. Fill the fields as I have shown in the Image again choose a name appropriate to your application.

Scopes are added so that you can restrict API’s capability say you want a particular API only to get not create you can do that using the scopes. I recommend you use that in this case. Then click save changes.

16. Click on App client Settings.

Click on select all.

Scroll and select what I have high lighted.

Client Credentials will be used for authentication purpose and scopes, as mentioned, are useful to give necessary capabilities to API.

17. Client Credentials are in AppClient.

Copy your ClientId and Client Secret Someplace.

You are set with Cognito. Yipeee :).

Part three: Creating API gate way and integrating with Cognito.

In Aws Console choose API Gateway

Hit CreateAPI button. Select the one configuration you require, I have selected the below and select build.

3. Next Choose New API and give valid and human-friendly API name.

4. Choose CreateAPI Option.

5. Click on Actions and select Create Method.

6. And select appropriate CRUD method, In the above rest API, we have GET So I have selected GET to demonstrate the Cognito Integration. Select the check icon after that.

7. Endpoint URL is the API that you got after deploying to AWS, Refer part 1 if you are confused and hit save.

8. Choose Authorizers in the same window and hit CreateNewAuthorizer button.

Give a valid name and select the type as Cognito and the Cognito User Pool field will give a drop-down with the Cognito User pool we Created in Part 2. Choose that.

Token Source Give it as Authorization.

9. Click on Resources and Select Get which currently shows authorization as None. Click on Get

10. Click on Method Request.

11. In Authorization drop-down select the Authorizer you created in step 8.

OAuth Scopes, you can find it in AppClientSettings

Repeat steps 9 through 11 for all the methods.

12. Voila you have now integrated Cognito with API gateway. Deploy and test the gateway using Postman.

Testing our Application Set up.

Fire up the postman, Click on new request

You need to keep these things ready to get access token

Auth Url

https://{your-domain-name}.{region}.amazoncognito.com/oauth2/token

In my case it was domain125

You will need your Client Secret and Client Id, Refer step 17 part 2.

Don’t forget to set the Grant type to Authorization, Click on GetNewAccessTokenButton, You will get new Token.

Set the Authorization in Headers Token and hit send you will get response

I have tried to keep this article as short as possible, In case if you have any question leave a comment I will try to get back to you at the earliest.

Say cheese Chickpeas :). hehe