My handler named taskToFireOff has allUsers
deleted, the Cloud Tasks Enqueuer
principal's name is set to the ServiceAccountEmail
, and its role is set to Invoker
(not shown)
When a post is created, I use .onCreate
to create a task. The task is created but the function that the http request is connected to never fires. I looked here https://console.cloud.google.com/cloudtasks/queue/ and see that there are 2 tasks (I tried twice) just sitting there.
When I go to the Cloud Task page in the console > click the task > Tasks > Method / URL
, it says
POST https://us-east1-myProjectId.cloudfunctions.net/taskToFireOff
But when I go to the task > Logs > View > Suggested > Query Details it says
New error group seen in onCreateData: Error: 3 INVALID_ARGUMENT: Any resource that needs App Engine can only be created/updated in the App Engine region. Location must equal us-east1 because the App
It's an odd error because the above url begins with us-east1 and in my index.js file I set the location using us-east1
exports.onCreateData = functions.database.ref('/posts/{postId}').onCreate(async (snapshot, context) => {
const payload = {"postId": context.params.postId};
const project = JSON.parse(process.env.FIREBASE_CONFIG!).projectId;
const location = 'us-east1';
const queue = 'ttl-task';
const serviceAccountEmail = '[email protected]';
const queuePath = tasksClient.queuePath(project, location, queue);
const url = `https://${location}-${project}.cloudfunctions.net/taskToFireOff`
const task = {
httpRequest: {
httpMethod: 'POST',
url,
oidcToken: {
serviceAccountEmail
},
body: Buffer.from(JSON.stringify(payload)).toString('base64'), // JSON.parse(JSON.stringify(payloadData));
headers: {
'Content-Type': 'application/json',
},
},
scheduleTime: {
seconds: // ...
}
}
const [response] = await tasksClient.createTask({ parent: queuePath, task });
// ...
});
exports.taskToFireOff = functions.https.onRequest((request, response) => {
// ...
});
Also while inside the console, when I click the Task itself and look at the payload, all the correct information is there.
When I created my RealTimeDatabase I was given us-central1
But when I created my Storage bucket, I selected us-east1
. At the bottom of Firestore Database it says
When I enter $ gcloud app describe
it says that the locationId is us-east1
Could the problem be that the Cloud Task is in us-east1
but the Cloud Functions are in us-central1
?
Running $ gcloud functions list
lists all of my Cloud Functions in the us-central1
region
Btw, I created another project just to see the selection that I'm given when creating a Storage bucket. For Storage, there isn't a way to select us-central1. There is a default Multi-region nam5(us-central
) but there isn't a specific region for us-central1
. I selected us-east1
because I'm in NY.
The problem was that the App Engine was in us-east1
but the function handler taskToFireOff was in us-central1
. The error was saying that the function associated with App Engine/Cloud Task needs to be in the same region together. In short I needed to change that particular cloud function’s region.
To fix the issue I had to create a new handler which was pretty easy. I followed this answer which led me to this link specifically the Change a function's region or regions section.
1st- in terminal I ran:
$ gcloud app describe
// use whatever appears for the locationId which for me was us-east1
2nd- I added this new function to the index.js file:
// whatever appeared for the locationId I put inside the region param
exports.newTaskToFireOff = functions.region('us-east1').https.onRequest((request, response) => {
// ... in here I c+p all of the code from my initial taskToFireOff function
});
3rd- I ran this in terminal to update gcloud with the new function
$ firebase deploy --only functions:newTaskToFireOff
4th- I ran this in terminal to delete the old function from gcloud:
$ firebase functions: delete taskToFireOff
5th- I went into my index.js file and manually deleted the entire taskToFireOff. You won't need the old function, just the code from inside of it
// I deleted this old function
exports.taskToFireOff = functions.https.onRequest((request, response) => {
// ... I took this code and c+p inside the newTaskToFireOff before I deleted this function
});
7th-
When I go to the console's Cloud Functions page the old taskToFireOff is no longer there and the newTaskToFireOff is there with the region us-east1
After you finish these steps don’t forget that the new function from step 2 you have to go to the Cloud console (use the above link) and change its Permissions
. Set the role to Invoker
and set its principal role name to use whatever you set for the Cloud Task's ServiceAccountEmail
otherwise it still won’t work. You should delete allUsers
too.