I've created an AWS Amplify function with amplify add function
resulting in the following basic configuration:
General information
- Name: MyFunction
- Runtime: python
Resource access permission
- Not configured
Scheduled recurring invocation
- Not configured
Lambda layers
- Not configured
Environment variables:
- Not configured
Secrets configuration
- Not configured
I then added a REST API using amplify add api
that uses this function, and added a path with "create" and "read" access for authenticated users resulting in the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"execute-api:Invoke"
],
"Resource": [
"arn:aws:execute-api:us-west-2:...:.../staging/POST/reply/*/*",
"arn:aws:execute-api:us-west-2:...:.../staging/POST/reply/*",
"arn:aws:execute-api:us-west-2:...:.../staging/GET/reply/*/*",
"arn:aws:execute-api:us-west-2:...:.../staging/GET/reply/*" ],
"Effect": "Allow"
}
]
}
But when I invoke the API from my app, with a logged in authenticated user (who has no trouble using my GraphQL API via DataStore
) I get a 403 error.
I can't figure out what's happening here. What would cause a 403 error in this case? This is all pretty much out of the box from the Amplify CLI. What's wrong with the authentication I'm providing?
The code for the Lambda function (generated by the CLI, with no further edits) is:
def handler(event, context):
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps('Hello from your new Amplify Python lambda!')
}
The invoking code (copied from the Amplify documentation) is:
import ... { API } from 'aws-amplify'
// ...
const callLambdaFunction = async () => {
try {
const response = await API.post( 'Chat', '/reply/whatever', {
body: { data },
headers: {
Authorization: `Bearer ${ (await Auth.currentSession())
.getIdToken()
.getJwtToken() }`,
},
} )
setResult( response )
}
catch ( error ) {
console.log( error )
}
}
Some notes:
- Why do I even need to provide authentication? Doesn't the API.post already know about the currently authenticated user and append the necessary headers? DataStore does.
- What do "read", "create", etc. mean in the
amplify api
CLI? How do the relate to what the endpoint does or is, or who can access it. Is it a secret code for "GET", "POST", etc.? - I've tried pasting the JWT I get from
Auth.currentSession
into Postman but get nonsense:
even if I just paste random text.{ "message": "'eyJhbG...0HMs' not a valid key=value pair (missing equal-sign) in Authorization header: 'Bearer eyJhbG...0HMs'." }