1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-08-08 22:36:41 +02:00

support cloudfront for static files

This commit is contained in:
Lee Brown
2019-07-15 00:37:07 -08:00
parent cf77e9d9eb
commit 0ce9b0f50a
3 changed files with 56 additions and 37 deletions

View File

@ -11,6 +11,7 @@
"acm:ListCertificates", "acm:ListCertificates",
"acm:RequestCertificate", "acm:RequestCertificate",
"acm:DescribeCertificate", "acm:DescribeCertificate",
"cloudfront:CreateDistribution",
"ec2:DescribeSubnets", "ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups", "ec2:DescribeSecurityGroups",
"ec2:CreateSecurityGroup", "ec2:CreateSecurityGroup",

View File

@ -17,13 +17,13 @@ import (
"strings" "strings"
"time" "time"
"github.com/aws/aws-sdk-go/service/cloudfront"
"geeks-accelerator/oss/saas-starter-kit/internal/platform/tests" "geeks-accelerator/oss/saas-starter-kit/internal/platform/tests"
"geeks-accelerator/oss/saas-starter-kit/internal/schema" "geeks-accelerator/oss/saas-starter-kit/internal/schema"
"geeks-accelerator/oss/saas-starter-kit/tools/devops/internal/retry" "geeks-accelerator/oss/saas-starter-kit/tools/devops/internal/retry"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/acm" "github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr" "github.com/aws/aws-sdk-go/service/ecr"
@ -282,26 +282,42 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
}, },
}) })
// The S3 key prefix used as the origin when cloud front is enabled.
if req.S3BucketPublicKeyPrefix == "" {
req.S3BucketPublicKeyPrefix = "/public"
}
if flags.S3BucketPublicCloudfront { if flags.S3BucketPublicCloudfront {
allowedMethods := &cloudfront.AllowedMethods{
Items: aws.StringSlice([]string{"HEAD", "GET"}),
}
allowedMethods.Quantity = aws.Int64(int64(len(allowedMethods.Items)))
allowedMethods:= &cloudfront.AllowedMethods{} cacheMethods := &cloudfront.CachedMethods{
allowedMethods.SetItems(aws.StringSlice([]string{ "HEAD", "GET"})) Items: aws.StringSlice([]string{"HEAD", "GET"}),
}
cacheMethods := &cloudfront.CachedMethods{} cacheMethods.Quantity = aws.Int64(int64(len(cacheMethods.Items)))
cacheMethods.SetItems(aws.StringSlice([]string{ "HEAD", "GET"}))
allowedMethods.SetCachedMethods(cacheMethods) allowedMethods.SetCachedMethods(cacheMethods)
domainId := "S3"+req.S3BucketPublicName domainId := "S3-" + req.S3BucketPublicName
domainName := fmt.Sprintf("%s.s3.%s.amazonaws.com", req.S3BucketPublicName, req.AwsCreds.Region) domainName := fmt.Sprintf("%s.s3.%s.amazonaws.com", req.S3BucketPublicName, req.AwsCreds.Region)
origins := &cloudfront.Origins{} origins := &cloudfront.Origins{
origins.SetItems([]*cloudfront.Origin{ Items: []*cloudfront.Origin{
&cloudfront.Origin{ &cloudfront.Origin{
Id: aws.String(domainId), Id: aws.String(domainId),
DomainName: aws.String(domainName), DomainName: aws.String(domainName),
OriginPath: aws.String(req.S3BucketPublicKeyPrefix), OriginPath: aws.String(req.S3BucketPublicKeyPrefix),
S3OriginConfig: &cloudfront.S3OriginConfig{
OriginAccessIdentity: aws.String(""),
}, },
}) CustomHeaders: &cloudfront.CustomHeaders{
Quantity: aws.Int64(0),
},
},
},
}
origins.Quantity = aws.Int64(int64(len(origins.Items)))
req.CloudfrontPublic = &cloudfront.DistributionConfig{ req.CloudfrontPublic = &cloudfront.DistributionConfig{
Comment: aws.String(""), Comment: aws.String(""),
@ -317,8 +333,16 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
MaxTTL: aws.Int64(31536000), MaxTTL: aws.Int64(31536000),
ForwardedValues: &cloudfront.ForwardedValues{ ForwardedValues: &cloudfront.ForwardedValues{
QueryString: aws.Bool(true), QueryString: aws.Bool(true),
Cookies: &cloudfront.CookiePreference{
Forward: aws.String("none"),
}, },
}, },
TrustedSigners: &cloudfront.TrustedSigners{
Enabled: aws.Bool(false),
Quantity: aws.Int64(0),
},
ViewerProtocolPolicy: aws.String("allow-all"),
},
Origins: origins, Origins: origins,
ViewerCertificate: &cloudfront.ViewerCertificate{ ViewerCertificate: &cloudfront.ViewerCertificate{
CertificateSource: aws.String("cloudfront"), CertificateSource: aws.String("cloudfront"),
@ -328,7 +352,6 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
PriceClass: aws.String("PriceClass_All"), PriceClass: aws.String("PriceClass_All"),
CallerReference: aws.String("devops-deploy"), CallerReference: aws.String("devops-deploy"),
} }
req.CloudfrontPublic = nil
} }
} }
@ -404,11 +427,6 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
}) })
} }
// The S3 key prefix used as the origin when cloud front is enabled.
if req.S3BucketPublicKeyPrefix == "" {
req.S3BucketPublicKeyPrefix = "public"
}
// The S3 prefix used to upload static files served to public. // The S3 prefix used to upload static files served to public.
if req.StaticFilesS3Prefix == "" { if req.StaticFilesS3Prefix == "" {
req.StaticFilesS3Prefix = filepath.Join(req.S3BucketPublicKeyPrefix, releaseTag(req.Env, req.ServiceName), "static") req.StaticFilesS3Prefix = filepath.Join(req.S3BucketPublicKeyPrefix, releaseTag(req.Env, req.ServiceName), "static")
@ -2406,7 +2424,7 @@ func ServiceDeploy(log *log.Logger, req *serviceDeployRequest) error {
"{HOST_NAMES}": strings.Join(req.ServiceHostNames, ","), "{HOST_NAMES}": strings.Join(req.ServiceHostNames, ","),
"{STATIC_FILES_S3_ENABLED}": "false", "{STATIC_FILES_S3_ENABLED}": "false",
"{STATIC_FILES_S3_PREFIX}": "", "{STATIC_FILES_S3_PREFIX}": req.StaticFilesS3Prefix,
"{STATIC_FILES_CLOUDFRONT_ENABLED}": "false", "{STATIC_FILES_CLOUDFRONT_ENABLED}": "false",
"{STATIC_FILES_IMG_RESIZE_ENABLED}": "false", "{STATIC_FILES_IMG_RESIZE_ENABLED}": "false",