Skip to content

Instantly share code, notes, and snippets.

@mikebroberts
Last active March 7, 2024 13:45
Show Gist options
  • Save mikebroberts/09e8c8b4aaac6e26149c4622fd492414 to your computer and use it in GitHub Desktop.
Save mikebroberts/09e8c8b4aaac6e26149c4622fd492414 to your computer and use it in GitHub Desktop.

Revisions

  1. mikebroberts revised this gist May 14, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion template.yaml
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Description: CloudFront Functions Demo

    # This example shows how to use CloudFront, CloudFront Functions, and CloudFormation.
    # In this simple example we setup CloudFront so that on any request we redirect to another site.
    # While simple, this example can be expanded to provide typical redirect scenarios, based
    # While basic, this example can be expanded to provide typical redirect scenarios, based
    # on the event passed to the function.

    # This example written by Mike Roberts (https://twitter.com/mikebroberts), Symphonia.
  2. mikebroberts revised this gist May 14, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions template.yaml
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ Description: CloudFront Functions Demo
    # While simple, this example can be expanded to provide typical redirect scenarios, based
    # on the event passed to the function.

    # This example written by Mike Roberts, Symphonia. For more ideas about using AWS more effectively,
    # see our blog at https://blog.symphonia.io/
    # This example written by Mike Roberts (https://twitter.com/mikebroberts), Symphonia.
    # For more ideas about using AWS more effectively,see our blog at https://blog.symphonia.io/

    Parameters:
    RedirectDomainName:
  3. mikebroberts created this gist May 14, 2021.
    60 changes: 60 additions & 0 deletions template.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    Description: CloudFront Functions Demo

    # This example shows how to use CloudFront, CloudFront Functions, and CloudFormation.
    # In this simple example we setup CloudFront so that on any request we redirect to another site.
    # While simple, this example can be expanded to provide typical redirect scenarios, based
    # on the event passed to the function.

    # This example written by Mike Roberts, Symphonia. For more ideas about using AWS more effectively,
    # see our blog at https://blog.symphonia.io/

    Parameters:
    RedirectDomainName:
    Type: String
    Default: www.google.com

    Outputs:
    # Go to the value of this output in a browser, and you'll be redirected to the domain specified in RedirectDomainName
    CloudfrontDomainName:
    Value: !GetAtt CloudFrontDistribution.DomainName

    Resources:
    CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
    # This example doesn't set custom aliases, but those can be added in the usual way
    DistributionConfig:
    Enabled: true
    DefaultCacheBehavior:
    TargetOriginId: redirectOrigin
    ViewerProtocolPolicy: 'redirect-to-https'
    # "Managed-CachingDisabled" from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html
    CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
    FunctionAssociations:
    - EventType: viewer-request
    FunctionARN: !GetAtt RedirectFunction.FunctionMetadata.FunctionARN
    # CloudFront requires at least one origin, even though we're always going to redirect
    Origins:
    - DomainName: !Ref RedirectDomainName
    Id: redirectOrigin
    CustomOriginConfig:
    OriginProtocolPolicy: match-viewer

    RedirectFunction:
    Type: AWS::CloudFront::Function
    Properties:
    AutoPublish: true
    FunctionCode: !Sub |
    function handler(event) {
    return {
    statusCode: 302,
    statusDescription: 'Found',
    headers: {
    location: { value: 'https://${RedirectDomainName}/' }
    }
    }
    }
    FunctionConfig:
    Comment: !Sub 'Redirect to ${RedirectDomainName}'
    Runtime: cloudfront-js-1.0
    Name: !Sub "${AWS::StackName}-redirectFunction"