|
| 1 | +Invoking a Private Custom Domain Name cross-account |
| 2 | + |
| 3 | +## Architecture Overview |
| 4 | + |
| 5 | +This architecture enables the invocation of a Private Custom Domain Name deployed for a Private API Gateway cross-account. The solution leverages [Amazon Private API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-apis.html), [Execute-API VPC Endpoint](https://docs.aws.amazon.com/vpc/latest/privatelink/create-interface-endpoint.html), and the Private Custom Domain N [Private Custom Domain name](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-custom-domains.html). |
| 6 | + |
| 7 | +Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-private-custom-domain-name-cross-account). |
| 8 | + |
| 9 | +You can update the template to add AWS resources through the same deployment process that updates your application code. |
| 10 | + |
| 11 | +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 more details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 12 | + |
| 13 | +### Requirements |
| 14 | + |
| 15 | +- Two [AWS accounts](https://signin.aws.amazon.com/signup?request_type=register). IAM users or roles with sufficient permissions to make the necessary AWS service calls and manage AWS resources. |
| 16 | +- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed and configured. |
| 17 | +- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) (AWS SAM) installed. |
| 18 | +- Setup .aws/credentials [named profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) namely **accountA** and **accountB** so you can run CLI and AWS SAM commands against them. |
| 19 | +- An [Amazon Execute-API VPC Endpoint](https://docs.aws.amazon.com/vpc/latest/privatelink/create-interface-endpoint.html) needed to invoke your private custom domain name. |
| 20 | +- A [Public ACM Certificate issued](https://docs.aws.amazon.com/acm/latest/userguide/gs-acm-request-public.html) so that API Gateway can prove its identity to clients establishing secure HTTPS connections |
| 21 | + |
| 22 | +### How it works |
| 23 | + |
| 24 | +This pattern utilizes two AWS Sccounts and their respective templates. |
| 25 | + |
| 26 | +1. **Account A** : Hosts the Private API Gateway and the Private Custom Domain Name: |
| 27 | + - **Amazon API Gateway (Private)**: Receives requests from the Account B via the Execute VPC Endpoint deployed in Account B |
| 28 | + - **Custom Domain Name** which is hit by the client in Account B to invoke the API Gateway |
| 29 | + - The **Custom Domain Name** is then shared with Account B via AWS Resource Access Manager (AWS RAM) |
| 30 | + |
| 31 | +2. **Account B** : Hosts the Private Hosted Zone, the Execute API VPC Endpoint and creates the Domain Name Access association. A client in the VPC where the VPC Endpoint is deployed can then send requests to the Private API Gateway in Account A using the Custom Domain Name |
| 32 | + |
| 33 | + |
| 34 | +### Deployment Instructions |
| 35 | + |
| 36 | +**Note**: Please make sure to follow the below steps in order to make sure the deployment is successful. |
| 37 | + |
| 38 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 39 | + ``` bash |
| 40 | + git clone https://github.com/aws-samples/serverless-patterns |
| 41 | + ``` |
| 42 | +2. Change directory to the pattern directory: |
| 43 | + ```bash |
| 44 | + cd serverless-patterns/apigw-private-custom-domain-name-cross-account |
| 45 | + ``` |
| 46 | + |
| 47 | +#### AccountA |
| 48 | + |
| 49 | +1. In account A, where you would like to create **Private API Gateway**, along with its Private Custom Domain Name, navigate to the `accountA` directory from the main directory and deploy using *(if you are in a different directory, then run `cd ..` before entering the below command)*: |
| 50 | + ```bash |
| 51 | + cd accountA |
| 52 | + |
| 53 | + sam deploy --guided --profile accountA |
| 54 | +
|
| 55 | +2. During the prompts: |
| 56 | + - Enter **stack name** and desired **AWS Region**. |
| 57 | + - Enter **the AccountB VPC Endpoint**. |
| 58 | + - Enter **the AWS ACM Certificate ARN**. |
| 59 | + - Enter the **Custom Domain Name** which is covered by the aforementioned certificate. |
| 60 | + - Enter the **AccountB ID**. This will be used to share the Custom Domain Name resource with. |
| 61 | + - Allow SAM CLI to create IAM roles with the required permissions. |
| 62 | +3. Note the outputs from the SAM deployment process. This contains both the `Custom Domain Name` and `Custom Domain Name ARN`, which will be used as inputs for the second account's stack deployment. |
| 63 | +
|
| 64 | +#### Accept the Resource Share Invitation |
| 65 | +Upon deploying the first template, you can get the invitation ARN from the AWS CLI by running the following command: |
| 66 | + ``` |
| 67 | + aws ram get-resource-share-invitations --profile <target-account-profile> |
| 68 | + ``` |
| 69 | +
|
| 70 | +Copy the invitation ARN, and paste it in the following command: |
| 71 | + ``` |
| 72 | + aws ram accept-resource-share-invitation \ |
| 73 | + --resource-share-invitation-arn arn:aws:ram:region:account-id:resource-share-invitation/invitation-id \ |
| 74 | + --profile <target-account-profile> |
| 75 | + ``` |
| 76 | +
|
| 77 | +#### AccountB |
| 78 | +1. In account B, where you would like to create the **Custom Domain Name Access Association** and the **Private Hosted Zone**, navigate to the `accountB` directory from the main directory and deploy using *(if you are in a different directory, then run `cd ..` before entering the below command)*: |
| 79 | + ```bash |
| 80 | + cd accountB |
| 81 | + |
| 82 | + sam deploy --guided --profile accountB |
| 83 | + ``` |
| 84 | +2. During the prompts: |
| 85 | + - Enter **stack name** and desired **AWS Region**. |
| 86 | + - Enter the **VPC ID** where the Hosted Zone will be created |
| 87 | + - Enter the **VPC Endpoint DNS Name**. It will be used to create the Alias record in the Private Hosted Zone |
| 88 | + - Enter the **Custom Domain Name** created in the first template |
| 89 | + - Enter the **Custom Domain Name ARN** created in the first template |
| 90 | + - Enter the **VPC Endpoint Hosted Zone ID** |
| 91 | + - Allow SAM CLI to create IAM roles with the required permissions. |
| 92 | +
|
| 93 | +
|
| 94 | +## Cleanup |
| 95 | +To avoid incurring future charges, it's important to delete the resources in the correcct order. Follow these steps to clean up the resources created by the four templates *(Make sure to navigate to the directory containing the template before running the below commands)*: |
| 96 | +
|
| 97 | +1. Delete Account A template |
| 98 | + ```bash |
| 99 | + sam delete --stack-name STACK_NAME --profile PROFILE_NAME |
| 100 | + ``` |
| 101 | +2. Delete Account B template |
| 102 | + ```bash |
| 103 | + sam delete --stack-name STACK_NAME_ACCOUNT_B --profile accountB |
| 104 | + ``` |
0 commit comments