Skip to content

Commit

Permalink
Merge branch 'master' into fix-ecs-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Aug 13, 2021
2 parents b3d9133 + f8d8795 commit eb0bfa3
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ with 3 API mapping resources across different APIs and Stages.
| api | beta | `https://${domainName}/bar` |
| apiDemo | $default | `https://${domainName}/demo` |

You can retrieve the full domain URL with mapping key using the `domainUrl` property as so -

```ts
const demoDomainUrl = apiDemo.defaultStage.domainUrl; // returns "https://example.com/demo"
```

### Managing access

API Gateway supports multiple mechanisms for [controlling and managing access to your HTTP
Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/lib/common/api-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export class ApiMapping extends Resource implements IApiMapping {
*/
public readonly mappingKey?: string;

/**
* API domain name
*/
public readonly domainName: IDomainName;

constructor(scope: Construct, id: string, props: ApiMappingProps) {
super(scope, id);

Expand Down Expand Up @@ -121,5 +126,6 @@ export class ApiMapping extends Resource implements IApiMapping {

this.apiMappingId = resource.ref;
this.mappingKey = props.apiMappingKey;
this.domainName = props.domainName;
}
}
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-apigatewayv2/lib/common/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export abstract class StageBase extends Resource implements IStage {
public abstract readonly stageName: string;
protected abstract readonly baseApi: IApi;

/**
* The created ApiMapping if domain mapping has been added
* @internal
*/
protected _apiMapping?: ApiMapping

/**
* The URL to this stage.
*/
Expand All @@ -45,7 +51,10 @@ export abstract class StageBase extends Resource implements IStage {
* @internal
*/
protected _addDomainMapping(domainMapping: DomainMappingOptions) {
new ApiMapping(this, `${domainMapping.domainName}${domainMapping.mappingKey}`, {
if (this._apiMapping) {
throw new Error('Only one ApiMapping allowed per Stage');
}
this._apiMapping = new ApiMapping(this, `${domainMapping.domainName}${domainMapping.mappingKey}`, {
api: this.baseApi,
domainName: domainMapping.domainName,
stage: this,
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/lib/http/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,15 @@ export class HttpStage extends HttpStageBase {
const urlPath = this.stageName === DEFAULT_STAGE_NAME ? '' : this.stageName;
return `https://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}`;
}

/**
* The custom domain URL to this stage
*/
public get domainUrl(): string {
if (!this._apiMapping) {
throw new Error('domainUrl is not available when no API mapping is associated with the Stage');
}

return `https://${this._apiMapping.domainName.name}/${this._apiMapping.mappingKey ?? ''}`;
}
}
47 changes: 45 additions & 2 deletions packages/@aws-cdk/aws-apigatewayv2/test/http/stage.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Template } from '@aws-cdk/assertions';
import { Certificate } from '@aws-cdk/aws-certificatemanager';
import { Metric } from '@aws-cdk/aws-cloudwatch';
import { Stack } from '@aws-cdk/core';
import { HttpApi, HttpStage } from '../../lib';

import { DomainName, HttpApi, HttpStage } from '../../lib';

describe('HttpStage', () => {
test('default', () => {
Expand Down Expand Up @@ -116,4 +116,47 @@ describe('HttpStage', () => {
const metricNames = metrics.map(m => m.metricName);
expect(metricNames).toEqual(['4xx', '5xx', 'DataProcessed', 'Latency', 'IntegrationLatency', 'Count']);
});
});

describe('HttpStage with domain mapping', () => {
const domainName = 'example.com';
const certArn = 'arn:aws:acm:us-east-1:111111111111:certificate';

test('domainUrl returns the correct path', () => {
const stack = new Stack();
const api = new HttpApi(stack, 'Api', {
createDefaultStage: false,
});

const dn = new DomainName(stack, 'DN', {
domainName,
certificate: Certificate.fromCertificateArn(stack, 'cert', certArn),
});

const stage = new HttpStage(stack, 'DefaultStage', {
httpApi: api,
domainMapping: {
domainName: dn,
},
});

expect(stage.domainUrl).toBe(`https://${domainName}/`);
});

test('domainUrl throws error if domainMapping is not configured', () => {
const stack = new Stack();
const api = new HttpApi(stack, 'Api', {
createDefaultStage: false,
});

const stage = new HttpStage(stack, 'DefaultStage', {
httpApi: api,
});

const t = () => {
stage.domainUrl;
};

expect(t).toThrow(/domainUrl is not available when no API mapping is associated with the Stage/);
});
});
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function bundle(options: BundlingOptions): lambda.Code {

const image = cdk.DockerImage.fromBuild(stagedir, {
buildArgs: {
IMAGE: runtime.bundlingDockerImage.image,
IMAGE: runtime.bundlingImage.image,
},
file: dockerfile,
});
Expand Down
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-s3-deployment/lib/bucket-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,15 @@ export class BucketDeployment extends CoreConstruct {
constructor(scope: Construct, id: string, props: BucketDeploymentProps) {
super(scope, id);

if (props.distributionPaths && !props.distribution) {
throw new Error('Distribution must be specified if distribution paths are specified');
if (props.distributionPaths) {
if (!props.distribution) {
throw new Error('Distribution must be specified if distribution paths are specified');
}
if (!cdk.Token.isUnresolved(props.distributionPaths)) {
if (!props.distributionPaths.every(distributionPath => cdk.Token.isUnresolved(distributionPath) || distributionPath.startsWith('/'))) {
throw new Error('Distribution paths must start with "/"');
}
}
}

const handler = new lambda.SingletonFunction(this, 'CustomResourceHandler', {
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/bucket-deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,30 @@ test('fails if distribution paths provided but not distribution ID', () => {

});

test('fails if distribution paths don\'t start with "/"', () => {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Dest');
const distribution = new cloudfront.CloudFrontWebDistribution(stack, 'Distribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: bucket,
},
behaviors: [{ isDefaultBehavior: true }],
},
],
});

// THEN
expect(() => new s3deploy.BucketDeployment(stack, 'Deploy', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website.zip'))],
destinationBucket: bucket,
distribution,
distributionPaths: ['images/*'],
})).toThrow(/Distribution paths must start with "\/"/);
});

testFutureBehavior('lambda execution role gets permissions to read from the source bucket and read/write in destination', s3GrantWriteCtx, cdk.App, (app) => {
// GIVEN
const stack = new cdk.Stack(app);
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export class Stack extends CoreConstruct implements ITaggable {
const numberOfResources = Object.keys(resources).length;

if (numberOfResources > this.maxResources) {
throw new Error(`Number of resources: ${numberOfResources} is greater than allowed maximum of ${this.maxResources}`);
throw new Error(`Number of resources in stack '${this.node.path}': ${numberOfResources} is greater than allowed maximum of ${this.maxResources}`);
} else if (numberOfResources >= (this.maxResources * 0.8)) {
Annotations.of(this).addInfo(`Number of resources: ${numberOfResources} is approaching allowed maximum of ${this.maxResources}`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('stack', () => {

expect(() => {
app.synth();
}).toThrow('Number of resources: 1000 is greater than allowed maximum of 500');
}).toThrow('Number of resources in stack \'MyStack\': 1000 is greater than allowed maximum of 500');


});
Expand All @@ -81,7 +81,7 @@ describe('stack', () => {

expect(() => {
app.synth();
}).toThrow('Number of resources: 200 is greater than allowed maximum of 100');
}).toThrow('Number of resources in stack \'MyStack\': 200 is greater than allowed maximum of 100');


});
Expand Down

0 comments on commit eb0bfa3

Please sign in to comment.