Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom domain support for apigw #1679

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deploy/stacks/backend_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
vpc_endpoints_sg=None,
internet_facing=True,
custom_domain=None,
apigw_custom_domain=None,
ip_ranges=None,
apig_vpce=None,
prod_sizing=False,
Expand Down Expand Up @@ -201,6 +202,7 @@ def __init__(
email_custom_domain=ses_stack.ses_identity.email_identity_name if ses_stack is not None else None,
ses_configuration_set=ses_stack.configuration_set.configuration_set_name if ses_stack is not None else None,
custom_domain=custom_domain,
apigw_custom_domain=apigw_custom_domain,
custom_auth=custom_auth,
custom_waf_rules=custom_waf_rules,
allowed_origins=allowed_origins,
Expand Down
2 changes: 2 additions & 0 deletions deploy/stacks/backend_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
vpc_endpoints_sg=None,
internet_facing=True,
custom_domain=None,
apigw_custom_domain=None,
ip_ranges=None,
apig_vpce=None,
prod_sizing=False,
Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(
vpc_endpoints_sg=vpc_endpoints_sg,
internet_facing=internet_facing,
custom_domain=custom_domain,
apigw_custom_domain=apigw_custom_domain,
ip_ranges=ip_ranges,
apig_vpce=apig_vpce,
prod_sizing=prod_sizing,
Expand Down
37 changes: 35 additions & 2 deletions deploy/stacks/lambda_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
aws_kms as kms,
aws_sqs as sqs,
aws_logs as logs,
aws_route53 as r53,
aws_route53_targets as r53_targets,
Duration,
CfnOutput,
Fn,
RemovalPolicy,
BundlingOptions,
)
from aws_cdk.aws_apigateway import DomainNameOptions, EndpointType, SecurityPolicy
from aws_cdk.aws_certificatemanager import Certificate
from aws_cdk.aws_ec2 import (
InterfaceVpcEndpoint,
InterfaceVpcEndpointAwsService,
Expand Down Expand Up @@ -56,13 +60,14 @@ def __init__(
email_custom_domain=None,
ses_configuration_set=None,
custom_domain=None,
apigw_custom_domain=None,
custom_auth=None,
allowed_origins='*',
log_retention_duration=None,
**kwargs,
):
super().__init__(scope, id, **kwargs)

self.apigw_custom_domain = apigw_custom_domain
self.log_retention_duration = log_retention_duration
log_level = 'INFO' if prod_sizing else 'DEBUG'

Expand Down Expand Up @@ -662,15 +667,43 @@ def set_up_graphql_api_gateway(
types=[apigw.EndpointType.PRIVATE], vpc_endpoints=[api_vpc_endpoint]
),
policy=api_policy,
disable_execute_api_endpoint=bool(self.apigw_custom_domain),
)
else:
gw = apigw.RestApi(
self,
backend_api_name,
rest_api_name=backend_api_name,
deploy_options=api_deploy_options,
disable_execute_api_endpoint=bool(self.apigw_custom_domain),
)

if self.apigw_custom_domain:
certificate = Certificate.from_certificate_arn(
self, 'CustomDomainCertificate', self.apigw_custom_domain['certificate_arn']
)
gw.add_domain_name(
'ApiGwCustomDomainName',
certificate=certificate,
domain_name=self.apigw_custom_domain['hosted_zone_name'],
endpoint_type=EndpointType.EDGE if internet_facing else EndpointType.PRIVATE,
security_policy=SecurityPolicy.TLS_1_2,
)
r53.ARecord(
self,
'ApiGwARecordId',
zone=r53.HostedZone.from_hosted_zone_attributes(
self,
'ApiGwHostedZoneId',
hosted_zone_id=self.apigw_custom_domain['hosted_zone_id'],
zone_name=self.apigw_custom_domain['hosted_zone_name'],
),
target=r53.RecordTarget.from_alias(r53_targets.ApiGateway(gw)),
)
api_url = gw.url
api_url = f'https://{gw.domain_name.domain_name}/'
else:
api_url = gw.url

integration = apigw.LambdaIntegration(api_handler)
request_validator = apigw.RequestValidator(
self,
Expand Down
1 change: 1 addition & 0 deletions deploy/stacks/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def set_backend_stage(self, target_env, repository_name):
vpc_restricted_nacls=target_env.get('vpc_restricted_nacl', False),
internet_facing=target_env.get('internet_facing', True),
custom_domain=target_env.get('custom_domain'),
apigw_custom_domain=target_env.get('apigw_custom_domain'),
ip_ranges=target_env.get('ip_ranges'),
apig_vpce=target_env.get('apig_vpce'),
prod_sizing=target_env.get('prod_sizing', True),
Expand Down
Loading