Terraform - API Gateway


API Gateway

resource "aws_api_gateway_rest_api" "example" {
  name        = "dregdata-agwapi-dhs-authoriser-public-test"
  description = "API Gateway for Google Merchant Center"
  body = templatefile("authos.json", {
    title = "test-title",
    lambda_invoke_arn = "${aws_lambda_function.example.invoke_arn}"
  })

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}
resource "aws_api_gateway_deployment" "example" {
  rest_api_id = aws_api_gateway_rest_api.example.id

  triggers = {
    redeployment = sha1(jsonencode(aws_api_gateway_rest_api.example.body))
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "dev"
}

"x-amazon-apigateway-integration": {
  "httpMethod": "POST",
  "type": "aws_proxy",
  "uri": "${lambda_invoke_arn}"
}

LAMBDA

data "aws_iam_role" "example" {
  name = "may-int-map-role-x5qnl10f"
}

resource "aws_lambda_function" "example" {
  function_name = "example"
  role          = data.aws_iam_role.example.arn
  runtime       = "nodejs18.x"
  filename      = "${path.module}/artifact-app-v1.0.0.zip"
  handler       = "artifact-app-v1.0.0/index.handler"
  package_type  = "Zip"
}

resource "aws_lambda_permission" "apigw_lambda" {
  statement_id  = "AllowAPIGatewayInvoke"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.example.function_name
  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_api_gateway_rest_api.example.execution_arn}/*/POST/authorisation"
}