cloudfront.go (1981B)
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 //go:build !nodeploy 15 // +build !nodeploy 16 17 package deploy 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/aws/aws-sdk-go/aws" 24 "github.com/aws/aws-sdk-go/aws/session" 25 "github.com/aws/aws-sdk-go/service/cloudfront" 26 ) 27 28 // InvalidateCloudFront invalidates the CloudFront cache for distributionID. 29 // It uses the default AWS credentials from the environment. 30 func InvalidateCloudFront(ctx context.Context, distributionID string) error { 31 // SharedConfigEnable enables loading "shared config (~/.aws/config) and 32 // shared credentials (~/.aws/credentials) files". 33 // See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more 34 // details. 35 // This is the same codepath used by Go CDK when creating an s3 URL. 36 // TODO: Update this to a Go CDK helper once available 37 // (https://github.com/google/go-cloud/issues/2003). 38 sess, err := session.NewSessionWithOptions(session.Options{SharedConfigState: session.SharedConfigEnable}) 39 if err != nil { 40 return err 41 } 42 req := &cloudfront.CreateInvalidationInput{ 43 DistributionId: aws.String(distributionID), 44 InvalidationBatch: &cloudfront.InvalidationBatch{ 45 CallerReference: aws.String(time.Now().Format("20060102150405")), 46 Paths: &cloudfront.Paths{ 47 Items: []*string{aws.String("/*")}, 48 Quantity: aws.Int64(1), 49 }, 50 }, 51 } 52 _, err = cloudfront.New(sess).CreateInvalidationWithContext(ctx, req) 53 return err 54 }