[AWS] Lab: Create a REST API with API Gateway and Lambda with CORS

1. Create a Lambda function: called `get-groups`

const data = [
    {name: 'angular', id: 1},
    {name: 'react', id: 2},
    {name: 'vue', id: 3}
];

exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({items: data}),
    };
    return response;
};

To prevent CORS problems, we enables CORS headers as well.

 

2. Create an API Gateway

  • For REST API
  • Build a New Rest API
  • Actions -> Create Resource, this is used as endpoint, let's named `groups`
  • Action -> Create Method, `GET`

[AWS] Lab: Create a REST API with API Gateway and Lambda with CORS

  • Actiona -> Deploy

[AWS] Lab: Create a REST API with API Gateway and Lambda with CORS

 

3. After deployed, it generate a URL you can used for testing in POSTMAN

GET: https://<id>.execute-api.us-east-1.amazonaws.com/dev/groups

上一篇:同源策略&CORS跨域漏洞&JSONP跨域漏洞


下一篇:Cors跨域配置