Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created December 7, 2015 10:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hhatto/9cd20cdcb98431855a6b to your computer and use it in GitHub Desktop.
Save hhatto/9cd20cdcb98431855a6b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/cloudfront"
)
const (
CRED_PROFILE = "CREDENTIAL_PROFILE"
CF_DIST_ID = "YOUR_DISTRIBUTION_ID"
)
func viewListInvalidations(svc *cloudfront.CloudFront) error {
resp, err := svc.ListInvalidations(&cloudfront.ListInvalidationsInput{
DistributionId: aws.String(CF_DIST_ID),
})
if err != nil {
return fmt.Errorf("list.inval. err:%v", err.Error())
}
fmt.Printf("invs:%v\n", resp)
return nil
}
func createInvalidationRequest(svc *cloudfront.CloudFront, pattern string) error {
now := time.Now()
resp, err := svc.CreateInvalidation(&cloudfront.CreateInvalidationInput{
DistributionId: aws.String(CF_DIST_ID),
InvalidationBatch: &cloudfront.InvalidationBatch{
CallerReference: aws.String(
fmt.Sprintf("goinvali%s", now.Format("2006/01/02,15:04:05"))),
Paths: &cloudfront.Paths{
Quantity: aws.Int64(1),
Items: []*string{
aws.String(pattern),
},
},
},
})
if err != nil {
return fmt.Errorf("create.inval. err:%v", err.Error())
}
fmt.Printf("invs:%v\n", resp)
return nil
}
func main() {
creds := credentials.NewSharedCredentials("", CRED_PROFILE)
svc := cloudfront.New(&aws.Config{
Credentials: creds,
})
if err := viewListInvalidations(svc); err != nil {
fmt.Println(err)
return
}
pattern := "/*.txt"
if err := createInvalidationRequest(svc, pattern); err != nil {
fmt.Println(err)
return
}
if err := viewListInvalidations(svc); err != nil {
fmt.Println(err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment