AWS Api Gateway Setup

0

This terraform can setup the apigateway. need to add the request headers using this terraform code.

resource "aws_cloudwatch_log_group" "example_log_group" { name = "example-apigateway-logs" }

resource "aws_api_gateway_account" "demo" { cloudwatch_role_arn = aws_iam_role.example_iam_role.arn }

resource "aws_iam_role" "example_iam_role" { name = "example-apigateway-logs-role"

assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Principal = { Service = "apigateway.amazonaws.com" } Action = "sts:AssumeRole" } ] }) }

resource "aws_iam_policy_attachment" "example_policy_attachment" { name = "example-apigateway-logs-attachment" policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

roles = [ aws_iam_role.example_iam_role.name ] }

data "aws_api_gateway_vpc_link" "my_api_gateway_vpc_link" { name = "terra_vpc_link" depends_on = [ aws_api_gateway_vpc_link.test ] }

resource "aws_api_gateway_rest_api" "terr-test-api-gateway" {

body = jsonencode({ openapi = "3.0.1" info = { title = "terra-api" version = "1.0" } paths = { "/testapiv1" = { get = { x-amazon-apigateway-integration = { httpMethod = "GET" payloadFormatVersion = "1.0" type = "HTTP_PROXY" uri = "https://ip-ranges.amazonaws.com/ip-ranges.json" } } }, "/testapiv2" = {

    get = {
      x-amazon-apigateway-integration = {
        httpMethod           = "GET"
        payloadFormatVersion = "1.0"
        type                 = "HTTP_PROXY"
        uri                  = "*"
          connection_type = "VPC_LINK"
          connection_id   = resource.aws_api_gateway_vpc_link.test.id
                  # Set request headers

request_parameters = { # "integration.request.header.Host" = "*" "method.request.header.X-Some-Header" = true "method.request.header.Host" = true "method.request.querystring.some-query-param" = true "method.request.path.proxy" = true } } } } } })

depends_on = [ aws_api_gateway_vpc_link.test ]

name = "terra-api"

endpoint_configuration { types = ["REGIONAL"] }

}

resource "aws_api_gateway_method_settings" "example" { rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id stage_name = aws_api_gateway_stage.api_gateway_stage.stage_name method_path = "/"

depends_on = [ aws_api_gateway_stage.api_gateway_stage ]

settings { metrics_enabled = true logging_level = "INFO"

} }

resource "aws_api_gateway_deployment" "apigateway_deployment" { rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id

triggers = { redeployment = sha1(jsonencode(aws_api_gateway_rest_api.terr-test-api-gateway.id)) }

lifecycle { create_before_destroy = true } }

resource "aws_api_gateway_stage" "api_gateway_stage" { deployment_id = aws_api_gateway_deployment.apigateway_deployment.id rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id stage_name = "test" depends_on = [aws_cloudwatch_log_group.example_log_group]

access_log_settings { destination_arn = aws_cloudwatch_log_group.example_log_group.arn format = "{"requestId":"$context.requestId","extendedRequestId":"$context.extendedRequestId","ip":"$context.identity.sourceIp","caller":"$context.identity.caller","user":"$context.identity.user","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status","protocol":"$context.protocol","responseLength":"$context.responseLength"}"

} }

resource "aws_apigatewayv2_api_mapping" "apigw_mapping" { api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id domain_name = "taapi.tricogdev.net" stage = "test" api_mapping_key = "admintest"

depends_on = [ aws_api_gateway_stage.api_gateway_stage, aws_apigatewayv2_domain_name.example

] }

resource "aws_api_gateway_vpc_link" "test" { name = "terra_vpc_link" target_arns = ["*"] tags = { Environment = "uat" } }

resource "aws_api_gateway_domain_name" "example" {

certificate_arn = "*"

domain_name = "*"

security_policy = "TLS_1_2"

endpoint_configuration {

types = ["REGIONAL"]

}

}

resource "aws_apigatewayv2_domain_name" "example" { domain_name = " 8"

domain_name_configuration { certificate_arn = "*" endpoint_type = "REGIONAL" security_policy = "TLS_1_2" } }

resource "aws_api_gateway_resource" "api-resource" {

rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id

parent_id = aws_api_gateway_rest_api.terr-test-api-gateway.root_resource_id

path_part = "demoapi"

}

resource "aws_api_gateway_method" "api_method" {

rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id

resource_id = aws_api_gateway_resource.api-resource.id

http_method = "GET"

authorization = "NONE"

}

resource "aws_api_gateway_integration" "test" {

rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id

resource_id = aws_api_gateway_resource.api-resource.id

http_method = aws_api_gateway_method.api_method.http_method

request_templates = {

"application/json" = ""

"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"

}

request_parameters = {

"integration.request.header.X-Authorization" = "'static'"

"integration.request.header.X-Foo" = "'Bar'"

}

type = "HTTP"

uri = "http://nlb-test-api-968ccd5f6070696f.elb.ap-south-1.amazonaws.com"

integration_http_method = "GET"

passthrough_behavior = "WHEN_NO_MATCH"

content_handling = "CONVERT_TO_TEXT"

connection_type = "VPC_LINK"

connection_id = "iu62f9"

}

Deepak
gefragt vor einem Jahr406 Aufrufe
1 Antwort
0

To add request headers to your AWS API Gateway using Terraform, you can modify the existing Terraform code as follows:

Make sure to replace the placeholders such as "*" with actual values for your environment.

Add the request headers under the request_parameters block in the aws_api_gateway_integration "test" resource:

request_parameters = {
  "integration.request.header.X-Authorization" = "'static'"
  "integration.request.header.X-Foo" = "'Bar'"
}

Make sure to replace 'static' and 'Bar' with the actual values you want to set for the request headers.

Here's the updated Terraform code with the added request headers:

resource "aws_cloudwatch_log_group" "example_log_group" {
  name = "example-apigateway-logs"
}

resource "aws_api_gateway_account" "demo" {
  cloudwatch_role_arn = aws_iam_role.example_iam_role.arn
}

resource "aws_iam_role" "example_iam_role" {
  name = "example-apigateway-logs-role"

  assume_role_policy = jsonencode({
    Version   = "2012-10-17"
    Statement = [
      {
        Effect    = "Allow"
        Principal = { Service = "apigateway.amazonaws.com" }
        Action    = "sts:AssumeRole"
      }
    ]
  })
}

resource "aws_iam_policy_attachment" "example_policy_attachment" {
  name       = "example-apigateway-logs-attachment"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
  roles      = [aws_iam_role.example_iam_role.name]
}

data "aws_api_gateway_vpc_link" "my_api_gateway_vpc_link" {
  name       = "terra_vpc_link"
  depends_on = [aws_api_gateway_vpc_link.test]
}

resource "aws_api_gateway_rest_api" "terr-test-api-gateway" {
  body = jsonencode({
    openapi = "3.0.1"
    info    = {
      title   = "terra-api"
      version = "1.0"
    }
    paths = {
      "/testapiv1" = {
        get = {
          x-amazon-apigateway-integration = {
            httpMethod           = "GET"
            payloadFormatVersion = "1.0"
            type                 = "HTTP_PROXY"
            uri                  = "https://ip-ranges.amazonaws.com/ip-ranges.json"
          }
        }
      },
      "/testapiv2" = {
        get = {
          x-amazon-apigateway-integration = {
            httpMethod           = "GET"
            payloadFormatVersion = "1.0"
            type                 = "HTTP_PROXY"
            uri                  = "*"
            connection_type      = "VPC_LINK"
            connection_id        = resource.aws_api_gateway_vpc_link.test.id
          }
          # Set request headers
          request_parameters = {
            "integration.request.header.Host"                     = "*"
            "method.request.header.X-Some-Header"                 = true
            "method.request.header.Host"                          = true
            "method.request.querystring.some-query-param"         = true
            "method.request.path.proxy"                           = true
          }
        }
      }
    }
  })

  depends_on = [aws_api_gateway_vpc_link.test]

  name = "terra-api"

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

resource "aws_api_gateway_method_settings" "example" {
  rest_api_id  = aws_api_gateway_rest_api.terr-test-api-gateway.id
  stage_name   = aws_api_gateway_stage.api_gateway_stage.stage_name
  method

_path  = "/"

  depends_on = [aws_api_gateway_stage.api_gateway_stage]

  settings {
    metrics_enabled = true
    logging_level   = "INFO"
  }
}

resource "aws_api_gateway_deployment" "apigateway_deployment" {
  rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id

  triggers = {
    redeployment = sha1(jsonencode(aws_api_gateway_rest_api.terr-test-api-gateway.id))
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "api_gateway_stage" {
  deployment_id = aws_api_gateway_deployment.apigateway_deployment.id
  rest_api_id   = aws_api_gateway_rest_api.terr-test-api-gateway.id
  stage_name    = "test"

  depends_on = [aws_cloudwatch_log_group.example_log_group]

  access_log_settings {
    destination_arn = aws_cloudwatch_log_group.example_log_group.arn
    format          = "{\"requestId\":\"$context.requestId\",\"extendedRequestId\":\"$context.extendedRequestId\",\"ip\":\"$context.identity.sourceIp\",\"caller\":\"$context.identity.caller\",\"user\":\"$context.identity.user\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
  }
}

resource "aws_apigatewayv2_api_mapping" "apigw_mapping" {
  api_id          = aws_api_gateway_rest_api.terr-test-api-gateway.id
  domain_name     = "taapi.tricogdev.net"
  stage           = "test"
  api_mapping_key = "admintest"

  depends_on = [
    aws_api_gateway_stage.api_gateway_stage,
    aws_apigatewayv2_domain_name.example
  ]
}

resource "aws_api_gateway_vpc_link" "test" {
  name        = "terra_vpc_link"
  target_arns = ["*"]

  tags = {
    Environment = "uat"
  }
}

resource "aws_api_gateway_domain_name" "example" {
  certificate_arn = "*"
  domain_name     = "*"
  security_policy = "TLS_1_2"

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

resource "aws_apigatewayv2_domain_name" "example" {
  domain_name = "8"

  domain_name_configuration {
    certificate_arn = "*"
    endpoint_type   = "REGIONAL"
    security_policy = "TLS_1_2"
  }
}

resource "aws_api_gateway_resource" "api-resource" {
  rest_api_id = aws_api_gateway_rest_api.terr-test-api-gateway.id
  parent_id   = aws_api_gateway_rest_api.terr-test-api-gateway.root_resource_id
  path_part   = "demoapi"
}

resource "aws_api_gateway_method" "api_method" {
  rest_api_id  = aws_api_gateway_rest_api.terr-test-api-gateway.id
  resource_id  = aws_api_gateway_resource.api-resource.id
  http_method  = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "test" {
  rest_api_id          = aws_api_gateway_rest_api.terr-test-api-gateway.id
  resource_id          = aws_api_gateway_resource.api-resource.id
  http_method          = aws_api_gateway_method.api_method.http_method
  request_templates    = {
    "application/json" = ""
    "application/xml"  = "#set($inputRoot = $input.path('$'))\n{ }"
  }
  request_parameters   = {
    "integration

.request.header.X-Authorization" = "'static'"
    "integration.request.header.X-Foo"           = "'Bar'"
  }
  type                 = "HTTP"
  uri                  = "http://nlb-test-api-968ccd5f6070696f.elb.ap-south-1.amazonaws.com"
  integration_http_method = "GET"
  passthrough_behavior = "WHEN_NO_MATCH"
  content_handling     = "CONVERT_TO_TEXT"
  connection_type      = "VPC_LINK"
  connection_id        = "iu62f9"
}

Make sure to replace the placeholders such as "*" with actual values for your environment.

beantwortet vor einem Jahr

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen