Skip to content

Instantly share code, notes, and snippets.

@tforster
Created February 4, 2021 20:30
Show Gist options
  • Save tforster/14bbea2f170bc8a2feeedd2ee0d2b971 to your computer and use it in GitHub Desktop.
Save tforster/14bbea2f170bc8a2feeedd2ee0d2b971 to your computer and use it in GitHub Desktop.
CORS for AWS Lambda using Serverless Framework
// Because I can never remember the precise syntax and the Serverless Framework docs are out of date (at least when I wrote this)...
// Handler.js
resSuccess(data = {}, statusCode = 200) {
return {
statusCode,
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
},
};
}
// Serverless.yml
provider:
name: aws
region: ${env:AWS_LAMBDA_REGION}
stage: ${env:AWS_STAGE}
runtime: nodejs14.x
stackName: <redacted>-${env:AWS_STAGE}
apiName: rest-<redacted>
memorySize: 512
timeout: 6
websocketsApiName: ws-<redacted>
websocketsApiRouteSelectionExpression: $request.body.action
iamRoleStatements:
- Effect: 'Allow'
Action:
- dynamodb:PartiQLInsert
- dynamodb:PartiQLUpdate
- dynamodb:PartiQLSelect
Resource: 'arn:aws:dynamodb:<redacted>'
httpApi:
cors: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment