Documentation
¶
Overview ¶
Package terracost provides functionality to estimate the costs of infrastructure based on Terrafom plan files.
This package depends on the pricing data located in a MySQL database to work correctly. The following snippet will run all required database migrations and ingest pricing data from AmazonEC2 in eu-west-3 region:
db, err := sql.Open("mysql", "...")
backend := mysql.NewBackend(db)
// Run all database migrations
err = mysql.Migrate(ctx, db, "pricing_migrations")
// Ingest pricing data into the database
ingester := aws.NewIngester("AmazonEC2", "eu-west-3")
err = terracost.IngestPricing(ctx, backend, ingester)
With pricing data in the database, a Terraform plan can be read and estimated:
file, err := os.Open("path/to/tfplan.json")
plan, err := terracost.EstimateTerraformPlan(ctx, backend, file)
for _, res := range plan.ResourceDifferences() {
fmt.Printf("%s: %s -> %s\n", res.Address, res.PriorCost().String(), res.PlannedCost().String())
}
Index ¶
- func EstimateHCL(ctx context.Context, backend Backend, fs afero.Fs, path string, ...) (*cost.Plan, error)
- func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader, ...) (*cost.Plan, error)
- func IngestPricing(ctx context.Context, backend Backend, ingester Ingester) error
- type Backend
- type Ingester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EstimateHCL ¶ added in v0.3.0
func EstimateHCL(ctx context.Context, backend Backend, fs afero.Fs, path string, providerInitializers ...terraform.ProviderInitializer) (*cost.Plan, error)
EstimateHCL is a helper function that recursively reads Terraform modules from a directory at the given path and generates a planned cost.State that is returned wrapped in a cost.Plan. It uses the Backend to retrieve the pricing data.
func EstimateTerraformPlan ¶
func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader, providerInitializers ...terraform.ProviderInitializer) (*cost.Plan, error)
EstimateTerraformPlan is a helper function that reads a Terraform plan using the provided io.Reader, generates the prior and planned cost.State, and then creates a cost.Plan from them that is returned. It uses the Backend to retrieve the pricing data.
Types ¶
type Backend ¶
type Backend interface {
Product() product.Repository
Price() price.Repository
}
Backend represents a storage method used to store pricing data. It must include concrete implementations of all repositories.
type Ingester ¶
type Ingester interface {
// Ingest downloads pricing data from a cloud provider and sends prices with their associated products
// on the returned channel.
Ingest(ctx context.Context, chSize int) <-chan *price.WithProduct
// Err returns any potential error.
Err() error
}
Ingester represents a vendor-specific mechanism to load pricing data.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package aws enables cost estimation of AWS resources and ingestion of pricing data from AWS.
|
Package aws enables cost estimation of AWS resources and ingestion of pricing data from AWS. |
|
Package cost defines structures that represent cloud resources and states in a cloud-agnostic, as well as tool-agnostic way.
|
Package cost defines structures that represent cloud resources and states in a cloud-agnostic, as well as tool-agnostic way. |
|
Package mock is a generated GoMock package.
|
Package mock is a generated GoMock package. |
|
Package mysql implements the various domain entity repositories and includes a Backend that groups them.
|
Package mysql implements the various domain entity repositories and includes a Backend that groups them. |
|
Package query defines agnostic structures used to communicate between the tool layer (e.g.
|
Package query defines agnostic structures used to communicate between the tool layer (e.g. |
|
Package terraform includes functionality related to reading Terraform plan files.
|
Package terraform includes functionality related to reading Terraform plan files. |