简体   繁体   中英

Enable POST on AWS API with CloudFront

I've followed this tutorial ( https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html ) to build a basic API.

Then, on the AWS API Gateway for lambda-pipeline-stack I added a custom domain (eg api.example.com) which points to a CloudFront distribution.

When I tried changing the template to use POST request instead of GET , I received an error:

This distribution is not configured to allow the HTTP request method that was used for this request.
The distribution supports only cachable requests.

However, when I go into my CloudFront distributions to try to enable POST , that distribution is not accessible. How can I enable POST for this type of API?

Here is my template for reference:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Shows time
Resources:
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs10
      CodeUri: ./
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /time
            Method: POST

you need to allow POST on CloudFront because only GET, HEAD are allowed by default

Type: AWS::CloudFront::Distribution
Properties:
  DistributionConfig:
    ...
    HttpVersion: http2
    DefaultCacheBehavior:
     ...
      AllowedMethods: [DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT]

enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM