Skip to content
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

feat(cloudfront): support gRPC for distribution #32535

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Dec 16, 2024

Issue # (if applicable)

Closes #32534.

Reason for this change

CloudFormation supports GrpcConfig property to enable gRPC in CacheBehavior and DefaultCacheBehavior.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-grpcconfig.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-cachebehavior.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-defaultcachebehavior.html

So it would be good to enable gRPC for CloudFront Distribution using L2.

Description of changes

Add enableGrpc property in BehaviorOptions.

Description of how you validated changes

Both unit and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team December 16, 2024 08:47
@github-actions github-actions bot added distinguished-contributor [Pilot] contributed 50+ PRs to the CDK feature-request A feature should be added or improved. p2 labels Dec 16, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@go-to-k go-to-k marked this pull request as ready for review December 17, 2024 06:04
@github-actions github-actions bot added the effort/small Small work item – less than a day of effort label Dec 17, 2024
Copy link

codecov bot commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.85%. Comparing base (e8d8237) to head (83893e0).
Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32535      +/-   ##
==========================================
+ Coverage   78.80%   78.85%   +0.04%     
==========================================
  Files         108      108              
  Lines        7159     7165       +6     
  Branches     1319     1319              
==========================================
+ Hits         5642     5650       +8     
+ Misses       1332     1330       -2     
  Partials      185      185              
Flag Coverage Δ
suite.unit 78.85% <ø> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 78.85% <ø> (+0.04%) ⬆️

@go-to-k go-to-k marked this pull request as draft December 17, 2024 07:04
@aws-cdk-automation aws-cdk-automation dismissed their stale review December 17, 2024 07:21

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@go-to-k go-to-k marked this pull request as ready for review December 17, 2024 07:33
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 17, 2024
@go-to-k go-to-k marked this pull request as draft December 17, 2024 08:53
@go-to-k go-to-k marked this pull request as ready for review December 17, 2024 08:54
@go-to-k go-to-k marked this pull request as draft December 17, 2024 09:21
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 17, 2024
Comment on lines +802 to +804
if (![HttpVersion.HTTP2, HttpVersion.HTTP2_AND_3].includes(this.httpVersion)) {
throw new Error(`'httpVersion' must be HttpVersion.HTTP2 or HttpVersion.HTTP2_AND_3 if 'enableGrpc' in 'defaultBehavior' or 'additionalBehaviors' is true, got ${this.httpVersion}`);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise CFn will fail:

Resource handler returned message: "Invalid request provided: AWS::CloudFront::Distribution: The parameter gRPC only supports HTTP/2. (Service: CloudFront, Status Code: 400, Request ID:...

Comment on lines 729 to +734
private renderCacheBehaviors(): CfnDistribution.CacheBehaviorProperty[] | undefined {
if (this.additionalBehaviors.length === 0) { return undefined; }
return this.additionalBehaviors.map(behavior => behavior._renderBehavior());
return this.additionalBehaviors.map(behavior => {
this.validateGrpc(behavior);
return behavior._renderBehavior();
});
Copy link
Contributor Author

@go-to-k go-to-k Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If even one behavior has gRpc with invalid settings such as httpVersion: cloudfront.HttpVersion.HTTP3, an error will occur, so check each behavior.

The example is the case for gRPC with a httpVersion not including HTTP2:

new cloudfront.Distribution(stack, 'TestDistribution', {
  httpVersion: cloudfront.HttpVersion.HTTP3,
  defaultBehavior: {
    origin,
    allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
    // enableGrpc: true,
  },
  additionalBehaviors: {
    '/second': {
      origin,
      allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
      // enableGrpc: true,
    },
    '/third': {
      origin,
      allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
      enableGrpc: true,
    },
  },
});

Comment on lines +36 to +38
if (props.edgeLambdas !== undefined && props.edgeLambdas.length > 0) {
throw new Error('\'edgeLambdas\' cannot be specified if \'enableGrpc\' is true');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise CFn will fail

Resource handler returned message: "Invalid request provided: AWS::CloudFront::Distribution: The parameter gRPC doesn't support Lambda@Edge. (Service: CloudFront, Status Code: 400, Request ID:...

Comment on lines +33 to +35
if (props.allowedMethods !== AllowedMethods.ALLOW_ALL) {
throw new Error('\'allowedMethods\' can only be AllowedMethods.ALLOW_ALL if \'enableGrpc\' is true');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise CFn will fail:

Resource handler returned message: "Invalid request provided: AWS::CloudFront::Distribution: The parameter gRPC only supports the POST method. (Service: CloudFront, Status Code: 400, Request ID: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't write like the following:

throw new Error(`'allowedMethods' can only be AllowedMethods.ALLOW_ALL if 'enableGrpc' is true, got: ${props.allowedMethods?.methods.join(', ')}`);

Because the AllowedMethods.methods is an array type and the message will not be intuitive.

export class AllowedMethods {
  /** HEAD and GET */
  public static readonly ALLOW_GET_HEAD = new AllowedMethods(['GET', 'HEAD']);
  /** HEAD, GET, and OPTIONS */
  public static readonly ALLOW_GET_HEAD_OPTIONS = new AllowedMethods(['GET', 'HEAD', 'OPTIONS']);
  /** All supported HTTP methods */
  public static readonly ALLOW_ALL = new AllowedMethods(['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE']);

  /** HTTP methods supported */
  public readonly methods: string[];

  private constructor(methods: string[]) { this.methods = methods; }
}

Otherwise, if the user specifies AllowedMethods.ALLOW_GET_HEAD_OPTIONS, the following message will be displayed:

'allowedMethods' can only be AllowedMethods.ALLOW_ALL if 'enableGrpc' is true, got: 'GET', 'HEAD', 'OPTIONS'

@go-to-k go-to-k marked this pull request as ready for review December 18, 2024 08:03
@go-to-k go-to-k marked this pull request as draft December 18, 2024 08:04
this.grpcEnabled = props.enableGrpc;

if (this.grpcEnabled) {
if (props.allowedMethods !== AllowedMethods.ALLOW_ALL) {
Copy link
Contributor Author

@go-to-k go-to-k Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined for allowedMethods is also be bad because the default value is AllowedMethods.ALLOW_GET_HEAD.

@go-to-k go-to-k marked this pull request as ready for review December 18, 2024 10:58
@@ -22,10 +22,21 @@ export interface CacheBehaviorProps extends AddBehaviorOptions {
* CloudFrontWebDistribution implementation.
*/
export class CacheBehavior {
public readonly grpcEnabled?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposed to validate settings with gRPC in Distribution construct.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 9d2391b
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(cloudfront): support gRPC config for distribution
2 participants