For a reason I won't mention here, I need to give a random person with no account a secret link to my HTTP API method which calls a lambda function in AWS. The API method needs to be under authorization, but I am unsure on what is the best approach. Ideally, the link should only be allowed to be used one time. If not possible, perhaps some TTL could suffice.
My backend needs to generate the link using some AWS .NET SDK.
I am relatively new to AWS, and there are just so many options. What should I do? Should I sign the link somehow, should I use AWS Cognito somehow? Should I create a custom lambda authorizer which accesses DynamoDB for some token?
What is the simplest and cheapest way?
You can achieve this using IAM role with only permissions to invoke API, and AWS temporary credentials generated by AWS STS. It is similiar to the process described in this doc. The temporary token generated by AWS STS have a minimum of 15 min TTL and maximum of 12 hours TTL.
First, create API gateway and its method that uses IAM authorizer. Then you can create an IAM role that contains only the permission to invoke the API endpoint and its method.
the following is example CDK code to create the aws resources
Second, you can generate temporary credentials to assume the lambdaGuestRole. Assuming your backend is a lambda on the same aws account, it will be able to assume the lambdaGuestRole, and get the temporary credentials.
the following is example python code to fetch temporary credentials for the assumed role. It uses the default TTL duration of 1 hour. See AssumeRole doc to customize TTL duration. This token can be provided to your users to invoke the API. The AWS .NET SDK have similar logic to fetch the credentials.
Third, your users can use the temporary credentials to invoke your API as needed.
the following is example python code to invoke API using temporary credentials. Note that the credentials are part of the header, which can not be included in a link.
You can also generate an URL for AWS Management Console access, and your users can invoke the API from AWS console.