Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation - 'AWS_PROXY' currently only supports Lambda function

I have the following error when I want to create a stack with cloudformation:

The following resource(s) failed to create: [ApiSyncLogsPost]. . Rollback requested by user.
CREATE_FAILED   AWS::ApiGateway::Method ApiSyncLogsPost Integrations of type 'AWS_PROXY' currently only supports Lambda function and Firehose stream invocations. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 8bff48b0-93e4-11e8-a183-5de2b976f282)

I didn't find anything relevant regarding this error on google, when I search about it I just get a lot of AWS cloudformation documentation.

What I think is happening is that ApiSyncLogsPost resource is not defined properly, but the examples I found on the internet are similar.

What needs to be changed to solve that error?

Here is the cloudformation json:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
    "AWS::CloudFormation::Designer": {
        "a73e3686-2291-49a2-b052-565c5f85a9c9": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 270,
                "y": 120
            },
            "z": 2,
            "parent": "d68e59d4-72c0-425a-9b7b-227cbac3252d",
            "embeds": []
        },
        "0665ec07-964b-4c90-86c5-5f7731cece7b": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 420,
                "y": 120
            },
            "z": 1,
            "embeds": []
        },
        "d68e59d4-72c0-425a-9b7b-227cbac3252d": {
            "size": {
                "width": 300,
                "height": 240
            },
            "position": {
                "x": 60,
                "y": 90
            },
            "z": 1,
            "embeds": [
                "a73e3686-2291-49a2-b052-565c5f85a9c9",
                "2cefafdd-86fd-4cbe-b42e-c638a8c23a19"
            ],
            "iscontainedinside": [
                "a86c6b83-5254-4848-943e-db6e35f09bf6"
            ]
        },
        "2cefafdd-86fd-4cbe-b42e-c638a8c23a19": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 60,
                "y": 120
            },
            "z": 2,
            "parent": "d68e59d4-72c0-425a-9b7b-227cbac3252d",
            "embeds": [],
            "iscontainedinside": [
                "d68e59d4-72c0-425a-9b7b-227cbac3252d",
                "d68e59d4-72c0-425a-9b7b-227cbac3252d",
                "a86c6b83-5254-4848-943e-db6e35f09bf6"
            ]
        },
        "a86c6b83-5254-4848-943e-db6e35f09bf6": {
            "size": {
                "width": 330,
                "height": 330
            },
            "position": {
                "x": 510,
                "y": 90
            },
            "z": 1,
            "embeds": [
                "66745c5a-f3ad-4b8c-b575-e90d73cbf19b",
                "cf340921-6a8b-45f2-8a2a-14ffb0e537e8",
                "925587cf-52b3-4507-9131-6be6777905fe"
            ]
        },
        "66745c5a-f3ad-4b8c-b575-e90d73cbf19b": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 540,
                "y": 150
            },
            "z": 2,
            "parent": "a86c6b83-5254-4848-943e-db6e35f09bf6",
            "embeds": [],
            "iscontainedinside": [
                "a86c6b83-5254-4848-943e-db6e35f09bf6"
            ]
        },
        "cf340921-6a8b-45f2-8a2a-14ffb0e537e8": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 680,
                "y": 200
            },
            "z": 2,
            "parent": "a86c6b83-5254-4848-943e-db6e35f09bf6",
            "embeds": [],
            "iscontainedinside": [
                "a86c6b83-5254-4848-943e-db6e35f09bf6"
            ]
        },
        "925587cf-52b3-4507-9131-6be6777905fe": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 540,
                "y": 270
            },
            "z": 2,
            "parent": "a86c6b83-5254-4848-943e-db6e35f09bf6",
            "embeds": [],
            "iscontainedinside": [
                "a86c6b83-5254-4848-943e-db6e35f09bf6"
            ],
            "dependson": [
                "2cefafdd-86fd-4cbe-b42e-c638a8c23a19"
            ]
        }
    }
},
"Parameters": {
    "DatabaseUrl": {
        "Description": "DATABASE_URL taken from elastic bean. Example: postgres://my_own_user:my_own_access@my_own_link.rds.amazonaws.com:PORT/my_database_name",
        "Type": "String"
    },
    "LogentriesToken": {
        "Description": "",
        "Type": "String"
    },
    "RedisUrl": {
        "Description": "",
        "Type": "String"
    },
    "DeploymentStage": {
        "Description": "",
        "Type": "String",
        "Default": "staging"
    },
    "StackResourcePrefix": {
        "Description": "The name will be used to create all the resources that belongs to the same stack.",
        "Default": "StagingSyncLogs",
        "Type": "String"
    }
},
"Resources": {
    "ErrorCfn": {
        "Type": "AWS::ApiGateway::Model",
        "Properties": {
            "ContentType": "application/json",
            "Name": "ErrorCfn",
            "RestApiId": {
                "Ref": "SyncLogsApi"
            },
            "Schema": {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "Error Schema",
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string"
                    }
                }
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "cf340921-6a8b-45f2-8a2a-14ffb0e537e8"
            }
        }
    },
    "UserPropertyModel": {
        "Type": "AWS::ApiGateway::Model",
        "Properties": {
            "ContentType": "application/json",
            "Name": "UserPropertyModel",
            "RestApiId": {
                "Ref": "SyncLogsApi"
            },
            "Schema": {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "UserPropertyModel",
                "type": "object",
                "properties": {
                    "Name": {
                        "type": "string"
                    }
                }
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "66745c5a-f3ad-4b8c-b575-e90d73cbf19b"
            }
        }
    },
    "LambdaExecutionRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                                "lambda.amazonaws.com"
                            ]
                        },
                        "Action": [
                            "sts:AssumeRole"
                        ]
                    }
                ]
            },
            "Path": "/",
            "Policies": [
                {
                    "PolicyName": "PolicyNameForLambdaRole",
                    "PolicyDocument": {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Sid": "1",
                                "Effect": "Allow",
                                "Action": [
                                    "lambda:InvokeFunction"
                                ],
                                "Resource": [
                                    "*"
                                ]
                            },
                            {
                                "Sid": "2",
                                "Effect": "Allow",
                                "Action": [
                                    "logs:CreateLogGroup",
                                    "logs:CreateLogStream",
                                    "logs:PutLogEvents"
                                ],
                                "Resource": [
                                    "*"
                                ]
                            }
                        ]
                    }
                }
            ],
            "RoleName": {
                "Fn::Join": [
                    "_",
                    [
                        {
                            "Ref": "StackResourcePrefix"
                        },
                        "PostgresCron",
                        "Role"
                    ]
                ]
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "0665ec07-964b-4c90-86c5-5f7731cece7b"
            }
        }
    },
    "SyncLogsLambda": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "myBucket456464",
                "S3Key": "myCode34535.zip"
            },
            "Environment": {
                "Variables": {
                    "DATABASE_URL": {
                        "Ref": "DatabaseUrl"
                    },
                    "LOGENTRIES_TOKEN": {
                        "Ref": "LogentriesToken"
                    },
                    "REDIS_URL": {
                        "Ref": "RedisUrl"
                    }
                }
            },
            "Tags": [
                {
                    "Key": "DeploymentStage",
                    "Value": {
                        "Ref": "DeploymentStage"
                    }
                }
            ],
            "FunctionName": {
                "Fn::Join": [
                    "",
                    [
                        {
                            "Ref": "StackResourcePrefix"
                        },
                        "_",
                        "lambda"
                    ]
                ]
            },
            "Description": "Lambda function for syncing logs to logentries",
            "Handler": "index.handler",
            "Runtime": "nodejs6.10",
            "MemorySize": 128,
            "Timeout": 300,
            "Role": {
                "Fn::GetAtt": [
                    "LambdaExecutionRole",
                    "Arn"
                ]
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "a73e3686-2291-49a2-b052-565c5f85a9c9"
            }
        }
    },
    "ProxyResource": {
        "Type": "AWS::ApiGateway::Resource",
        "Properties": {
            "RestApiId": {
                "Ref": "SyncLogsApi"
            },
            "ParentId": {
                "Fn::GetAtt": [
                    "SyncLogsApi",
                    "RootResourceId"
                ]
            },
            "PathPart": "synclogs"
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "d68e59d4-72c0-425a-9b7b-227cbac3252d"
            }
        }
    },
    "SyncLogsApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
            "Name": "Sync logs Api",
            "Description": "Sync logs when this link is called.",
            "FailOnWarnings": true
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "a86c6b83-5254-4848-943e-db6e35f09bf6"
            }
        }
    },
    "ApiSyncLogsPost": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "ResourceId": {
                "Ref": "ProxyResource"
            },
            "RestApiId": {
                "Ref": "SyncLogsApi"
            },
            "HttpMethod": "POST",
            "AuthorizationType": "NONE",
            "Integration": {
                "Type": "AWS_PROXY",
                "IntegrationHttpMethod": "POST",
                "Uri": {
                    "Fn::Join": [
                        "",
                        [
                            "arn:aws:apigateway:",
                            {
                                "Ref": "AWS::Region"
                            },
                            ":lambda:path/2015-03-31/functions/",
                            {
                                "Fn::GetAtt": [
                                    "SyncLogsLambda",
                                    "Arn"
                                ]
                            },
                            "/synclogs"
                        ]
                    ]
                }
            },
            "MethodResponses": [
                {
                    "ResponseModels": {
                        "application/json": {
                            "Ref": "UserPropertyModel"
                        }
                    },
                    "StatusCode": 200
                },
                {
                    "ResponseModels": {
                        "application/json": {
                            "Ref": "ErrorCfn"
                        }
                    },
                    "StatusCode": 404
                },
                {
                    "ResponseModels": {
                        "application/json": {
                            "Ref": "ErrorCfn"
                        }
                    },
                    "StatusCode": 500
                }
            ]
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "2cefafdd-86fd-4cbe-b42e-c638a8c23a19"
            }
        }
    },
    "RestApiDeployment": {
        "Type": "AWS::ApiGateway::Deployment",
        "Properties": {
            "RestApiId": {
                "Ref": "SyncLogsApi"
            },
            "StageName": "Staging"
        },
        "DependsOn": [
            "ApiSyncLogsPost"
        ],
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "925587cf-52b3-4507-9131-6be6777905fe"
            }
        }
    }
}
}
like image 320
Sas Gabriel Avatar asked Jan 03 '23 01:01

Sas Gabriel


1 Answers

The error is due to improper value for Integration.Uri in the ApiSyncLogsPost resource. The Uri should have the form:

arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}

Example:

arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:012345678901:function:MyLambda/invocations

In your case, the string "/synclogs" at the end which should actually be "/invocations"

The following Integration.Uri for ApiSyncLogsPost should fix the issue:

{
  "Uri": {
    "Fn::Join": [
      "",
      [
        "arn:aws:apigateway:",
        {
          "Ref": "AWS::Region"
        },
        ":lambda:path/2015-03-31/functions/",
        {
          "Fn::GetAtt": [
            "SyncLogsLambda",
            "Arn"
          ]
        },
        "/invocations"
      ]
    ]
  }
}
like image 120
user818510 Avatar answered Jan 04 '23 17:01

user818510