Skip to content

Instantly share code, notes, and snippets.

@sators
Last active May 5, 2023 18:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sators/1d10e81bc1667994dea382fcb35c4000 to your computer and use it in GitHub Desktop.
Save sators/1d10e81bc1667994dea382fcb35c4000 to your computer and use it in GitHub Desktop.
Set all App Sync API Key Expiration Dates to 365 Days from <Today> to support Public / Guest APIs
var AWS = require('aws-sdk');
async function asyncForEach(array, callback)
{
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
exports.handler = async (event) =>
{
const response = {
statusCode: 500,
body: JSON.stringify("Error"),
};
var keyCount = 0;
var appsync = new AWS.AppSync({ apiVersion: '2017-07-25' });
var d = new Date();
d.setDate(d.getDate() + 365);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
const expires = d / 1000 | 0;
const graphQlResponse = await appsync.listGraphqlApis().promise();
if (!graphQlResponse.graphqlApis || graphQlResponse.graphqlApis.length === 0) {
response.statusCode = 200;
response.body = JSON.stringify("No APIs found.");
return response;
}
await asyncForEach(graphQlResponse.graphqlApis, async api =>
{
const apiId = api.apiId;
const keysResponse = await appsync.listApiKeys({ apiId }).promise();
if (!keysResponse.apiKeys || keysResponse.apiKeys.length === 0) {
return;
}
await asyncForEach(keysResponse.apiKeys, async key => {
var params = {
apiId,
id: key.id,
expires,
};
const result = await appsync.updateApiKey(params).promise();
if (result.apiKey){
keyCount++;
}
});
});
response.statusCode = 200;
response.body = JSON.stringify(`${keyCount} key${keyCount !== 1 ? "s" : ""} updated.`);
return response;
};
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"appsync:ListGraphqlApis",
"appsync:ListApiKeys",
"appsync:UpdateApiKey"
],
"Resource": "*"
}
]
}
@sators
Copy link
Author

sators commented Dec 29, 2022

Sweet @youssef-almardini! You're welcome. 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment