Raj
Raj

Reputation: 623

Troposphere template for Cloudfront using S3 website origin

I am trying to automate the deployment of a static website. I have a static website (angular/javascript/html) sitting in a bucket, and need to use the AWS Cloudfront CDN.

   s3dnsname = self.template.add_parameter(Parameter(
        "S3DNSName",
        Description="The DNS name of an existing S3 bucket to use as the "
                    "Cloudfront distribution origin",
        Type="String",
    ))

    myDistribution = self.template.add_resource(Distribution(
        "myDistribution",
        DistributionConfig=DistributionConfig(
            Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
                            S3OriginConfig=S3Origin())],
            DefaultCacheBehavior=DefaultCacheBehavior(
                TargetOriginId="Origin 1",
                ForwardedValues=ForwardedValues(
                    QueryString=False
                ),
                ViewerProtocolPolicy="allow-all"),
            Enabled=True,
            HttpVersion='http2'

        )
    ))

This is my code and I am getting an error "The parameter Origin DomainName does not refer to a valid S3 bucket."

I looked into several articles/blogs on this and they all point to adding the following:

custom_origin_config { 
    origin_protocol_policy = "http-only" 
    http_port              = 80 
    https_port             = 443 
    origin_ssl_protocols   = ["TLSv1.2", "TLSv1.1", "TLSv1"] 
} 

Can anyone please point me to where I need to add this part?

Update: I tried to replace S3OriginConfig with CustomOriginConfig below like this:

   myDistribution = self.template.add_resource(Distribution(
        "myDistribution",
        DistributionConfig=DistributionConfig(
            Origins=[Origin(
                Id="Origin 1",
                DomainName=Ref(s3dnsname),
                CustomOriginConfig=CustomOriginConfig(
                    OriginProtocolPolicy="http-only",
                    HTTPPort=80,
                    HTTPSPort=443))],
            DefaultCacheBehavior=DefaultCacheBehavior(
                TargetOriginId="Origin 1",
                ForwardedValues=ForwardedValues(
                    QueryString=False
                ),
                ViewerProtocolPolicy="allow-all"),
            Enabled=True,
            HttpVersion='http2'
        )
    ))

After running this, I get an error "NameError: global name 'CustomOriginConfig' is not defined"

I am missing something here which I am not able to identify.

Upvotes: 1

Views: 787

Answers (1)

Raj
Raj

Reputation: 623

Here is the solution that worked for me:

from troposphere.cloudfront import S3Origin, CustomOrigin

And changed the code to:

Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
                            CustomOriginConfig=CustomOrigin(
                                HTTPPort=80,
                                HTTPSPort=443,
                                OriginProtocolPolicy="http-only"
                            ))],

I am using this example: https://github.com/cloudtools/troposphere/blob/master/examples/CloudFront_S3.py

Hope this helps anyone facing the same issue.

Upvotes: 1

Related Questions