0

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

5
  • 1
    Can you post exact error message? Commented Aug 19, 2020 at 3:17
  • Yes, error message posted at end. Also here Error: Error parsing /home/camelType/api_gateway.tf: At 7:18: Unknown token: 7:18 IDENT aws_api_gateway_rest_api.example.id Commented Aug 19, 2020 at 3:30
  • 1
    What terraform version are you using? There are some similar issues indicating that you might be using old version which does no support new syntax? Commented Aug 19, 2020 at 4:10
  • 1
    @Marcin That was it. I haven't gotten used to snaps yet, and it installed v0.11.11. I thought I had already upgraded, but it was still using the snap. What a ridiculous mistake. Thanks! I'm now on v0.13.0, and now i'm getting a much more manageable Error creating API Gateway: AccessDeniedException which is much easier to troubleshoot. Commented Aug 19, 2020 at 4:26
  • No problem. If you don't mind I will provide an answer with more info for future references :-) Commented Aug 19, 2020 at 4:26

1 Answer 1

2

Based on the comments.

The issue was caused by using old version of terraform (v0.11.11), while using syntax from newer version in the config files.

Similar issues have been reported:

The solution was to upgrade terraform.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again for the help! One day I'll get used to snaps......or leave Ubuntu....We'll see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.