버전 비교

  • 이 줄이 추가되었습니다.
  • 이 줄이 삭제되었습니다.
  • 서식이 변경되었습니다.

...

기존 url_short_stack.py 파일을 수정합니다. 아래에 scv@studydev.com 의 이메일 주소는 본인의 알림 받을 수 있는 Email로 변경합니다.

코드 블럭
languagepy
themeRDark
title url_shrotenershort_stack.py for cdk-watchful
linenumberstrue
import os
from aws_cdk import core, aws_dynamodb, aws_lambda, aws_apigateway, aws_ecs, aws_ec2
from traffic import Traffic
from cdk_watchful import Watchful

# we need default values here since aws-cdk-examples build synthesizes the app
ACCOUNT= os.environ['CDK_DEFAULT_ACCOUNT']
REGION = os.environ['CDK_DEFAULT_REGION']
VPC_ID = os.environ.get('TESTENV_VPC_ID', 'vpc-0e3593e52a4149549')
AWS_ENV = core.Environment(account=ACCOUNT, region=REGION)

class UrlShortStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, env=AWS_ENV, **kwargs)

        # The code that defines your stack goes here
        table = aws_dynamodb.Table(self, "mapping",
            partition_key = aws_dynamodb.Attribute(name="id",type=aws_dynamodb.AttributeType.STRING))
            
        function = aws_lambda.Function(self, "backend",
            runtime=aws_lambda.Runtime.PYTHON_3_7,
            handler="handler.main",
            code=aws_lambda.Code.asset("./lambda"))

        table.grant_read_write_data(function)
        function.add_environment("TABLE_NAME", table.table_name)
        
        api = aws_apigateway.LambdaRestApi(self, "api", handler=function)
        
        wf = Watchful(self, 'monitoring', alarm_email='scv@studydev.com')
        wf.watch_scope(self)
        
        
class TrafficStack(core.Stack):
    
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, env=AWS_ENV, **kwargs)
        
        # lookup our pre-created VPC by ID
        vpc_env = aws_ec2.Vpc.from_lookup(self, "vpc",
            vpc_id=VPC_ID)

        Traffic(self, 'TestTraffic',
            vpc= vpc_env,
            url="https://7vzqf6htub.execute-api.ap-southeast-1.amazonaws.com/prod/2a3c26e0",
            tps=10)

...

코드 블럭
languagebash
themeDJango
title모든 cdk 스택 배포
cdk deploy "*"

Image RemovedImage Added

하단의 모니터링을 위한 CloudWatch 대쉬보드 URL이 생성되는 것을 볼 수 있습니다. 해당 링크를 클릭합니다.

...

트래픽을 증가시켜서 DynamoDB의 RCU를 넘기도록 설정합니다. TPS를 10에서 15로 변경하여 트래픽을 절반 더 늘리고, 5분 정도 기다립니다.

코드 블럭
languagepy
themeRDark
title url_short_stack.py for cdk-watchful
linenumberstrue
import os
from aws_cdk import core, aws_dynamodb, aws_lambda, aws_apigateway, aws_ecs, aws_ec2
from traffic import Traffic
from cdk_watchful import Watchful

# we need default values here since aws-cdk-examples build synthesizes the app
ACCOUNT= os.environ['CDK_DEFAULT_ACCOUNT']
REGION = os.environ['CDK_DEFAULT_REGION']
VPC_ID = os.environ.get('TESTENV_VPC_ID', 'vpc-0e3593e52a4149549')
AWS_ENV = core.Environment(account=ACCOUNT, region=REGION)

class UrlShortStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, env=AWS_ENV, **kwargs)

        # The code that defines your stack goes here
        table = aws_dynamodb.Table(self, "mapping",
            partition_key = aws_dynamodb.Attribute(name="id",type=aws_dynamodb.AttributeType.STRING))
            
        function = aws_lambda.Function(self, "backend",
            runtime=aws_lambda.Runtime.PYTHON_3_7,
            handler="handler.main",
            code=aws_lambda.Code.asset("./lambda"))

        table.grant_read_write_data(function)
        function.add_environment("TABLE_NAME", table.table_name)
        
        api = aws_apigateway.LambdaRestApi(self, "api", handler=function)
        
        wf = Watchful(self, 'monitoring', alarm_email='scv@studydev.com')
        wf.watch_scope(self)
        
        
class TrafficStack(core.Stack):
    
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, env=AWS_ENV, **kwargs)
        
        # lookup our pre-created VPC by ID
        vpc_env = aws_ec2.Vpc.from_lookup(self, "vpc",
            vpc_id=VPC_ID)

        Traffic(self, 'TestTraffic',
            vpc= vpc_env,
            url="https://7vzqf6htub.execute-api.ap-southeast-1.amazonaws.com/prod/2a3c26e0",
            tps=15)


코드 블럭
languagebash
themeDJango
title모든 cdk 스택 배포
cdk deploy "*"


DynamoDB Console에서도 동일한 메트릭 정보를 확인할 수 있습니다. RCU가 부족한 현상이 발생합니다.

...