本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
下列範例 AWS CloudFormation 範本會建立私有 API 和私有自訂網域名稱、將私有 API 對應至自訂網域名稱,然後建立網域名稱存取關聯。您需要提供自己的 VPC 端點、網域名稱和憑證 ARN。
下列考量可能會影響您使用 AWS CloudFormation 來建立私有自訂網域名稱:
-
您無法使用 拒絕網域名稱存取關聯 AWS CloudFormation。若要拒絕網域名稱存取關聯,請使用 AWS CLI。
-
使用
AWS::ApiGateway::DomainNameV2
AWS CloudFormation 屬性來建立私有自訂網域名稱。 -
使用
AWS::ApiGateway:BasePathMappingV2
AWS CloudFormation 屬性建立基本路徑映射。
AWSTemplateFormatVersion: 2010-09-09 Parameters: EndpointID: Type: String Default:
vpce-abcd1234567efg
Description: A VPC endpoint with enableDnsHostnames and enableDnsSupport set to true. DomainName: Type: String Default:private.example.com
Description: A domain name that you own. CertificateArn: Type: String Default:arn:aws:acm:us-west-2:123456789:certificate/abcd-000-1234-0000-000000abcd
Description: An ACM certificate that covers the domain name. Resources: PrivateApi: Type: 'AWS::ApiGateway::RestApi' Properties: EndpointConfiguration: Types: - PRIVATE VpcEndpointIds: - !Ref EndpointID Name: private-api Policy: Statement: - Action: 'execute-api:Invoke' Effect: Allow Principal: '*' Resource: 'execute-api:/*' - Action: 'execute-api:Invoke' Condition: StringNotEquals: 'aws:SourceVpce': !Ref EndpointID Effect: Deny Principal: '*' Resource: 'execute-api:/*' Version: 2012-10-17 PrivateApiDeployment: Type: 'AWS::ApiGateway::Deployment' Properties: RestApiId: !Ref PrivateApi Description: Private API deployment DependsOn: - PrivateApiMethod PrivateApiStage: Type: 'AWS::ApiGateway::Stage' Properties: RestApiId: !Ref PrivateApi DeploymentId: !Ref PrivateApiDeployment StageName: prod PrivateApiMethod: Type: 'AWS::ApiGateway::Method' Properties: HttpMethod: ANY ResourceId: !GetAtt PrivateApi.RootResourceId RestApiId: !Ref PrivateApi AuthorizationType: NONE Integration: Type: MOCK RequestTemplates: application/json: "{\"statusCode\": 200}" IntegrationResponses: - StatusCode: '200' MethodResponses: - StatusCode: '200' PrivateDomainName: Type: AWS::ApiGateway::DomainNameV2 Properties: DomainName: !Ref DomainName CertificateArn: !Ref CertificateArn EndpointConfiguration: Types: - PRIVATE SecurityPolicy: TLS_1_2 Policy: Statement: - Action: 'execute-api:Invoke' Effect: Allow Principal: '*' Resource: 'execute-api:/*' - Action: 'execute-api:Invoke' Condition: StringNotEquals: 'aws:SourceVpce': !Ref EndpointID Effect: Deny Principal: '*' Resource: 'execute-api:/*' Version: 2012-10-17 PrivateBasePathMapping: Type: AWS::ApiGateway::BasePathMappingV2 DependsOn: - PrivateApiStage Properties: BasePath: prod DomainNameArn: !GetAtt PrivateDomainName.DomainNameArn RestApiId: !Ref PrivateApi Stage: prod DomainNameAccessAssociation: Type: AWS::ApiGateway::DomainNameAccessAssociation Properties: DomainNameArn: !GetAtt PrivateDomainName.DomainNameArn AccessAssociationSource: !Ref EndpointID AccessAssociationSourceType: VPCE