Last active
April 24, 2021 04:33
-
-
Save hugosenari/20ca4715946333c1d957523cb1afc699 to your computer and use it in GitHub Desktop.
List AWS lambda of API id
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
#!/usr/bin/env nim | |
import json | |
import options | |
import os | |
import sequtils | |
import strutils | |
import strformat | |
type | |
CmdResultKind = enum | |
crkSuccess, crkFail | |
CmdResult* = ref object | |
case kind*: CmdResultKind | |
of crkFail: | |
error*: string | |
errorNo*: int | |
else: | |
output*: string | |
CmdResponse = tuple[output:string, exitCode: int] | |
proc success*(output: string): CmdResult = | |
CmdResult(kind: crkSuccess, output: output) | |
proc fail*(error: string, errorNo: int): CmdResult = | |
CmdResult(kind: crkFail, error: error, errorNo: errorNo) | |
proc failed*(cmdResult: CmdResult): bool = | |
cmdResult.kind == crkFail | |
proc succeded*(cmdResult: CmdResult): bool = | |
cmdResult.kind == crkSuccess | |
const CMD_OK = 0 | |
proc toResult*(response: CmdResponse): CmdResult = | |
if response.exitCode == CMD_OK: | |
success response.output | |
else: | |
fail response.output, response.exitCode | |
proc run*(cmd: string): CmdResult = | |
toResult gorgeEx cmd | |
proc read*(file_path: string): CmdResult = | |
run "cat {file_path}".fmt | |
proc write*(file_path, content: string): CmdResult = | |
run """ | |
cat << EOF > {file_path} | |
{content}""".fmt | |
proc getResources*(restApiId: string): JsonNode = | |
let | |
cache_file_path = "/tmp/gateway_functions_{restApiId}.json".fmt | |
cached_json = read cache_file_path | |
if cached_json.succeded: | |
return parseJson cached_json.output | |
let newResponse = run "aws apigateway get-resources --output json --rest-api-id {restApiId} --embed methods".fmt | |
if newResponse.failed: | |
quit newResponse.error, newResponse.errorNo | |
discard write(cache_file_path, newResponse.output) | |
return parseJson newResponse.output | |
type | |
LambdaArn* = string | |
ApiMethod* = object | |
id, path, methodName: string | |
arn: LambdaArn | |
proc region*(lambdaArn: LambdaArn): string = | |
lambdaArn.split(':')[3] | |
proc functionName*(lambdaArn: LambdaArn): string = | |
lambdaArn.replace("/invocations", "").split(':')[11] | |
proc consoleUrls*(restApiId: string, apiMethod: ApiMethod): string = | |
const awsUrl = "https://console.aws.amazon.com" | |
let | |
functionName = apiMethod.arn.functionName | |
region = apiMethod.arn.region | |
lambdaUrl = "{awsUrl}/lambda/home?region={region}#/functions/{functionName}".fmt | |
logPath = "logsV2:log-groups/log-group/$252Faws$252Flambda$252F" | |
logTime = if paramCount() >= 3: paramStr(3) else: "-3600000" | |
logUrl = "{awsUrl}/cloudwatch/home?region={region}#{logPath}{functionName}/log-events$3Fstart$3D{logTime}".fmt | |
apiGUrl = "{awsUrl}/apigateway/home?region={region}#/apis/{restApiId}/resources/{apiMethod.id}/methods/{apiMethod.methodName}".fmt | |
""""{apiMethod.path}","{apiMethod.methodName}","{apiGUrl}","{lambdaUrl}","{logUrl}"""".fmt | |
iterator hasResourceMethods(items: JsonNode): JsonNode = | |
for item in items: | |
if item{"resourceMethods"}.isNil: | |
continue | |
yield item | |
iterator hasArn(item: JsonNode): tuple[key: string, value: JsonNode] = | |
for methodName, methodInfo in item["resourceMethods"]: | |
if (isNil methodInfo{"methodIntegration"}) or | |
(isNil methodInfo["methodIntegration"]{"uri"}): | |
continue | |
yield (methodName, methodInfo) | |
iterator apiMethods(items: JsonNode): ApiMethod = | |
for item in items.hasResourceMethods: | |
for methodName, methodInfo in item.hasArn: | |
yield ApiMethod( | |
id: item["id"].getStr, | |
path: item["path"].getStr, | |
methodName: methodName, | |
arn: methodInfo["methodIntegration"]{"uri"}.getStr) | |
when isMainModule: | |
const FIRST_PARAMTER = 2 | |
let | |
restApiId = paramStr FIRST_PARAMTER | |
content = getResources restApiId | |
echo """"path","httpMethod","apigatewayURL","lambdaURL","logURL"""" | |
for item in content["items"].apiMethods: | |
echo consoleUrls(restApiId, item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements:
Configured aws cli
Usage: