Created
February 4, 2021 20:30
-
-
Save tforster/14bbea2f170bc8a2feeedd2ee0d2b971 to your computer and use it in GitHub Desktop.
CORS for AWS Lambda using Serverless Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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