mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
deploy: Create AWS session for CloudFront invalidation via Go CDK
This allows the AWS credentials to be picked up from the configured target URL (like blob does) rather than the current behaviour of only relying on the defaults. Relying on the defaults here means having to specify credentials twice (once in the URL for the blob, once in the environment for this code path) when non-default AWS credentials are in used (e.g. via a profile).
This commit is contained in:
parent
d7dcc76d27
commit
c3f273b2d7
2 changed files with 10 additions and 5 deletions
|
@ -18,6 +18,7 @@ package deploy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
@ -26,14 +27,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// InvalidateCloudFront invalidates the CloudFront cache for distributionID.
|
// InvalidateCloudFront invalidates the CloudFront cache for distributionID.
|
||||||
// It uses the default AWS credentials from the environment.
|
// Uses AWS credentials config from the bucket URL.
|
||||||
func InvalidateCloudFront(ctx context.Context, distributionID string) error {
|
func InvalidateCloudFront(ctx context.Context, target *Target) error {
|
||||||
sess, err := gcaws.NewDefaultSession()
|
u, err := url.Parse(target.URL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sess, _, err := gcaws.NewSessionFromURLParams(u.Query())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req := &cloudfront.CreateInvalidationInput{
|
req := &cloudfront.CreateInvalidationInput{
|
||||||
DistributionId: aws.String(distributionID),
|
DistributionId: aws.String(target.CloudFrontDistributionID),
|
||||||
InvalidationBatch: &cloudfront.InvalidationBatch{
|
InvalidationBatch: &cloudfront.InvalidationBatch{
|
||||||
CallerReference: aws.String(time.Now().Format("20060102150405")),
|
CallerReference: aws.String(time.Now().Format("20060102150405")),
|
||||||
Paths: &cloudfront.Paths{
|
Paths: &cloudfront.Paths{
|
||||||
|
|
|
@ -271,7 +271,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
d.logger.Println("Invalidating CloudFront CDN...")
|
d.logger.Println("Invalidating CloudFront CDN...")
|
||||||
if err := InvalidateCloudFront(ctx, d.target.CloudFrontDistributionID); err != nil {
|
if err := InvalidateCloudFront(ctx, d.target); err != nil {
|
||||||
d.logger.Printf("Failed to invalidate CloudFront CDN: %v\n", err)
|
d.logger.Printf("Failed to invalidate CloudFront CDN: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue