github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/deploy/cloudfront.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // +build !nodeploy
    15  
    16  package deploy
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"github.com/aws/aws-sdk-go/aws"
    23  	"github.com/aws/aws-sdk-go/aws/session"
    24  	"github.com/aws/aws-sdk-go/service/cloudfront"
    25  )
    26  
    27  // InvalidateCloudFront invalidates the CloudFront cache for distributionID.
    28  // It uses the default AWS credentials from the environment.
    29  func InvalidateCloudFront(ctx context.Context, distributionID string) error {
    30  	// SharedConfigEnable enables loading "shared config (~/.aws/config) and
    31  	// shared credentials (~/.aws/credentials) files".
    32  	// See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more
    33  	// details.
    34  	// This is the same codepath used by Go CDK when creating an s3 URL.
    35  	// TODO: Update this to a Go CDK helper once available
    36  	// (https://github.com/google/go-cloud/issues/2003).
    37  	sess, err := session.NewSessionWithOptions(session.Options{SharedConfigState: session.SharedConfigEnable})
    38  	if err != nil {
    39  		return err
    40  	}
    41  	req := &cloudfront.CreateInvalidationInput{
    42  		DistributionId: aws.String(distributionID),
    43  		InvalidationBatch: &cloudfront.InvalidationBatch{
    44  			CallerReference: aws.String(time.Now().Format("20060102150405")),
    45  			Paths: &cloudfront.Paths{
    46  				Items:    []*string{aws.String("/*")},
    47  				Quantity: aws.Int64(1),
    48  			},
    49  		},
    50  	}
    51  	_, err = cloudfront.New(sess).CreateInvalidationWithContext(ctx, req)
    52  	return err
    53  }