...
위 Test URL은 API Gateway에서 생성하여 제공하는 URL입니다. 따라서, URL 호출을 하기 위해서 URL이 길어집니다.
따라서, 만약 Route 53에 도메인이 등록되어 있고, 해당 도메인을 이용하여 API Gateway에 Custom Domain으로 설정을 할 Domain을 설정할 수 있습니다.
예를 들어, https://vp8tpwdmd4.execute-api.ap-northeast-2.amazonaws.com/prod/7c9b02b8 라는 URL 대신, https://cdk.awsdemo.kr/7c9b02b8 변경할 수 있습니다.
또한, Lambda 함수를 수정하거나(쿼리스트링의 매개변수를 추가로 받거나), DynamoDB에 들어있는 내용을 추가하여 https://cdk.awsdemo.kr/hol-short-url 이름으로 현재 wiki 페이지로 접속하도록 설정할 수 있습니다.
먼저 Certificate Manager에서 Custom Domain으로 사용할 인증서를 생성하고, 인증서와 Route 53에 등록된 Zone 정보를 기반으로 아래 코드에 반영합니다.
성능 테스트는 종료되었으므로 tps 값은 0으로 하거나 또는 해당 스택을 destory 할 하여 운용할 수 있습니다.
코드 블럭 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
from aws_cdk import core, aws_dynamodb, aws_lambda, aws_apigateway, aws_ec2, aws_route53, aws_route53_targets, aws_certificatemanager import os 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-07aa21d32b2df894f') ZONE_NAME = os.environ.get('TESTENV_ZONE_NAME', 'awsdemo.kr') ZONE_ID = os.environ.get('TESTENV_ZONE_ID', 'Z2XXXXXXXXXXYO') ZONE_CERT = os.environ.get('TESTENV_ZONE_CERT', 'arn:aws:acm:ap-northeast-2:123456789012:certificate/8d2adfd2-xxxx-xxxx-abcd-2c50292xxxx') 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) # Custom Domain self.map_subdomain("cdk", api) def map_subdomain(self, subdomain: str, api: aws_apigateway.RestApi) -> str: """ Maps a sub-domain of waltersco.co to an API gateway :param subdomain: The sub-domain (e.g. "www") :param api: The API gateway endpoint :return: The base url (e.g. "https://www.waltersco.co") """ domain_name = subdomain + '.' + ZONE_NAME url = 'https://' + domain_name cert = aws_certificatemanager.Certificate.from_certificate_arn(self, 'DomainCertificate', ZONE_CERT) hosted_zone = aws_route53.HostedZone.from_hosted_zone_attributes(self, 'HostedZone', hosted_zone_id=ZONE_ID, zone_name=ZONE_NAME) # add the domain name to the api and the A record to our hosted zone domain = api.add_domain_name('Domain', certificate=cert, domain_name=domain_name) aws_route53.ARecord( self, 'ShortUrlDomain', record_name=subdomain, zone=hosted_zone, target=aws_route53.RecordTarget.from_alias(aws_route53_targets.ApiGatewayDomain(domain))) return url 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://vp8tpwdmd4.execute-api.ap-northeast-2.amazonaws.com/prod/7c9b02b8", tps=0) |
리소스 삭제
모든 실습이 완료되었다면 다음과 같이 3개의 작업을 통해서 리소스를 삭제해야 합니다.
...