Skip to content

Instantly share code, notes, and snippets.

@cuperman
Created December 28, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuperman/504dd15115001bbf0a349b7e70c416ed to your computer and use it in GitHub Desktop.
Save cuperman/504dd15115001bbf0a349b7e70c416ed to your computer and use it in GitHub Desktop.
Supporting CORS using SAM
// cors_handler.js
const ALLOW_HEADERS = [
'Content-Type',
'X-Amz-Date',
'Authorization',
'X-Api-Key',
'X-Amz-Security-Token'
];
const ALLOW_METHODS = [
'GET',
'OPTIONS',
'POST'
];
const ALLOW_ORIGIN = '*';
export.preflight = (event, context, callback) => {
callback({
statusCode: 200,
headers: {
'Access-Control-Allow-Headers': ALLOW_HEADERS.join(','),
'Access-Control-Allow-Methods': ALLOW_METHODS.join(','),
'Access-Control-Allow-Origin': ALLOW_ORIGIN
},
body: ''
});
};
// template.yaml
//
// Preflight:
// Type: AWS::Serverless::Function
// Properties:
// Handler: cors_handler/preflight
// Runtime: nodejs6.10
// CodeUri: ./package.zip
// Events:
// OptionsPeople:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /people
// OptionsPerson:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /people/{personId}
// OptionsPlaces:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /places
// OptionsPlace:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /places/{placeId}
// OptionsThings:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /things
// OptionsThing:
// Type: Api
// Properties:
// Method: OPTIONS
// Path: /things/{thingId}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment