Terraform - AWS API Gateway - Swagger


resource "aws_api_gateway_rest_api" "api_gateway_api" {
  name        = "${var.application}-api-gateway-${var.environment}"
  description = "API Gateway for Google Merchant Center ${var.application}"
  body        = file("${path.module}/swaggers/authos.json")
}

# Get all resources
data "aws_api_gateway_resource" "authorisation" {
  rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
  path        = "/authorisation"
  depends_on  = [aws_api_gateway_rest_api.api_gateway_api]
}

# Define the integration
resource "aws_api_gateway_integration" "authorisation_integration" {
  rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
  resource_id = data.aws_api_gateway_resource.authorisation.id
  http_method = "POST"
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode({
      statusCode = 200
    })
  }
}

resource "aws_api_gateway_integration_response" "authorisation_integration_response" {
  rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
  resource_id = data.aws_api_gateway_resource.authorisation.id
  http_method = "POST"
  status_code = "201"

  response_templates = {
    "application/json" = jsonencode({
      dataHolderId              = "whitford"
      dataRecipientId           = "c4951483-d91f-47ac-8985-d6aa43323386"
      dataRecipientSoftwareId   = "string"
      industry                  = "35382ee5-bf04-4518-99db-564e8b283822"
      createdAt                 = "2019-08-24T14:15:22.453Z"
      keySetDH                  = "9ecd5c86-32d9-4e8c-8c30-68d8bd27af36"
      keySetDR                  = "1b382a11-910a-48e6-8004-3b8d46f99ad5"
      authDelegatedFromDH       = "VhdXBAvz9gAr-op7"
      authDelegatedFromDHNonce  = "ke-E_n9BPCENseTCNljJjSds1xPY2ngn"
      authDelegatedByDH         = "VhdXBAvz9gAr-op7"
      authDelegatedByDHNonce    = "ke-E_n9BPCENseTCNljJjSds1xPY2ngn"
      authDelegatedByDR         = "L6yGZT_lIhQQ_7mD"
      authDelegatedByDRNonce    = "ke-E_n9BPCENseTCNljJjSds1xPY2ngn"
      createdByFirstNameDH      = "JVjjihW5YPDY89K_4tJePw27OSfmqm9F"
      createdByFirstNameDHNonce = "sdfdfsW5YPDY89K_4tJePw27OSfmqm9F"
      createdByLastNameDH       = "woN88Yct8r8uRlyw48aI20xsUD0="
      createdByLastNameDHNonce  = "yu788Yct8dddr8uR48aI20xsUD0="
      onlineBankingIdDH         = "VhdXBAvz9gAr-op7"
      onlineBankingIdDHNonce    = "ke-E_n9BPCENseTCNljJjSds1xPY2ngn"
      authorisation = {
        authorisationId   = "string"
        scopes            = "bank:accounts.basic:read, bank:payees:read,profile"
        sharingDuration   = 86400000
        status            = "ACTIVE"
        sharedData        = [
          {
            dataIdentifierDR       = "M2KvtUnw6jNz6MJPYtI="
            dataIdentifierDRNonce  = "dhduyeuvtUnw6jNz6MJPYtI=n"
            maskedDataIdentifier   = "xxx-xxx xxxxx4567"
            customerDataRelationship = "SOLE"
          }
        ]
      }
    })
  }

  depends_on = [
    aws_api_gateway_integration.authorisation_integration,
  ]
}

resource "aws_api_gateway_deployment" "api_gateway_deployment" {
  depends_on = [
    aws_api_gateway_integration.authorisation_integration,
  ]

  rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id

  triggers = {
    redeployment = filesha256("${path.module}/swaggers/authos.json")
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "api_gateway_stage" {
  deployment_id = aws_api_gateway_deployment.api_gateway_deployment.id
  rest_api_id   = aws_api_gateway_rest_api.api_gateway_api.id
  stage_name    = var.environment
}