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

Added API Gateway v2 HTTP & Websocket support #207

Merged
merged 8 commits into from
Nov 9, 2020

Conversation

robh007
Copy link
Contributor

@robh007 robh007 commented Nov 8, 2020

Objective:

Adds support for api_gatewayv2_api

Example output:

 NAME                                      MONTHLY QTY  UNIT      PRICE  HOURLY COST  MONTHLY COST  

  aws_apigatewayv2_api.http                                                                          
  ├─ Requests (first 300m)                      1000000  requests  1e-06       0.0014        1.0000  
  └─ Requests (over 300m)                             0  requests  9e-07       0.0000        0.0000  
  Total                                                                        0.0014        1.0000  
                                                                                                     
  aws_apigatewayv2_api.websocket                                                                     
  ├─ Requests (first 1B message transfers)   1000000000  requests  1e-06       1.3699     1000.0000  
  └─ Requests (over 1B message transfers)             0  requests  8e-07       0.0000        0.0000  
  Total                                                                        1.3699     1000.0000  
                                                                                                     
  OVERALL TOTAL                                                                1.3712     1001.0000  


Pricing details:

The aws_apigatewayv2_api provides 4 pricing components described in https://aws.amazon.com/api-gateway/pricing/. This adds support for the API gateway HTTP API & WebSocket API.

Copy link
Member

@alikhajeh1 alikhajeh1 left a comment

Choose a reason for hiding this comment

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

return &schema.RegistryItem{
Name: "aws_apigatewayv2_api",
Notes: []string{
"WebSocket Connection minutes is not yet supported",
Copy link
Member

Choose a reason for hiding this comment

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

I think we can cost components for this:

$ cd infracost/infracost/scripts/price_explorer

$ ./2_find_product_families.sh AmazonApiGateway
List of Product Families for AmazonApiGateway:
Executing query... done
[
  "API Calls",
  "Amazon API Gateway Cache",
  "WebSocket"
]

$ ./3_find_attribute_keys.sh AmazonApiGateway WebSocket
Executing query... done

#####################################

Found 2 different products
------------------

....
     "key": "usagetype",
-    "value": "USE1-ApiGatewayMessage",
+    "value": "USE1-ApiGatewayMinute",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alikhajeh1 do you want me to add the cost component in anyway? The reason I left it out was because of the calculation variation on websocket connections. It can be varied per second / minute / hour & the number of connections rate can be per second / minute / hour / day.

Copy link
Member

@alikhajeh1 alikhajeh1 Nov 9, 2020

Choose a reason for hiding this comment

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

@robh007 I think it's helpful to see that, even if there is no infracost provider support for it, e.g.:

  └─ Connection duration          -  1M minutes     0.025       -          -

If we add the provider support, would having average_connection_duration (in minutes) and monthly_connections help? so the user inputs those and we multiply them to calculate the total connection minutes.

internal/providers/terraform/aws/apigatewayv2_api.go Outdated Show resolved Hide resolved
internal/providers/terraform/aws/apigatewayv2_api.go Outdated Show resolved Hide resolved
func websocketApiCostComponent(d *schema.ResourceData, u *schema.ResourceData) []*schema.CostComponent {
region := d.Get("region").String()
monthlyMessages := decimal.Zero
messageSize := decimal.NewFromInt(0)
Copy link
Member

Choose a reason for hiding this comment

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

Messages are metered in 32 KB increments.

Should this default to 32KB as that seems to be the minimum?


return []*schema.CostComponent{
{
Name: "Requests (first 1B message transfers)",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Name: "Requests (first 1B message transfers)",
Name: "Messages (first 1B)",

The WebSocket APIs section of https://aws.amazon.com/api-gateway/pricing/ talks about messages, and that's how websocket load tests usually work too (based on number of messages and message size), so I think we can override the Number of Requests title they use in their pricing table. The usagetype also uses the word "message".

return []*schema.CostComponent{
{
Name: "Requests (first 1B message transfers)",
Unit: "requests",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Unit: "requests",
Unit: "messages",

Please also add UnitMultiplier: 1000000,

Service: strPtr("AmazonApiGateway"),
ProductFamily: strPtr("WebSocket"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "description", Value: strPtr("WebSocket Messages")},
Copy link
Member

Choose a reason for hiding this comment

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

this isn't needed

},
},
{
Name: "Requests (over 1B message transfers)",
Copy link
Member

Choose a reason for hiding this comment

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

Same three comments from above apply here too.

@robh007
Copy link
Contributor Author

robh007 commented Nov 9, 2020

@alikhajeh1 I've updated with the requested changes. I can't assign back to you, I've just managed to un-assign myself from this....

@robh007
Copy link
Contributor Author

robh007 commented Nov 9, 2020

New output

 NAME                            MONTHLY QTY  UNIT         PRICE   HOURLY COST  MONTHLY COST  

  aws_apigatewayv2_api.http                                                                    
  └─ Requests (first 300m)                  1  1M requests  1.0000       0.0014        1.0000  
  Total                                                                  0.0014        1.0000  
                                                                                               
  aws_apigatewayv2_api.websocket                                                               
  └─ Messages (first 1B)                1,000  1M messages  1.0000       1.3699    1,000.0000  
  Total                                                                  1.3699    1,000.0000  
                                                                                               
  OVERALL TOTAL (USD)                                                    1.3712    1,001.0000 

Copy link
Member

@alikhajeh1 alikhajeh1 left a comment

Choose a reason for hiding this comment

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

Thanks @robh007! I added my suggestions, thanks for your patience as I improve the CONTRIBUTING.md file to clarify the tweaks/wording.

btw, maybe run make fmt and make lint to see if that fixes the failed CI build.

Service: strPtr("AmazonApiGateway"),
ProductFamily: strPtr("API Calls"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "description", Value: strPtr("HTTP API Requests")},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{Key: "description", Value: strPtr("HTTP API Requests")},

description is needed either (descriptions might change if AWS reword things? I think usagetype is the one that users rely on in the billing CSV too)

Service: strPtr("AmazonApiGateway"),
ProductFamily: strPtr("API Calls"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "description", Value: strPtr("HTTP API Requests")},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{Key: "description", Value: strPtr("HTTP API Requests")},


CostComponent := []*schema.CostComponent{
{
Name: "Requests (first 300m)",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Name: "Requests (first 300m)",
Name: "Requests (first 300M)",

I'll add this to the CONTRIBUTING.md file

- tiers in names: use the K postfix for thousand, M for million, B for billion and T for trillion, e.g. "Requests (first 300M)" and "Messages (first 1B)". Note the capital case letters.


if apiTierTwo.GreaterThan(decimal.NewFromInt(0)) {
CostComponent = append(CostComponent, &schema.CostComponent{
Name: "Requests (over 300m)",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Name: "Requests (over 300m)",
Name: "Requests (over 300M)",

Name: "aws_apigatewayv2_api.http",
CostComponentChecks: []testutil.CostComponentCheck{
{
Name: "Requests (first 300m)",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Name: "Requests (first 300m)",
Name: "Requests (first 300M)",

@alikhajeh1
Copy link
Member

@robh007 I resolved the conflict with master, I think this PR just needs the Connection duration cost component and can be merged.

@alikhajeh1 alikhajeh1 merged commit 3dcaf28 into infracost:master Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants