-
Notifications
You must be signed in to change notification settings - Fork 1k
New serverless pattern - private-apigw-public-custom-domain added #2828
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
Changes from 6 commits
feaffea
be5c41d
be4bb6c
b3efa46
f353ecb
1d00c0c
cedf842
dca1807
67bbf92
e5b8860
408a807
efde3c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,155 @@ | ||||||
| # Amazon Private API Gateway with VPC Endpoints and Public Domain | ||||||
|
|
||||||
| This pattern creates an Amazon Private API Gateway that is only accessible through VPC endpoints, with public custom domain name resolution for internal only access through an Amazon internal Application Load Balancer. | ||||||
|
|
||||||
| This architecture is intended for use cases which require private APIs, which are only accessible from on-premises via VPN or Direct Connect, while the DNS can be resolved publicly. | ||||||
|
|
||||||
| Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/private-apigw-custom-domain](https://serverlessland.com/patterns/private-apigw-custom-domain) | ||||||
|
|
||||||
| Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||||||
|
|
||||||
| ## Project Structure | ||||||
|
|
||||||
| ``` | ||||||
| ├── app.py # CDK app entry point | ||||||
| ├── cdk.json # CDK configuration | ||||||
| ├── requirements.txt # Python dependencies | ||||||
| ├── private_api_gateway/ | ||||||
| │ ├── __init__.py | ||||||
| │ └── private_api_gateway_stack.py # Main stack definition | ||||||
| └── README.md # This file | ||||||
| ``` | ||||||
| ## Architecture | ||||||
|
|
||||||
| - **VPC**: 10.0.0.0/16 with DNS support | ||||||
| - **Subnets**: 2 public + 2 private subnets across 2 AZs | ||||||
| - **NAT Gateway**: Managed by CDK in public subnets | ||||||
| - **Private API Gateway**: PetStore sample API with VPC endpoint restriction | ||||||
| - **Application Load Balancer**: Internal ALB for SSL termination | ||||||
| - **Lambda Automation**: Custom resource for VPC endpoint target registration | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
| ## Requirements | ||||||
| Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||||||
|
|
||||||
| * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||||||
| * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||||||
| * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||||||
| * [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/v2/guide/getting-started.html) (AWS CDK) installed | ||||||
| * [Amazon Route 53](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/dns-configuring.html) configured as DNS service and public hosted zone created | ||||||
| * [Public Certificate](https://docs.aws.amazon.com/acm/latest/userguide/acm-public-certificates.html) requested in Amazon Certificate Manager (ACM) | ||||||
| * [Python 3.8+](https://www.python.org/downloads/) installed | ||||||
|
|
||||||
|
|
||||||
| ## Deployment Instructions | ||||||
|
|
||||||
| ### 1. Install Dependencies | ||||||
| ```bash | ||||||
| # Create virtual environment | ||||||
| python3 -m venv .venv | ||||||
|
|
||||||
| # Activate virtual environment | ||||||
| source .venv/bin/activate # or your venv activation command | ||||||
|
|
||||||
| # Install CDK dependencies | ||||||
| pip install -r requirements.txt | ||||||
| ``` | ||||||
|
|
||||||
| ### 2. Get Parameters | ||||||
|
|
||||||
| You must provide both context parameters: | ||||||
|
|
||||||
| 1. **domain_name**: Your custom domain name (e.g., api.example.com) | ||||||
| 2. **certificate_arn**: ARN of your ACM certificate that covers the domain | ||||||
|
|
||||||
| ### 3. CDK Deployment | ||||||
|
|
||||||
| ```bash | ||||||
| # Deploy with both required parameters | ||||||
| cdk deploy \ | ||||||
| -c domain_name=api.example.com \ | ||||||
| -c certificate_arn=arn:aws:acm:region:account:certificate/cert-id | ||||||
|
||||||
| -c certificate_arn=arn:aws:acm:region:account:certificate/cert-id | |
| -c certificate_arn=arn:aws:acm:region:account:certificate/<certifcate-id> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be consistent with the example below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted.
bfreiberg marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that redundant given the first two points?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
Sliiiin marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edited and added deletion of manually created resources and cdk deployment.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. This should be changed in the pattern-model as well: https://github.com/aws-samples/serverless-patterns/blob/main/_pattern-model/README.md
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #!/usr/bin/env python3 | ||
| import aws_cdk as cdk | ||
| from private_api_gateway.private_api_gateway_stack import PrivateApiGatewayStack | ||
|
|
||
| app = cdk.App() | ||
|
|
||
| # Get required context parameters | ||
| domain_name = app.node.try_get_context("domain_name") | ||
| certificate_arn = app.node.try_get_context("certificate_arn") | ||
|
|
||
| if not domain_name: | ||
| raise ValueError("domain_name context parameter is required. Use: cdk deploy -c domain_name=api.example.com") | ||
|
|
||
| if not certificate_arn: | ||
| raise ValueError("certificate_arn context parameter is required. Use: cdk deploy -c certificate_arn=arn:aws:acm:...") | ||
|
|
||
| PrivateApiGatewayStack( | ||
| app, | ||
| "PrivateApiGatewayStack", | ||
| domain_name=domain_name, | ||
| certificate_arn=certificate_arn, | ||
| env=cdk.Environment( | ||
| account=app.account, | ||
| region=app.region | ||
| ) | ||
| ) | ||
|
|
||
| app.synth() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| { | ||
| "app": "python app.py", | ||
| "watch": { | ||
| "include": [ | ||
| "**" | ||
| ], | ||
| "exclude": [ | ||
| "README.md", | ||
| "cdk*.json", | ||
| "requirements*.txt", | ||
| "source.bat", | ||
| "**/__pycache__", | ||
| "**/*.pyc" | ||
| ] | ||
| }, | ||
| "context": { | ||
| "@aws-cdk/aws-lambda:recognizeLayerVersion": true, | ||
| "@aws-cdk/core:checkSecretUsage": true, | ||
| "@aws-cdk/core:target-partitions": [ | ||
| "aws", | ||
| "aws-cn" | ||
| ], | ||
| "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, | ||
| "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, | ||
| "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, | ||
| "@aws-cdk/aws-iam:minimizePolicies": true, | ||
| "@aws-cdk/core:validateSnapshotRemovalPolicy": true, | ||
| "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, | ||
| "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, | ||
| "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, | ||
| "@aws-cdk/aws-apigateway:disableCloudWatchRole": false, | ||
| "@aws-cdk/core:enablePartitionLiterals": true, | ||
| "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, | ||
| "@aws-cdk/aws-iam:standardizedServicePrincipals": true, | ||
| "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, | ||
| "@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, | ||
| "@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, | ||
| "@aws-cdk/aws-route53-patters:useCertificate": true, | ||
| "@aws-cdk/customresources:installLatestAwsSdkDefault": false, | ||
| "@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true, | ||
| "@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true, | ||
| "@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true, | ||
| "@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true, | ||
| "@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true, | ||
| "@aws-cdk/aws-redshift:columnId": true, | ||
| "@aws-cdk/aws-stepfunctions-tasks:enableLoggingConfiguration": true, | ||
| "@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true, | ||
| "@aws-cdk/aws-apigateway:requestValidatorUniqueId": true, | ||
| "@aws-cdk/aws-kms:aliasNameRef": true, | ||
| "@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true, | ||
| "@aws-cdk/core:includePrefixInUniqueNameGeneration": true, | ||
| "@aws-cdk/aws-efs:denyAnonymousAccess": true, | ||
| "@aws-cdk/aws-opensearchservice:enableLogging": true, | ||
| "@aws-cdk/aws-lambda:useLatestRuntimeVersion": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.8 reached EOL end of last year. Let's use a more current version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to 3.12+