9

Nel Terraform documentation for AWS_API_GATEWAY_INTEGRATION, sono supportati i seguenti parametri:Specifica un modello di richiesta per un aws_api_gateway_integration in Terraform

  • rest_api_id
  • RESOURCE_ID
  • http_method
  • tipo
  • uri
  • integration_http_method

danno questo esempio:

resource "aws_api_gateway_integration" "MyDemoIntegration" { 
    rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}" 
    resource_id = "${aws_api_gateway_resource.MyDemoResource.id}" 
    http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}" 
    type = "MOCK" 
} 

Ma vorrei specificare un modello di mappatura (così come un'integrazione Lambda), come è possibile con l'interfaccia utente:

enter image description here

Tuttavia Non vedo modo di farlo con Terraform. È possibile?

Nota: Quello che sto facendo attualmente è apply ING il resto della configurazione (lambda, s3, iam ecc ...), e poi aggiungendo modello di mappatura manualmente in seguito (così come il tipo di integrazione di lambda) .

Ma poi ogni volta che ho terraform apply per applicare qualche altra configurazione (ad esempio: s3), Terraform ripristina il modello di associazione e il tipo di integrazione.

Il piano "tornando" si presenta così:

~ aws_api_gateway_integration.post_hit_integration 
    request_templates.#:    "1" => "0" 
    request_templates.application/json: "{\n \"body\" : $input.json('$'),\n \"headers\": {\n #foreach($param in $input.params().header.keySet())\n \"$param\": \"$util.escapeJavaScript($input.params().header.get($param))\" #if($foreach.hasNext),#end\n \n #end \n },\n \"stage\" : \"$context.stage\"\n}" => "" 
    uri:        "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:000000000000:function:create_hit/invocations" => "" 

risposta

11

Sulla base di this issue, ecco un config che funziona:

(Devi usare i params uri, type, integration_http_method e request_templates)

# API 
resource "aws_api_gateway_rest_api" "my_api" { 
    name = "my_api" 
    description = "My Api for adding pets" 
} 

# Resource 
resource "aws_api_gateway_resource" "pets_resource" { 
    rest_api_id = "${aws_api_gateway_rest_api.my_api.id}" 
    parent_id = "${aws_api_gateway_rest_api.my_api.root_resource_id}" 
    path_part = "pets" 
} 

# Method 
resource "aws_api_gateway_method" "post_pet_method" { 
    rest_api_id = "${aws_api_gateway_rest_api.my_api.id}" 
    resource_id = "${aws_api_gateway_resource.pets_resource.id}" 
    http_method = "POST" 
    authorization = "NONE" 
} 

# Integration 
resource "aws_api_gateway_integration" "post_pet_integration" { 
    rest_api_id = "${aws_api_gateway_rest_api.my_api.id}" 
    resource_id = "${aws_api_gateway_resource.pets_resource.id}" 
    http_method = "${aws_api_gateway_method.post_pet_method.http_method}" 
    uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${aws_lambda_function.create_pet.arn}/invocations" 
    type = "AWS"       # Documentation not clear 
    integration_http_method = "POST"  # Not documented 
    request_templates = {     # Not documented 
    "application/json" = "${file("api_gateway_body_mapping.template")}" 
    } 
} 

e contenuto della api_gate way_body_mapping.template:

{ 
    "body" : $input.json('$'), 
    "headers": { 
    #foreach($param in $input.params().header.keySet()) 
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end 

    #end 
    }, 
    "stage" : "$context.stage" 
} 
+0

Ho cercato in tutto il mondo per questo, vi ringrazio molto. Stavo per iniziare a elencare esaustivamente le intestazioni. – jstlaurent

1

Se si utilizza una funzione lambda come il punto finale, il tipo di integrazione sarebbe "AWS".

Questo è the AWS documentation che spiega la creazione di un'integrazione Lambda.

Questo è a GitHub post che mostra come ciò può essere fatto usando Terraform.

Spero che questo aiuti! Fateci sapere se avete domande.

1

Se si vuole avere in linea piuttosto che in un modulo separato che si può fare:

request_templates = {     
    "application/json" = <<REQUEST_TEMPLATE 
    { 
    "body" : $input.json('$'), 
    "headers": { 
    #foreach($param in $input.params().header.keySet()) 
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end 

    #end 
    }, 
    "stage" : "$context.stage" 
    } 
    REQUEST_TEMPLATE 
} 
Problemi correlati