diff --git a/packages/@aws-cdk/aws-apigateway/README.md b/packages/@aws-cdk/aws-apigateway/README.md index 10c5c811f87ae..6c6d316288790 100644 --- a/packages/@aws-cdk/aws-apigateway/README.md +++ b/packages/@aws-cdk/aws-apigateway/README.md @@ -950,8 +950,8 @@ domain.addBasePathMapping(api1, { basePath: 'go-to-api1' }); domain.addBasePathMapping(api2, { basePath: 'boom' }); ``` -You can specify the API `Stage` to which this base path URL will map to. By default, this will be the -`deploymentStage` of the `RestApi`. +By default, the base path URL will map to the `deploymentStage` of the `RestApi`. +You can specify a different API `Stage` to which the base path URL will map to. ```ts declare const domain: apigateway.DomainName; @@ -966,6 +966,19 @@ const betaStage = new apigateway.Stage(this, 'beta-stage', { domain.addBasePathMapping(restapi, { basePath: 'api/beta', stage: betaStage }); ``` +It is possible to create a base path mapping without associating it with a +stage by using the `attachToStage` property. When set to `false`, the stage must be +included in the URL when invoking the API. For example, + will invoke the stage named `prod` from the +`myapi` base path mapping. + +```ts +declare const domain: apigateway.DomainName; +declare const api: apigateway.RestApi; + +domain.addBasePathMapping(api, { basePath: 'myapi', attachToStage: false }); +``` + If you don't specify `basePath`, all URLs under this domain will be mapped to the API, and you won't be able to map another API to the same domain: @@ -978,6 +991,23 @@ domain.addBasePathMapping(api); This can also be achieved through the `mapping` configuration when defining the domain as demonstrated above. +Base path mappings can also be created with the `BasePathMapping` resource. + +```ts +declare const api: apigateway.RestApi; + +const domainName = apigateway.DomainName.fromDomainNameAttributes(this, 'DomainName', { + domainName: 'domainName', + domainNameAliasHostedZoneId: 'domainNameAliasHostedZoneId', + domainNameAliasTarget: 'domainNameAliasTarget', +}); + +new apigateway.BasePathMapping(this, 'BasePathMapping', { + domainName: domainName, + restApi: api, +}); +``` + If you wish to setup this domain with an Amazon Route53 alias, use the `targets.ApiGatewayDomain`: ```ts diff --git a/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts b/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts index d87fe8536f099..066ec229b832f 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts @@ -22,6 +22,14 @@ export interface BasePathMappingOptions { * @default - map to deploymentStage of restApi otherwise stage needs to pass in URL */ readonly stage?: Stage; + + /** + * Whether to attach the base path mapping to a stage. + * Use this property to create a base path mapping without attaching it to the Rest API default stage. + * This property is ignored if `stage` is provided. + * @default - true + */ + readonly attachToStage?: boolean; } export interface BasePathMappingProps extends BasePathMappingOptions { @@ -53,9 +61,12 @@ export class BasePathMapping extends Resource { } } + const attachToStage = props.attachToStage ?? true; + // if restApi is an owned API and it has a deployment stage, map all requests // to that stage. otherwise, the stage will have to be specified in the URL. - const stage = props.stage ?? (props.restApi instanceof RestApiBase + // if props.attachToStage is false, then do not attach to the stage. + const stage = props.stage ?? (props.restApi instanceof RestApiBase && attachToStage ? props.restApi.deploymentStage : undefined); @@ -63,7 +74,7 @@ export class BasePathMapping extends Resource { basePath: props.basePath, domainName: props.domainName.domainName, restApiId: props.restApi.restApiId, - stage: stage && stage.stageName, + stage: stage?.stageName, }); } } diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.assets.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.assets.json new file mode 100644 index 0000000000000..bd656b5bfc38c --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.assets.json @@ -0,0 +1,19 @@ +{ + "version": "21.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "basepathmappingDefaultTestDeployAssertDA82B6F0.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.template.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/basepathmappingDefaultTestDeployAssertDA82B6F0.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/cdk.out b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/cdk.out new file mode 100644 index 0000000000000..8ecc185e9dbee --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"21.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/integ.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/integ.json new file mode 100644 index 0000000000000..c270139cd5cfe --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "21.0.0", + "testCases": { + "base-path-mapping/DefaultTest": { + "stacks": [ + "test-stack" + ], + "assertionStack": "base-path-mapping/DefaultTest/DeployAssert", + "assertionStackName": "basepathmappingDefaultTestDeployAssertDA82B6F0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/manifest.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/manifest.json new file mode 100644 index 0000000000000..a57c7ce4a9fcd --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/manifest.json @@ -0,0 +1,147 @@ +{ + "version": "21.0.0", + "artifacts": { + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + }, + "test-stack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "test-stack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "test-stack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "test-stack.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/4328166cfc76604ab46a2a088da69e6631472872129e804d27c8b5336c842774.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "test-stack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "test-stack.assets" + ], + "metadata": { + "/test-stack/Api/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ApiF70053CD" + } + ], + "/test-stack/Api/Deployment/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ApiDeploymentB17BE62Df672ad8455f9678e4a3db5854bdb8d73" + } + ], + "/test-stack/Api/DeploymentStage.prod/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ApiDeploymentStageprod3EB9684E" + } + ], + "/test-stack/Api/Endpoint": [ + { + "type": "aws:cdk:logicalId", + "data": "ApiEndpoint4F160690" + } + ], + "/test-stack/Api/Default/GET/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ApiGET9257B917" + } + ], + "/test-stack/MappingOne/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MappingOneAB5D4FD4" + } + ], + "/test-stack/MappingTwo/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MappingTwo551C79ED" + } + ], + "/test-stack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/test-stack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "test-stack" + }, + "basepathmappingDefaultTestDeployAssertDA82B6F0.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "basepathmappingDefaultTestDeployAssertDA82B6F0.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "basepathmappingDefaultTestDeployAssertDA82B6F0": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "basepathmappingDefaultTestDeployAssertDA82B6F0.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "basepathmappingDefaultTestDeployAssertDA82B6F0.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "basepathmappingDefaultTestDeployAssertDA82B6F0.assets" + ], + "metadata": { + "/base-path-mapping/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/base-path-mapping/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "base-path-mapping/DefaultTest/DeployAssert" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.assets.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.assets.json new file mode 100644 index 0000000000000..a2ac187057b56 --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "21.0.0", + "files": { + "4328166cfc76604ab46a2a088da69e6631472872129e804d27c8b5336c842774": { + "source": { + "path": "test-stack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "4328166cfc76604ab46a2a088da69e6631472872129e804d27c8b5336c842774.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.template.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.template.json new file mode 100644 index 0000000000000..52d687e8b3f2d --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/test-stack.template.json @@ -0,0 +1,137 @@ +{ + "Resources": { + "ApiF70053CD": { + "Type": "AWS::ApiGateway::RestApi", + "Properties": { + "Name": "Api" + } + }, + "ApiDeploymentB17BE62Df672ad8455f9678e4a3db5854bdb8d73": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ApiF70053CD" + }, + "Description": "Automatically created by the RestApi construct" + }, + "DependsOn": [ + "ApiGET9257B917" + ] + }, + "ApiDeploymentStageprod3EB9684E": { + "Type": "AWS::ApiGateway::Stage", + "Properties": { + "RestApiId": { + "Ref": "ApiF70053CD" + }, + "DeploymentId": { + "Ref": "ApiDeploymentB17BE62Df672ad8455f9678e4a3db5854bdb8d73" + }, + "StageName": "prod" + } + }, + "ApiGET9257B917": { + "Type": "AWS::ApiGateway::Method", + "Properties": { + "HttpMethod": "GET", + "ResourceId": { + "Fn::GetAtt": [ + "ApiF70053CD", + "RootResourceId" + ] + }, + "RestApiId": { + "Ref": "ApiF70053CD" + }, + "AuthorizationType": "NONE", + "Integration": { + "Type": "MOCK" + } + } + }, + "MappingOneAB5D4FD4": { + "Type": "AWS::ApiGateway::BasePathMapping", + "Properties": { + "DomainName": "domainName", + "RestApiId": { + "Ref": "ApiF70053CD" + }, + "Stage": { + "Ref": "ApiDeploymentStageprod3EB9684E" + } + } + }, + "MappingTwo551C79ED": { + "Type": "AWS::ApiGateway::BasePathMapping", + "Properties": { + "DomainName": "domainName", + "BasePath": "path", + "RestApiId": { + "Ref": "ApiF70053CD" + } + } + } + }, + "Outputs": { + "ApiEndpoint4F160690": { + "Value": { + "Fn::Join": [ + "", + [ + "https://", + { + "Ref": "ApiF70053CD" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".", + { + "Ref": "AWS::URLSuffix" + }, + "/", + { + "Ref": "ApiDeploymentStageprod3EB9684E" + }, + "/" + ] + ] + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/tree.json b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/tree.json new file mode 100644 index 0000000000000..2a9baa7dc204d --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.integ.snapshot/tree.json @@ -0,0 +1,269 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.92" + } + }, + "test-stack": { + "id": "test-stack", + "path": "test-stack", + "children": { + "Api": { + "id": "Api", + "path": "test-stack/Api", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/Api/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi", + "aws:cdk:cloudformation:props": { + "name": "Api" + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnRestApi", + "version": "0.0.0" + } + }, + "Deployment": { + "id": "Deployment", + "path": "test-stack/Api/Deployment", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/Api/Deployment/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Deployment", + "aws:cdk:cloudformation:props": { + "restApiId": { + "Ref": "ApiF70053CD" + }, + "description": "Automatically created by the RestApi construct" + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnDeployment", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.Deployment", + "version": "0.0.0" + } + }, + "DeploymentStage.prod": { + "id": "DeploymentStage.prod", + "path": "test-stack/Api/DeploymentStage.prod", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/Api/DeploymentStage.prod/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage", + "aws:cdk:cloudformation:props": { + "restApiId": { + "Ref": "ApiF70053CD" + }, + "deploymentId": { + "Ref": "ApiDeploymentB17BE62Df672ad8455f9678e4a3db5854bdb8d73" + }, + "stageName": "prod" + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnStage", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.Stage", + "version": "0.0.0" + } + }, + "Endpoint": { + "id": "Endpoint", + "path": "test-stack/Api/Endpoint", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnOutput", + "version": "0.0.0" + } + }, + "Default": { + "id": "Default", + "path": "test-stack/Api/Default", + "children": { + "GET": { + "id": "GET", + "path": "test-stack/Api/Default/GET", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/Api/Default/GET/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Method", + "aws:cdk:cloudformation:props": { + "httpMethod": "GET", + "resourceId": { + "Fn::GetAtt": [ + "ApiF70053CD", + "RootResourceId" + ] + }, + "restApiId": { + "Ref": "ApiF70053CD" + }, + "authorizationType": "NONE", + "integration": { + "type": "MOCK" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnMethod", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.Method", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.ResourceBase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.RestApi", + "version": "0.0.0" + } + }, + "Domain": { + "id": "Domain", + "path": "test-stack/Domain", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, + "MappingOne": { + "id": "MappingOne", + "path": "test-stack/MappingOne", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/MappingOne/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::BasePathMapping", + "aws:cdk:cloudformation:props": { + "domainName": "domainName", + "restApiId": { + "Ref": "ApiF70053CD" + }, + "stage": { + "Ref": "ApiDeploymentStageprod3EB9684E" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnBasePathMapping", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.BasePathMapping", + "version": "0.0.0" + } + }, + "MappingTwo": { + "id": "MappingTwo", + "path": "test-stack/MappingTwo", + "children": { + "Resource": { + "id": "Resource", + "path": "test-stack/MappingTwo/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::BasePathMapping", + "aws:cdk:cloudformation:props": { + "domainName": "domainName", + "basePath": "path", + "restApiId": { + "Ref": "ApiF70053CD" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.CfnBasePathMapping", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apigateway.BasePathMapping", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + }, + "base-path-mapping": { + "id": "base-path-mapping", + "path": "base-path-mapping", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "base-path-mapping/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "base-path-mapping/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.92" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "base-path-mapping/DefaultTest/DeployAssert", + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTest", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts index a886b3c957dad..c3da93e83ba26 100644 --- a/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts +++ b/packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts @@ -1,4 +1,4 @@ -import { Template } from '@aws-cdk/assertions'; +import { Match, Template } from '@aws-cdk/assertions'; import * as acm from '@aws-cdk/aws-certificatemanager'; import * as cdk from '@aws-cdk/core'; import * as apigw from '../lib'; @@ -25,6 +25,7 @@ describe('BasePathMapping', () => { Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', { DomainName: { Ref: 'MyDomainE4943FBC' }, RestApiId: { Ref: 'MyApi49610EDF' }, + Stage: { Ref: 'MyApiDeploymentStageprodE1054AF0' }, }); }); @@ -97,6 +98,7 @@ describe('BasePathMapping', () => { restApi: api, domainName: domain, stage, + attachToStage: true, }); // THEN @@ -104,4 +106,28 @@ describe('BasePathMapping', () => { Stage: { Ref: 'MyStage572B0482' }, }); }); + + test('specify attachToStage property', () => { + // GIVEN + const stack = new cdk.Stack(); + const api = new apigw.RestApi(stack, 'MyApi'); + api.root.addMethod('GET'); // api must have atleast one method. + const domain = new apigw.DomainName(stack, 'MyDomain', { + domainName: 'example.com', + certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'), + endpointType: apigw.EndpointType.REGIONAL, + }); + + // WHEN + new apigw.BasePathMapping(stack, 'MyBasePath', { + restApi: api, + domainName: domain, + attachToStage: false, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', { + Stage: Match.absent(), + }); + }); }); diff --git a/packages/@aws-cdk/aws-apigateway/test/integ.base-path-mapping.ts b/packages/@aws-cdk/aws-apigateway/test/integ.base-path-mapping.ts new file mode 100644 index 0000000000000..9d3a31a3a205e --- /dev/null +++ b/packages/@aws-cdk/aws-apigateway/test/integ.base-path-mapping.ts @@ -0,0 +1,39 @@ +import * as cdk from '@aws-cdk/core'; +import { IntegTest } from '@aws-cdk/integ-tests'; +import * as apigateway from '../lib'; + +export class TestStack extends cdk.Stack { + constructor(scope: cdk.App, id: string) { + super(scope, id); + + const restApi = new apigateway.RestApi(this, 'Api'); + + restApi.root.addMethod('GET'); + + const domainName = apigateway.DomainName.fromDomainNameAttributes(this, 'Domain', { + domainName: 'domainName', + domainNameAliasHostedZoneId: 'domainNameAliasHostedZoneId', + domainNameAliasTarget: 'domainNameAliasTarget', + }); + + new apigateway.BasePathMapping(this, 'MappingOne', { + domainName, + restApi, + }); + + new apigateway.BasePathMapping(this, 'MappingTwo', { + domainName, + restApi, + basePath: 'path', + attachToStage: false, + }); + } +} + +const app = new cdk.App(); + +const testStack = new TestStack(app, 'test-stack'); + +new IntegTest(app, 'base-path-mapping', { + testCases: [testStack], +});