11# serverless.yml
22
33service: products-service
44
55provider:
66 name: aws
77 runtime: nodejs6.10
88
99functions:
1010 getProduct:
1111 handler: handler.getProduct
1212 events:
1313 - http:
1414 path: product/{id}
1515 method: get
1616 cors: true # <-- CORS!
1717 createProduct:
1818 handler: handler.createProduct
1919 events:
2020 - http:
2121 path: product
2222 method: post
2323 cors: true # <-- CORS!
11'use strict';
22
33module.exports.getProduct = (event, context, callback) => {
44
55 // Do work to retrieve Product
66 const product = retrieveProduct(event);
77
88 const response = {
99 statusCode: 200,
1010 headers: {
1111 'Access-Control-Allow-Origin': '*',
1212 'Access-Control-Allow-Credentials': true,
1313 },
1414 body: JSON.stringify({
1515 product: product
1616 }),
1717 };
1818
1919 callback(null, response);
2020};
2121
2222module.exports.createProduct = (event, context, callback) => {
2323
2424 // Do work to create Product
2525 const product = createProduct(event);
2626
2727 const response = {
2828 statusCode: 200,
2929 headers: {
3030 'Access-Control-Allow-Origin': '*',
3131 'Access-Control-Allow-Credentials': true,
3232 },
3333 body: JSON.stringify({
3434 product: product
3535 }),
3636 };
3737
3838 callback(null, response);
3939};
11# serverless.yml
22
33...
44
55 resources:
66 Resources:
77 GatewayResponseDefault4XX:
88 Type: 'AWS::ApiGateway::GatewayResponse'
99 Properties:
1010 ResponseParameters:
1111 gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
1212 gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
1313 ResponseType: DEFAULT_4XX
1414 RestApiId:
1515 Ref: 'ApiGatewayRestApi'
1616 ```