Last active
May 5, 2023 18:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"appsync:ListGraphqlApis", | |
"appsync:ListApiKeys", | |
"appsync:UpdateApiKey" | |
], | |
"Resource": "*" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet @youssef-almardini! You're welcome. 💯