Im trying to create an api gateway that incorporates two api’s:-
1. v1/user/sub/create
2. v1/user/sub/update
I have created different modules for different resources:-
Module for method:-
resource "aws_api_gateway_method" "api_gateway_method" {
rest_api_id = var.rest_api_id
resource_id = var.resource_id
api_key_required = false
http_method = var.http_method
authorization = "NONE"
request_parameters = var.request_parameters
}
resource "aws_api_gateway_method_response" "method_response_201" {
rest_api_id = var.rest_api_id
resource_id = var.resource_id
http_method = aws_api_gateway_method.api_gateway_method.http_method
status_code = 201
response_models = {
"application/json" = "Empty"
}
}
Module for integration:-
/*
Module to integrate API Gateway with an AWS resource like SQS or Lambda
*/
resource "aws_api_gateway_integration" "api_gateway_integration" {
rest_api_id = var.rest_api_id
resource_id = var.resource_id
http_method = var.http_method
type = "AWS"
integration_http_method = "POST"
passthrough_behavior = "NEVER"
credentials = var.iam_role_arn
uri = var.aws_resource_uri # uri of the AWS resource behind this API Gateway
request_parameters = var.request_parameters
request_templates = var.request_templates
}
# Define success response
resource "aws_api_gateway_integration_response" "integration_response_2xx" {
rest_api_id = var.rest_api_id
resource_id = var.resource_id
http_method = var.http_method
status_code = var.status_code
selection_pattern = "^2[0-9][0-9]"
response_templates = var.response_templates
depends_on = [
aws_api_gateway_integration.api_gateway_integration
]
}
Module for deployment:-
resource "aws_api_gateway_deployment" "api_gateway_deployment" {
rest_api_id = var.rest_api_id
stage_name = var.stage_name
depends_on = []
}
resource "aws_api_gateway_method_settings" "deployment_settings" {
rest_api_id = var.rest_api_id
stage_name = var.stage_name
method_path = "*/*"
settings {
metrics_enabled = true
logging_level = "ERROR"
data_trace_enabled = true
}
depends_on = [aws_api_gateway_deployment.api_gateway_deployment]
}
And then using them in my playbook to deploy two api’s:-
# Create a new API gateway
module "api_gateway" {
}
# /v1
resource "aws_api_gateway_resource" "api v1" {
}
// other resources
# /v1/user/sub/create
resource "aws_api_gateway_resource" "create api" {
rest_api_id = module.api_gateway.id
parent_id = // id to parent sub resource
path_part = "create"
}
#v1/user/sub/update
resource "aws_api_gateway_resource" "api update" {
rest_api_id = module.api_gateway.rest_api_id
parent_id = //id to parent sub resource
path_part = "user"
}
module "method_create" {
source = xxxx
rest_api_id = {rest api id}
resource_id = {create api resource id}
http_method = "POST"
request_parameters = {}
}
module "method_update" {
source = xxxx
rest_api_id = {rest api id}
resource_id = {update api resource id}
http_method = "POST"
request_parameters = {}
}
module "integration_create" {
source = xxxx
rest_api_id = {rest api id}
resource_id = {update api resource id}
iam_role_arn = xxxx
aws_resource_uri = xxxx
status_code = 201
http_method = "POST"
request_parameters = {
"integration.request.header.Content-Type" = xxxx,
}
request_templates = {
"application/json" = "Action=SendMessage&MessageBody=$input.body"
}
response_templates = {
"application/json" = xxxx
}
}
module "integration_update" {
source = xxxx
rest_api_id = {rest api id}
resource_id = {update api resource id}
iam_role_arn = xxxx
aws_resource_uri = xxxx
status_code = 201
http_method = "POST"
request_parameters = {
"integration.request.header.Content-Type" = xxxxx,
}
request_templates = {
"application/json" = "Action=SendMessage&MessageBody=$input.body"
}
response_templates = {
"application/json" = xxxx
}
}
module "deployment" {
source = xxx
rest_api_id = {rest api gateway id}
stage_name = var.stage_name
hosted_zone_id = var.hosted_zone_id
api_gateway_route53_domain_name = var.api_gateway_route53_domain_name
cluster_certificate_arn = var.cluster_certificate_arn
depends_on = [
module.integartion_update,
module.integration_create,
module.method_create,
module.method_update
]
}
when im deploying it for only v1/user/sub/create
then everything is working fine, but when im adding the second api v1/user/sub/update
resource, method, integration for that
then it is throwing error.
Can someone tell what im doing wrong here? It shows invalid method identifier but i have created a method for the second api and also created integration for the api, im not getting why am i getting this error. I have also mentioned the dependencies