无服务器部署有效,但terraform部署失败

时间:2020-10-07 16:13:09

标签: aws-lambda aws-api-gateway

我正在使用以下Cloudformation模板从this repo部署MyLizardApp

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
  "Parameters": {},
  "Conditions": {},
  "Resources": {
    "AspNetCoreFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "MyLizardApp::MyLizardApp.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore3.1",
        "CodeUri": "",
        "MemorySize": 512,
        "Timeout": 30,
        "Role": { "Fn::Join" : [ "", [ "arn:aws:iam::",{"Ref":"AWS::AccountId"},":role/MyLizardAppServiceRole" ] ]},
        "Policies": [
          "AWSLambdaFullAccess"
        ],
        "Environment": {
          "Variables": {}
        },
        "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          },
          "RootResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "ANY"
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}

这是我的terraform设置:

main.tf

##################################################################################
# VARIABLES
##################################################################################

variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "terraform_execution_role" {}
variable "timeout" {}
variable "name" {}
variable "region" {
  default = "eu-west-2"
}

##################################################################################
# PROVIDERS
##################################################################################

provider "aws" {
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
  region     = var.region
}

provider "archive" {
  version = "~> 1.2"
}

##################################################################################
# DATA
##################################################################################

data "archive_file" "archive" {
  type        = "zip"
  source_dir  = "../bin/Release/netcoreapp3.1/publish"
  output_path = "${path.module}/source.zip"
}

##################################################################################
# RESOURCES
##################################################################################

resource "aws_lambda_function" "MyLizardApp" {
  function_name     = var.name
  handler           = "MyLizardApp::MyLizardApp.LambdaEntryPoint::FunctionHandlerAsync"
  runtime           = "dotnetcore3.1"
  role              = var.terraform_execution_role
  timeout           = var.timeout
  filename          = data.archive_file.archive.output_path
  source_code_hash  = data.archive_file.archive.output_base64sha256
}

##################################################################################
# OUTPUT
##################################################################################

output "aws_lambda_function" {
 value = aws_lambda_function.MyLizardApp
}

apigateway.tf

##################################################################################
# RESOURCES
##################################################################################

resource "aws_api_gateway_rest_api" "lizardapi" {
  name = var.name
}

resource "aws_api_gateway_resource" "proxy" {
  rest_api_id = aws_api_gateway_rest_api.lizardapi.id
  parent_id   = aws_api_gateway_rest_api.lizardapi.root_resource_id
  path_part   = "{proxy+}"
}

resource "aws_api_gateway_method" "proxy" {
  rest_api_id   = aws_api_gateway_rest_api.lizardapi.id
  resource_id   = aws_api_gateway_resource.proxy.id
  http_method   = "ANY"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "lambda" {
  rest_api_id = aws_api_gateway_rest_api.lizardapi.id
  resource_id = aws_api_gateway_method.proxy.resource_id
  http_method = aws_api_gateway_method.proxy.http_method

  integration_http_method = "ANY"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.MyLizardApp.invoke_arn
}

resource "aws_api_gateway_method" "proxy_root" {
  rest_api_id   = aws_api_gateway_rest_api.lizardapi.id
  resource_id   = aws_api_gateway_rest_api.lizardapi.root_resource_id
  http_method   = "ANY"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "lambda_root" {
  rest_api_id = aws_api_gateway_rest_api.lizardapi.id
  resource_id = aws_api_gateway_method.proxy_root.resource_id
  http_method = aws_api_gateway_method.proxy_root.http_method

  integration_http_method = aws_api_gateway_method.proxy_root.http_method
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.MyLizardApp.invoke_arn
}

resource "aws_api_gateway_deployment" "lizardapi" {
  depends_on = [
    aws_api_gateway_integration.lambda,
    aws_api_gateway_integration.lambda_root,
  ]

  rest_api_id = aws_api_gateway_rest_api.lizardapi.id
  stage_name = "test"
}

##################################################################################
# OUTPUT
##################################################################################

output "base_url" {
  value = aws_api_gateway_deployment.lizardapi.invoke_url
}

使用serverless模板进行部署时,该解决方案可以部署并正常运行(它在网页上显示当前时间),但是通过terraform进行部署时,我得到的只是一个500 Internal server error并且页面显示{"message": "Internal server error"}

我的地形错误吗?有什么方法可以调查出什么问题了吗?

0 个答案:

没有答案