Skip to content

Commit

Permalink
Merge pull request #13 from grishasergii/feature/enable-api-access-logs
Browse files Browse the repository at this point in the history
Feature/enable api access logs
  • Loading branch information
grishasergii authored Dec 30, 2020
2 parents 8ff6579 + 53559e6 commit e772a02
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.0
current_version = 0.4.0
commit = True
tag = False
tag_name = v{new_version}
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.3.0
version=0.4.0
48 changes: 40 additions & 8 deletions ci/deploy_api_gateway_stage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import click
import json

from ci.external_cmd import ExternalCmd


def _get_output_from_stack_description(stack_description, output_key):
for output in stack_description["Outputs"]:
if output["OutputKey"] == output_key:
return output["OutputValue"]
raise KeyError(f"{output_key} not found")


@click.command()
@click.option("--stack-name", help="Name of the stack")
@click.option("--stage-name", help="Name of the api stage")
Expand All @@ -11,21 +19,45 @@ def deploy(stack_name, stage_name):
stack_descriptions = ExternalCmd.run_and_parse_json(
f"aws cloudformation describe-stacks --stack-name {stack_name}"
)
api_id = None
stack_description = stack_descriptions["Stacks"][0]
for output in stack_description["Outputs"]:
if output["OutputKey"] == "RestApiId":
api_id = output["OutputValue"]
break

if not api_id:
raise click.ClickException("RestApiId not found in stack outputs")
stack_description = stack_descriptions["Stacks"][0]
api_id = _get_output_from_stack_description(stack_description, "RestApiId")
log_group_arn = _get_output_from_stack_description(stack_description, "ApiGatewayAccessLogGroupArn")
log_group_arn = log_group_arn.rstrip(":*")

click.echo(f"API id is {api_id}")
click.echo(f"Deploying {stage_name} stage...")
ExternalCmd.run(
f"aws apigateway create-deployment --rest-api-id {api_id} --stage-name {stage_name}"
)
click.echo("Deployment done")

click.echo("Enabling access logging...")
operations = [
{
"op": "add",
"path": "/accessLogSettings/destinationArn",
"value": f"{log_group_arn}"
},
{
"op": "add",
"path": "/accessLogSettings/format",
"value": "{ \"requestId\":\"$context.requestId\", \"ip\": \"$context.identity.sourceIp\", \"caller\":\"$context.identity.caller\", \"user\":\"$context.identity.user\",\"requestTime\":\"$context.requestTime\", \"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\", \"status\":\"$context.status\",\"protocol\":\"$context.protocol\", \"responseLength\":\"$context.responseLength\" }"
},
{
"op": "replace",
"path": "/*/*/logging/loglevel",
"value": "INFO"
},
{
"op": "replace",
"path": "/*/*/logging/dataTrace",
"value": "true"
}
]
ExternalCmd.run(
f"aws apigateway update-stage --rest-api-id {api_id} --stage-name {stage_name} --patch-operations '{json.dumps(operations)}'"
)
click.echo("Done")


Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Kyivmural API

version 0.3.0
version 0.4.0

API component of kyivmural.com
2 changes: 1 addition & 1 deletion templates/api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ openapi: 3.0.1
info:
title: Kyivmural API
description: Kyivmural API
version: 0.3.0
version: 0.4.0
tags:
- name: mural
description: Operations about murals
Expand Down
2 changes: 1 addition & 1 deletion templates/api.cfn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
AWSTemplateFormatVersion: 2010-09-09
Description: Kyivmural API v0.3.0 - API
Description: Kyivmural API v0.4.0 - API

Parameters:
Branch:
Expand Down
2 changes: 1 addition & 1 deletion templates/backend.cfn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
AWSTemplateFormatVersion: 2010-09-09
Description: Kyivmural API v0.3.0 - Backend
Description: Kyivmural API v0.4.0 - Backend

Parameters:
MuralsTableName:
Expand Down
2 changes: 1 addition & 1 deletion templates/database.cfn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
AWSTemplateFormatVersion: 2010-09-09
Description: Kyivmural API v0.3.0 - Database
Description: Kyivmural API v0.4.0 - Database

Parameters:
Branch:
Expand Down
2 changes: 1 addition & 1 deletion templates/kyivmural-api.cfn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Kyivmural API v0.3.0
Description: Kyivmural API v0.4.0

Parameters:
Branch:
Expand Down

0 comments on commit e772a02

Please sign in to comment.