|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// snippet-start:[cloudfront.go-v2.CreateDistribution] |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "log" |
| 14 | + |
| 15 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 16 | + "github.com/aws/aws-sdk-go-v2/config" |
| 17 | + "github.com/aws/aws-sdk-go-v2/service/cloudfront" |
| 18 | + cloudfrontTypes "github.com/aws/aws-sdk-go-v2/service/cloudfront/types" |
| 19 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 20 | +) |
| 21 | + |
| 22 | +// CFDistributionAPI defines the interface for the CreateDistribution function. |
| 23 | +// We use this interface to test the function using a mocked service. |
| 24 | +type CFDistributionAPI interface { |
| 25 | + CreateDistribution(bucketName, certificateSSLArn, domain string) (*cloudfront.CreateDistributionOutput, error) |
| 26 | + createoriginAccessIdentity(domainName string) (string, error) |
| 27 | +} |
| 28 | + |
| 29 | +type CFDistributionAPIImpl struct { |
| 30 | + s3Client *s3.Client |
| 31 | + cloudfrontClient *cloudfront.Client |
| 32 | +} |
| 33 | + |
| 34 | +func createCFDistribution(s3client *s3.Client, cloudfront *cloudfront.Client) CFDistributionAPI { |
| 35 | + return &CFDistributionAPIImpl{ |
| 36 | + s3Client: s3client, |
| 37 | + cloudfrontClient: cloudfront, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (c *CFDistributionAPIImpl) CreateDistribution(bucketName, certificateSSLArn, domain string) (*cloudfront.CreateDistributionOutput, error) { |
| 42 | + locationOutput, err := c.s3Client.GetBucketLocation(context.Background(), &s3.GetBucketLocationInput{Bucket: aws.String(bucketName)}) |
| 43 | + |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + originDomain := bucketName + ".s3." + string(locationOutput.LocationConstraint) + ".amazonaws.com" |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + originAccessIdentityID, err := c.createoriginAccessIdentity(domain) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + cloudfrontResponse, err := c.cloudfrontClient.CreateDistribution(context.TODO(), &cloudfront.CreateDistributionInput{ |
| 59 | + DistributionConfig: &cloudfrontTypes.DistributionConfig{ |
| 60 | + Enabled: aws.Bool(true), |
| 61 | + CallerReference: &originDomain, |
| 62 | + Comment: &originDomain, |
| 63 | + IsIPV6Enabled: aws.Bool(false), |
| 64 | + PriceClass: cloudfrontTypes.PriceClassPriceClass100, |
| 65 | + HttpVersion: cloudfrontTypes.HttpVersionHttp11, |
| 66 | + DefaultRootObject: aws.String("index.html"), |
| 67 | + Aliases: &cloudfrontTypes.Aliases{ |
| 68 | + Quantity: aws.Int32(1), |
| 69 | + Items: []string{domain}, |
| 70 | + }, |
| 71 | + ViewerCertificate: &cloudfrontTypes.ViewerCertificate{ |
| 72 | + ACMCertificateArn: aws.String(certificateSSLArn), |
| 73 | + SSLSupportMethod: cloudfrontTypes.SSLSupportMethodSniOnly, |
| 74 | + }, |
| 75 | + CustomErrorResponses: &cloudfrontTypes.CustomErrorResponses{ |
| 76 | + Quantity: aws.Int32(1), |
| 77 | + Items: []cloudfrontTypes.CustomErrorResponse{ |
| 78 | + { |
| 79 | + ErrorCode: aws.Int32(403), |
| 80 | + ResponseCode: aws.String("200"), |
| 81 | + ErrorCachingMinTTL: aws.Int64(10), |
| 82 | + ResponsePagePath: aws.String("/index.html"), |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + Origins: &cloudfrontTypes.Origins{ |
| 87 | + Quantity: aws.Int32(1), |
| 88 | + Items: []cloudfrontTypes.Origin{ |
| 89 | + { |
| 90 | + DomainName: aws.String(originDomain), |
| 91 | + Id: aws.String(originDomain), |
| 92 | + S3OriginConfig: &cloudfrontTypes.S3OriginConfig{ |
| 93 | + OriginAccessIdentity: aws.String("origin-access-identity/cloudfront/" + originAccessIdentityID), |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + CacheBehaviors: nil, |
| 99 | + DefaultCacheBehavior: &cloudfrontTypes.DefaultCacheBehavior{ |
| 100 | + TargetOriginId: aws.String(originDomain), |
| 101 | + Compress: aws.Bool(true), |
| 102 | + ViewerProtocolPolicy: cloudfrontTypes.ViewerProtocolPolicyRedirectToHttps, |
| 103 | + AllowedMethods: &cloudfrontTypes.AllowedMethods{ |
| 104 | + Quantity: aws.Int32(2), |
| 105 | + Items: []cloudfrontTypes.Method{ |
| 106 | + cloudfrontTypes.MethodGet, |
| 107 | + cloudfrontTypes.MethodHead, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }) |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return cloudfrontResponse, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (c *CFDistributionAPIImpl) createoriginAccessIdentity(domainName string) (string, error) { |
| 122 | + ctx := context.Background() |
| 123 | + oai, err := c.cloudfrontClient.CreateCloudFrontOriginAccessIdentity(ctx, &cloudfront.CreateCloudFrontOriginAccessIdentityInput{ |
| 124 | + CloudFrontOriginAccessIdentityConfig: &cloudfrontTypes.CloudFrontOriginAccessIdentityConfig{ |
| 125 | + CallerReference: aws.String(domainName), |
| 126 | + Comment: aws.String(domainName), |
| 127 | + }, |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + return "", err |
| 131 | + } |
| 132 | + return *oai.CloudFrontOriginAccessIdentity.Id, nil |
| 133 | +} |
| 134 | + |
| 135 | +var ( |
| 136 | + // bucketName is the name of the S3 bucket to create a CloudFront distribution for. |
| 137 | + bucketName = "" |
| 138 | + // certificateSSLArn is the ARN value of the certificate issued by the AWS Certificate Manager (ACM). |
| 139 | + // When testing, please check and copy and paste the ARN of the pre-issued certificate. |
| 140 | + // If you don't know how to create a TLS/SSL certificate using ACM, follow the link below. |
| 141 | + // https://docs.aws.amazon.com/acm/latest/userguide/acm-overview.html |
| 142 | + certificateSSLArn = "" |
| 143 | + // domain refers to the domain that will be used in conjunction with CloudFront and Amazon Route 53. |
| 144 | + // For testing, please enter a domain that is registered in Route 53 and will be used in conjunction with CloudFront. |
| 145 | + domain = "" |
| 146 | +) |
| 147 | + |
| 148 | +// main uses the AWS SDK for Go V2 to create an Amazon CloudFront distribution. |
| 149 | +// This example uses the default settings specified in your shared credentials |
| 150 | +// and config files. |
| 151 | +func main() { |
| 152 | + |
| 153 | + flag.StringVar(&bucketName, "bucket", "", "<EXAMPLE-BUCKET-NAME>") |
| 154 | + flag.StringVar(&certificateSSLArn, "cert", "", "<AWS CERTIFICATE MANGER ARN>") |
| 155 | + flag.StringVar(&domain, "domain", "", "<YOUR DOMAIN>") |
| 156 | + flag.Parse() |
| 157 | + if bucketName == "" { |
| 158 | + log.Println(errors.New("please setup bucket name")) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + if certificateSSLArn == "" { |
| 163 | + log.Println(errors.New("please setup certificate ARN")) |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + if domain == "" { |
| 168 | + log.Println(errors.New("please setup your domain")) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + sdkConfig, err := config.LoadDefaultConfig(context.TODO()) |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") |
| 176 | + fmt.Println(err) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + s3Client := s3.NewFromConfig(sdkConfig) |
| 181 | + cloudfrontClient := cloudfront.NewFromConfig(sdkConfig) |
| 182 | + |
| 183 | + cfDistribution := createCFDistribution(s3Client, cloudfrontClient) |
| 184 | + |
| 185 | + result, err := cfDistribution.CreateDistribution(bucketName, certificateSSLArn, domain) |
| 186 | + if err != nil { |
| 187 | + fmt.Println("Couldn't create distribution. Please check error message and try again.") |
| 188 | + fmt.Println(err) |
| 189 | + return |
| 190 | + } |
| 191 | + fmt.Println(result.Distribution.ARN) |
| 192 | +} |
| 193 | + |
| 194 | +// snippet-end:[cloudfront.go-v2.CreateDistribution] |
0 commit comments