Skip to content

Instantly share code, notes, and snippets.

View chrismckinnel's full-sized avatar

Chris McKinnel chrismckinnel

View GitHub Profile
# Function to automatically call cfn-lint when deploying AWS CloudFormation templates
function aws() {
export COMMAND=$@
if [[ $COMMAND == cloudformation\ create-stack* ]] || [[ $COMMAND == cloudformation\ update-stack* ]];
then
while [[ $# -gt 0 ]]
do
key="$1"
├── .aws-sam
│ └── build
│ ├── RedirectLambda
│ │ ├── RedirectLambda.js
│ │ └── package.json
│ └── template.yaml
├── lambda-edge-prerequisites.yaml
├── lambda-edge.yaml
├── lambdas
│ ├── RedirectLambda.js
├── lambda-edge-prerequisites.yaml
├── lambda-edge.yaml
├── lambdas
│ ├── RedirectLambda.js
│ └── package.json
└── packaged
└── lambda-edge.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Full stack to demo Lambda@Edge for CloudFront redirects
Parameters:
RedirectLambdaName:
Type: String
Default: redirect-lambda
Resources:
CloudFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: true
TargetOriginId: google-origin
ViewerProtocolPolicy: redirect-to-https
{
"name": "lambda-redirect",
"version": "1.0.1",
"description": "Redirect lambda using Lambda@Edge and CloudFront",
"author": "Chris McKinnel",
"license": "MIT"
}
'use strict';
exports.handler = async (event) => {
console.log('Event: ', JSON.stringify(event, null, 2));
let request = event.Records[0].cf.request;
const redirects = {
'/path-1': 'https://consegna.cloud/',
'/path-2': 'https://www.amazon.com/',
};
RedirectLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/
FunctionName: !Ref RedirectLambdaName
Handler: RedirectLambda.handler
Role: !GetAtt RedirectLambdaFunctionRole.Arn
Runtime: nodejs10.x
AutoPublishAlias: live
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Full stack to demo Lambda@Edge for CloudFront redirects
Parameters:
RedirectLambdaName:
Type: String
Default: redirect-lambda
Resources:
AWSTemplateFormatVersion: '2010-09-09'
Resources:
RedirectLambdaBucket:
Type: AWS::S3::Bucket
Outputs:
RedirectLambdaBucketName:
Description: Redirect lambda package S3 bucket name
Value: !Ref RedirectLambdaBucket