1

There is this wicked post about configuring an API Gateway method for CORS through CloudFormation, and I'm giving it a go. I want to create the following endpoint with two methods, "options" and "post":

/image/submit

Here is my CF template snippet:

ApiDefault:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Name: "Stash-Default"
      FailOnWarnings: true
  ApiDefaultDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn:
      - "ApiMethodImageSubmitPost"
      - "ApiMethodImageSubmitOption"
    Properties:
      RestApiId: !Ref "ApiDefault"
      StageName: "v1"
  ApiResourceImage:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      ParentId: !GetAtt ["ApiDefault", "RootResourceId"]
      PathPart: "image"
      RestApiId: !Ref "ApiDefault"
  ApiResourceImageSubmit:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      ParentId: !Ref "ApiResourceImage"
      PathPart: "submit"
      RestApiId: !Ref "ApiDefault"
  ApiMethodImageSubmitPost:
    Type: "AWS::ApiGateway::Method"
    Properties:
      HttpMethod: "POST"
      AuthorizationType: "NONE"
      MethodResponses:
        - StatusCode: "200"
      Integration:
        IntegrationHttpMethod: "POST"
        Type: "AWS_PROXY"
        IntegrationResponses:
          - StatusCode: "200"
        Credentials: !GetAtt [ "ExecuteApiMethodImageSubmit", "Arn" ]
        Uri: !Sub
          - "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
          - lambdaArn: !GetAtt [ "ImageReceive", "Arn" ]
      RestApiId: !Ref "ApiDefault"
      ResourceId: !Ref "ApiResourceImageSubmit"
  ApiMethodImageSubmitOption:
    Type: "AWS::ApiGateway::Method"
    Properties:
      HttpMethod: "OPTIONS"
      AuthorizationType: "NONE"
      Integration:
        Type: "MOCK"
        IntegrationResponses:
          - StatusCode: "200"
            ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
      MethodResponses:
        - StatusCode: "200"
          ResponseModels:
            application/json: "Empty"
          ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: false
            method.response.header.Access-Control-Allow-Methods: false
            method.response.header.Access-Control-Allow-Origin: false
      RestApiId: !Ref "ApiDefault"
      ResourceId: !Ref "ApiResourceImageSubmit"

It bombs saying ApiMethodImageSubmitPost:

Method already exists for this resource (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: 454cf46a-b434-4626-bd4b-b6d4fe21142c)

Can you create two http-methods for a single API resource in this fashion? I'm not having a ton of luck with AWS' docs on this one.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • I'm speechless. I (in desperation) deleted and re-wrote (verbatim) the `ApiMethodImageSubmitPost` resource and it works. I dunno? AWS wanted to mess with me... – Adam Jul 30 '19 at 12:34
  • 1
    Glad that it worked for you. Just for your knowledge - You can create multiple methods for same resource. You can have all methods provided by AWS API G also under one resource. – node_saini Aug 02 '19 at 14:56

0 Answers0