I'm learning terraform, and one of the well written tutorials on hashicorp has given me some problems towards the end.
I have a file defining my lambda, and its relationship to an API Gateway. The connections to the API Gateway haven't been tested, since I can't seem to get the API Gateway to deploy.
lambda.tf
provider "aws" {
region = "us-east-1"
shared_credentials_file = "/home/camelType/.aws/credentials"
profile = "default"
}
resource "aws_lambda_function" "example" {
function_name = "ServerlessExample"
# The bucket name as created earlier with "aws s3api create-bucket"
s3_bucket = "camelType-serverless-example"
s3_key = "v1.0.0/example.zip"
# "main" is the filename within the zip file (main.js) and "handler"
# is the name of the property under which the handler function was
# exported in that file.
handler = "main.handler"
runtime = "nodejs12.x"
role = "${aws_iam_role.lambda_exec_role.arn}"
}
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_exec_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
//Haven't tested below here
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = aws_api_gateway_rest_api.example.id
parent_id = aws_api_gateway_rest_api.example.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "proxy" {
rest_api_id = aws_api_gateway_rest_api.example.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = "ANY"
authorization = "NONE"
}
I've deployed the lambda alone, and it worked as expected.
However, when I went to the next step, and added the api gateway, I get a parsing error for the aws_api_gateway_rest_api.example.id. I assumed that the example.id was a reference to the aws_api_gateway_rest_api resource above, so the example.id name is correct, but that's the line it's having trouble parsing.
api_gateway.tf
resource "aws_api_gateway_rest_api" "example" {
name = "ServerlessExample"
description = "Terraform Serverless Application Example"
}
resource "aws_api_gateway_integration" "lambda" {
//issue here
rest_api_id = aws_api_gateway_rest_api.example.id
resource_id = aws_api_gateway_method.proxy.resource_id
http_method = aws_api_gateway_method.proxy.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.example.invoke_arn
}
resource "aws_api_gateway_method" "proxy_root" {
rest_api_id = aws_api_gateway_rest_api.example.id
resource_id = aws_api_gateway_rest_api.example.root_resource_id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambda_root" {
rest_api_id = aws_api_gateway_rest_api.example.id
resource_id = aws_api_gateway_method.proxy_root.resource_id
http_method = aws_api_gateway_method.proxy_root.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.example.invoke_arn
}
resource "aws_api_gateway_deployment" "example" {
depends_on = [
aws_api_gateway_integration.lambda,
aws_api_gateway_integration.lambda_root,
]
rest_api_id = aws_api_gateway_rest_api.example.id
stage_name = "test"
}
I've gone through a few of their tutorials, and this is the first one I've had issues with. I'm sure it's my error, but I've gone over it several times, and can't seem to figure out what I'm doing wrong.
The copy paste of the error message is Error: Error parsing /home/camelType/api_gateway.tf: At 7:18: Unknown token: 7:18 IDENT aws_api_gateway_rest_api.example.id
Error: Error parsing /home/camelType/api_gateway.tf: At 7:18: Unknown token: 7:18 IDENT aws_api_gateway_rest_api.example.idError creating API Gateway: AccessDeniedExceptionwhich is much easier to troubleshoot.