Skip to content

Commit a41aecc

Browse files
committed
cfn edge function
1 parent 955d8d4 commit a41aecc

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

cloudformation/main.yml

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,6 @@ Resources:
859859
OriginProtocolPolicy: https-only
860860
Enabled: true
861861
Aliases:
862-
- !Join
863-
- ""
864-
- - "go."
865-
- !FindInMap
866-
- ApiGwConfig
867-
- !Ref RunEnvironment
868-
- EnvDomainName
869862
- !Join
870863
- ""
871864
- - "ical."
@@ -903,6 +896,76 @@ Resources:
903896
HttpVersion: http2
904897
PriceClass: PriceClass_100
905898

899+
LinkryRecordsCloudfrontStore:
900+
Type: AWS::CloudFront::KeyValueStore
901+
Properties:
902+
Name: infra-core-api-cloudfront-linkry-kv
903+
904+
LinkryRecordsCloudfrontFunction:
905+
Type: 'AWS::CloudFront::Function'
906+
Properties:
907+
Name: infra-core-api-cloudfront-linkry-redir
908+
FunctionConfig:
909+
Comment: 'Linkry Redirect Cloudfront Function'
910+
Runtime: 'cloudfront-js-2.0'
911+
KeyValueStoreAssociations:
912+
- KeyValueStoreARN: !Sub '${LinkryRecordsCloudfrontStore.Arn}'
913+
FunctionCode: !Sub |
914+
const kvsId = '${LinkryRecordsCloudfrontStore.Id}';
915+
const defaultPath = "https://acm.illinois.edu"
916+
function handler(event) {
917+
const request = event.request;
918+
const kvs = event.keyValueStore; // Access the associated KeyValueStore
919+
const path = request.uri.replace(/^\/+/, ''); // Clean leading slashes
920+
921+
// Lookup key in KeyValueStore
922+
const value = kvs.get(path);
923+
924+
if (value) {
925+
return {
926+
statusCode: 302,
927+
statusDescription: 'Found',
928+
headers: {
929+
location: { value: value }
930+
}
931+
};
932+
} else {
933+
// Default redirect if key is not found
934+
return {
935+
statusCode: 302,
936+
statusDescription: 'Found',
937+
headers: {
938+
location: { value: defaultPath } // Change to your default
939+
}
940+
};
941+
}
942+
}
943+
AutoPublish: true
944+
945+
AppLinkryCloudfrontDistribution:
946+
Type: AWS::CloudFront::Distribution
947+
Properties:
948+
DistributionConfig:
949+
Enabled: true
950+
DefaultCacheBehavior:
951+
ViewerProtocolPolicy: redirect-to-https
952+
TargetOriginId: dummyOrigin
953+
FunctionAssociations:
954+
- EventType: viewer-request
955+
FunctionARN: !GetAtt LinkryRecordsCloudfrontFunction.FunctionARN
956+
Origins:
957+
- Id: dummyOrigin
958+
DomainName: example.com
959+
CustomOriginConfig:
960+
OriginProtocolPolicy: https-only
961+
Aliases:
962+
- !Join
963+
- ""
964+
- - "go."
965+
- !FindInMap
966+
- ApiGwConfig
967+
- !Ref RunEnvironment
968+
- EnvDomainName
906969

907970
Outputs:
908971
DomainName:

0 commit comments

Comments
 (0)